@lsby/eslint-plugin 0.0.14 → 0.0.16
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.
- package/index.js +1 -2
- package/lib/rules/no-broken-link.js +2 -0
- package/lib/rules/no-definite-assignment-assertion.js +3 -11
- package/lib/rules/no-else.js +30 -0
- package/lib/rules/no-negation.js +3 -0
- package/lib/rules/prefer-let.js +6 -0
- package/package.json +1 -1
- package/lib/rules/no-null.js +0 -27
- package/lib/rules/require-has-check.js +0 -50
package/index.js
CHANGED
|
@@ -3,8 +3,7 @@ module.exports = {
|
|
|
3
3
|
'no-broken-link': require('./lib/rules/no-broken-link'),
|
|
4
4
|
'prefer-let': require('./lib/rules/prefer-let'),
|
|
5
5
|
'no-negation': require('./lib/rules/no-negation'),
|
|
6
|
-
'no-null': require('./lib/rules/no-null'),
|
|
7
|
-
'require-has-check': require('./lib/rules/require-has-check'),
|
|
8
6
|
'no-definite-assignment-assertion': require('./lib/rules/no-definite-assignment-assertion'),
|
|
7
|
+
'no-else': require('./lib/rules/no-else'),
|
|
9
8
|
},
|
|
10
9
|
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// 禁止类属性使用确定赋值断言语法
|
|
2
|
+
|
|
1
3
|
module.exports = {
|
|
2
4
|
meta: {
|
|
3
5
|
type: 'problem',
|
|
@@ -6,7 +8,7 @@ module.exports = {
|
|
|
6
8
|
recommended: 'error',
|
|
7
9
|
},
|
|
8
10
|
messages: {
|
|
9
|
-
noDefiniteAssignment: '
|
|
11
|
+
noDefiniteAssignment: '类属性不允许使用确定赋值断言 (!:)',
|
|
10
12
|
},
|
|
11
13
|
schema: [],
|
|
12
14
|
},
|
|
@@ -14,27 +16,17 @@ module.exports = {
|
|
|
14
16
|
return {
|
|
15
17
|
ClassProperty(node) {
|
|
16
18
|
if (node.definite) {
|
|
17
|
-
let name = '未知属性名'
|
|
18
|
-
if (node.key && node.key.type === 'Identifier') {
|
|
19
|
-
name = node.key.name
|
|
20
|
-
}
|
|
21
19
|
context.report({
|
|
22
20
|
node,
|
|
23
21
|
messageId: 'noDefiniteAssignment',
|
|
24
|
-
data: { name },
|
|
25
22
|
})
|
|
26
23
|
}
|
|
27
24
|
},
|
|
28
25
|
PropertyDefinition(node) {
|
|
29
26
|
if (node.definite) {
|
|
30
|
-
let name = '未知属性名'
|
|
31
|
-
if (node.key && node.key.type === 'Identifier') {
|
|
32
|
-
name = node.key.name
|
|
33
|
-
}
|
|
34
27
|
context.report({
|
|
35
28
|
node,
|
|
36
29
|
messageId: 'noDefiniteAssignment',
|
|
37
|
-
data: { name },
|
|
38
30
|
})
|
|
39
31
|
}
|
|
40
32
|
},
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// 禁止 else
|
|
2
|
+
// else 表示"除当前条件外的所有可能"
|
|
3
|
+
// 当状态集合未来扩展时, 依然会被包含在else分支里, 导致状态遗漏却无任何报错
|
|
4
|
+
// 应当使用提早返回 或 switch + 穷尽检查
|
|
5
|
+
|
|
6
|
+
module.exports = {
|
|
7
|
+
meta: {
|
|
8
|
+
type: 'problem',
|
|
9
|
+
docs: {
|
|
10
|
+
description: '禁止使用 else, 避免不稳定的否定剩余分支',
|
|
11
|
+
},
|
|
12
|
+
messages: {
|
|
13
|
+
noElse: '禁止使用 else, 请改用 early return 或 switch',
|
|
14
|
+
},
|
|
15
|
+
schema: [],
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
create(context) {
|
|
19
|
+
return {
|
|
20
|
+
IfStatement(node) {
|
|
21
|
+
if (node.alternate !== null) {
|
|
22
|
+
context.report({
|
|
23
|
+
node: node.alternate,
|
|
24
|
+
messageId: 'noElse',
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
}
|
package/lib/rules/no-negation.js
CHANGED
package/lib/rules/prefer-let.js
CHANGED
package/package.json
CHANGED
package/lib/rules/no-null.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
meta: {
|
|
3
|
-
type: 'suggestion', // 表示这是个建议类型的规则
|
|
4
|
-
docs: {
|
|
5
|
-
description: 'Disallow usage of null and replace it with undefined',
|
|
6
|
-
category: 'Best Practices',
|
|
7
|
-
recommended: false,
|
|
8
|
-
},
|
|
9
|
-
fixable: 'code', // 支持自动修复
|
|
10
|
-
schema: [], // 无额外配置
|
|
11
|
-
},
|
|
12
|
-
create(context) {
|
|
13
|
-
return {
|
|
14
|
-
Literal(node) {
|
|
15
|
-
if (node.value === null) {
|
|
16
|
-
context.report({
|
|
17
|
-
node,
|
|
18
|
-
message: '不允许使用‘null’。使用‘undefined’代替。',
|
|
19
|
-
fix(fixer) {
|
|
20
|
-
return fixer.replaceText(node, 'undefined')
|
|
21
|
-
},
|
|
22
|
-
})
|
|
23
|
-
}
|
|
24
|
-
},
|
|
25
|
-
}
|
|
26
|
-
},
|
|
27
|
-
}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
meta: {
|
|
3
|
-
type: 'problem',
|
|
4
|
-
docs: {
|
|
5
|
-
description: 'Enforce using `hasOwnProperty` instead of unsafe `null` checks',
|
|
6
|
-
category: 'Best Practices',
|
|
7
|
-
recommended: false,
|
|
8
|
-
},
|
|
9
|
-
fixable: 'code', // 自动修复支持
|
|
10
|
-
messages: {
|
|
11
|
-
useHas: 'Avoid comparing with `null` directly. Use `hasOwnProperty` for existence checks.',
|
|
12
|
-
},
|
|
13
|
-
schema: [], // No options
|
|
14
|
-
},
|
|
15
|
-
create(context) {
|
|
16
|
-
return {
|
|
17
|
-
BinaryExpression(node) {
|
|
18
|
-
// 检查是不是 a[x] === null 或 a[x] !== null
|
|
19
|
-
if (
|
|
20
|
-
['===', '!=='].includes(node.operator) &&
|
|
21
|
-
node.left.type === 'MemberExpression' &&
|
|
22
|
-
node.right.raw === 'null'
|
|
23
|
-
) {
|
|
24
|
-
// 检查是不是类实例的 null 比较, 比如 this.x === null
|
|
25
|
-
const isClassPropertyCheck = node.left.object.type === 'ThisExpression'
|
|
26
|
-
if (isClassPropertyCheck) {
|
|
27
|
-
// 如果是类实例的 null 检查,则跳过修复
|
|
28
|
-
return
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// 默认的 hasOwnProperty 检查
|
|
32
|
-
context.report({
|
|
33
|
-
node,
|
|
34
|
-
messageId: 'useHas',
|
|
35
|
-
fix(fixer) {
|
|
36
|
-
const sourceCode = context.getSourceCode()
|
|
37
|
-
const objectCode = sourceCode.getText(node.left.object)
|
|
38
|
-
const propertyCode = sourceCode.getText(node.left.property)
|
|
39
|
-
|
|
40
|
-
let replacement = `${objectCode}.hasOwnProperty(${propertyCode})`
|
|
41
|
-
replacement = `!${replacement}`
|
|
42
|
-
|
|
43
|
-
return fixer.replaceText(node, replacement)
|
|
44
|
-
},
|
|
45
|
-
})
|
|
46
|
-
}
|
|
47
|
-
},
|
|
48
|
-
}
|
|
49
|
-
},
|
|
50
|
-
}
|