@nest-boot/eslint-plugin 5.1.1 → 5.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,24 +1,24 @@
1
1
  /**
2
2
  * @fileoverview 实体字段不能使用可选属性和非空断言
3
- * @author 实体字段不能使用可选属性和非空断言
3
+ * @author D4rkCr0w
4
4
  */
5
5
  "use strict";
6
6
 
7
- //------------------------------------------------------------------------------
8
- // Rule Definition
9
- //------------------------------------------------------------------------------
10
-
11
7
  /** @type {import('eslint').Rule.RuleModule} */
12
8
  module.exports = {
13
9
  meta: {
14
- type: null, // `problem`, `suggestion`, or `layout`
10
+ type: "problem",
15
11
  docs: {
16
12
  description: "实体字段不能使用可选属性和非空断言",
17
13
  recommended: false,
18
- url: null, // URL to the documentation page for this rule
14
+ url: null,
15
+ },
16
+ fixable: "code",
17
+ schema: [],
18
+ messages: {
19
+ entityPropertyNoOptionalOrNonNullAssertion:
20
+ "实体字段不能使用可选属性和非空断言。",
19
21
  },
20
- fixable: null, // Or `code` or `whitespace`
21
- schema: [], // Add a schema if the rule has options
22
22
  },
23
23
 
24
24
  create(context) {
@@ -46,7 +46,28 @@ module.exports = {
46
46
  if (property.optional || property.definite) {
47
47
  context.report({
48
48
  node: property,
49
- message: "实体属性不能有 ? 或 ! 断言。",
49
+ messageId: "entityPropertyNoOptionalOrNonNullAssertion",
50
+ fix: (fixer) => {
51
+ const tokenBefore = context.sourceCode.getTokenBefore(
52
+ property.key,
53
+ );
54
+ const propertyStart =
55
+ tokenBefore && tokenBefore.range[1] < property.range[0]
56
+ ? tokenBefore.range[1]
57
+ : property.range[0];
58
+ const propertyEnd = property.range[1];
59
+
60
+ // 从属性中移除 ? 或 ! 符号
61
+ const fixedPropertyText = context.sourceCode
62
+ .getText()
63
+ .slice(propertyStart, propertyEnd)
64
+ .replace(/\?|!/, "");
65
+
66
+ return fixer.replaceTextRange(
67
+ [propertyStart, propertyEnd],
68
+ fixedPropertyText,
69
+ );
70
+ },
50
71
  });
51
72
  }
52
73
  }
@@ -1,39 +1,27 @@
1
1
  /**
2
2
  * @fileoverview 实体字段可为空时属性类型
3
- * @author 实体字段可为空时属性类型
3
+ * @author D4rkCr0w
4
4
  */
5
5
  "use strict";
6
6
 
7
- //------------------------------------------------------------------------------
8
- // Rule Definition
9
- //------------------------------------------------------------------------------
10
-
11
7
  /** @type {import('eslint').Rule.RuleModule} */
12
8
  module.exports = {
13
9
  meta: {
14
- type: null, // `problem`, `suggestion`, or `layout`
10
+ type: "problem",
15
11
  docs: {
16
12
  description: "实体字段可为空时属性类型",
17
13
  recommended: false,
18
- url: null, // URL to the documentation page for this rule
14
+ url: null,
15
+ },
16
+ fixable: "code",
17
+ schema: [],
18
+ messages: {
19
+ entityPropertyNullable:
20
+ "@Property({ nullable: true }) 属性类型需包含 null。",
19
21
  },
20
- fixable: null, // Or `code` or `whitespace`
21
- schema: [], // Add a schema if the rule has options
22
22
  },
23
23
 
24
24
  create(context) {
25
- // variables should be defined here
26
-
27
- //----------------------------------------------------------------------
28
- // Helpers
29
- //----------------------------------------------------------------------
30
-
31
- // any helper functions should go here or else delete this section
32
-
33
- //----------------------------------------------------------------------
34
- // Public
35
- //----------------------------------------------------------------------
36
-
37
25
  return {
38
26
  ClassDeclaration(node) {
39
27
  // 检查是否有 @Entity 装饰器
@@ -45,13 +33,13 @@ module.exports = {
45
33
  ) {
46
34
  // 遍历类属性
47
35
  node.body.body.forEach((property) => {
48
- // 检查是否有 @Property 装饰器
36
+ // 检查是否有 @Property() 装饰器
49
37
  if (property.type === "PropertyDefinition" && property.decorators) {
50
38
  const propertyDecorator = property.decorators.find(
51
39
  (decorator) => decorator.expression.callee.name === "Property",
52
40
  );
53
41
 
54
- // 检查是否有 @Property(nullable: true) 装饰器
42
+ // 检查 @Property() 装饰器 nullable 参数是否为 true
55
43
  if (
56
44
  propertyDecorator &&
57
45
  propertyDecorator.expression.arguments[0] &&
@@ -74,8 +62,23 @@ module.exports = {
74
62
  ) {
75
63
  context.report({
76
64
  node: property,
77
- message:
78
- "@Property({ nullable: true }) 属性类型需包含 null。",
65
+ messageId: "entityPropertyNullable",
66
+ fix: (fixer) => {
67
+ const typeAnnotationStart =
68
+ property.typeAnnotation.range[0];
69
+ const typeAnnotationEnd =
70
+ property.typeAnnotation.range[1];
71
+ const originalType = context.sourceCode.getText(
72
+ property.typeAnnotation,
73
+ );
74
+
75
+ return fixer.replaceTextRange(
76
+ [typeAnnotationStart, typeAnnotationEnd],
77
+ `${originalType} | null${
78
+ property.value ? "" : " = null"
79
+ }`,
80
+ );
81
+ },
79
82
  });
80
83
  }
81
84
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nest-boot/eslint-plugin",
3
- "version": "5.1.1",
3
+ "version": "5.1.2",
4
4
  "license": "MIT",
5
5
  "main": "./lib/index.js",
6
6
  "exports": "./lib/index.js",
@@ -8,16 +8,15 @@
8
8
  // Requirements
9
9
  //------------------------------------------------------------------------------
10
10
 
11
- const rule = require("../../../lib/rules/entity-property-nullable-type"),
11
+ const rule = require("../../../lib/rules/entity-property-nullable"),
12
12
  RuleTester = require("eslint").RuleTester;
13
13
 
14
-
15
14
  //------------------------------------------------------------------------------
16
15
  // Tests
17
16
  //------------------------------------------------------------------------------
18
17
 
19
18
  const ruleTester = new RuleTester();
20
- ruleTester.run("entity-property-nullable-type", rule, {
19
+ ruleTester.run("entity-property-nullable", rule, {
21
20
  valid: [
22
21
  // give me some code that won't trigger a warning
23
22
  ],