@lsby/eslint-plugin 0.0.7 → 0.0.9
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 -0
- package/lib/rules/no-null.js +22 -26
- package/lib/rules/prefer-let.js +4 -4
- package/lib/rules/require-has-check.js +41 -0
- package/package.json +1 -1
package/index.js
CHANGED
package/lib/rules/no-null.js
CHANGED
|
@@ -1,31 +1,27 @@
|
|
|
1
1
|
module.exports = {
|
|
2
|
-
|
|
3
|
-
'
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
},
|
|
24
|
-
})
|
|
25
|
-
}
|
|
26
|
-
},
|
|
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
|
+
})
|
|
27
23
|
}
|
|
28
24
|
},
|
|
29
|
-
}
|
|
25
|
+
}
|
|
30
26
|
},
|
|
31
27
|
}
|
package/lib/rules/prefer-let.js
CHANGED
|
@@ -6,15 +6,15 @@ module.exports = {
|
|
|
6
6
|
category: 'Best Practices',
|
|
7
7
|
recommended: false,
|
|
8
8
|
},
|
|
9
|
-
fixable: 'code',
|
|
10
|
-
schema: [],
|
|
9
|
+
fixable: 'code',
|
|
10
|
+
schema: [],
|
|
11
11
|
},
|
|
12
12
|
|
|
13
13
|
create(context) {
|
|
14
14
|
return {
|
|
15
15
|
VariableDeclaration(node) {
|
|
16
|
-
//
|
|
17
|
-
if (node.kind === 'const' || node.kind === 'var') {
|
|
16
|
+
// 检查变量声明的类型(排除 declare)
|
|
17
|
+
if ((node.kind === 'const' || node.kind === 'var') && !node.declare) {
|
|
18
18
|
// 使用修复功能将 const 或 var 替换为 let
|
|
19
19
|
context.report({
|
|
20
20
|
node,
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
meta: {
|
|
3
|
+
type: 'problem',
|
|
4
|
+
docs: {
|
|
5
|
+
description: 'Enforce using `hasOwnProperty` or `Map.has` instead of unsafe index checks',
|
|
6
|
+
category: 'Best Practices',
|
|
7
|
+
recommended: false,
|
|
8
|
+
},
|
|
9
|
+
fixable: 'code', // 自动修复支持
|
|
10
|
+
messages: {
|
|
11
|
+
useHas:
|
|
12
|
+
'Avoid comparing with `undefined` or `null` directly. Use `hasOwnProperty` or `Map.has` for existence checks.',
|
|
13
|
+
},
|
|
14
|
+
schema: [], // No options
|
|
15
|
+
},
|
|
16
|
+
create(context) {
|
|
17
|
+
return {
|
|
18
|
+
BinaryExpression(node) {
|
|
19
|
+
if (
|
|
20
|
+
['===', '!=='].includes(node.operator) &&
|
|
21
|
+
node.left.type === 'MemberExpression' &&
|
|
22
|
+
['undefined', 'null'].includes(node.right.type === 'Identifier' ? node.right.name : node.right.raw)
|
|
23
|
+
) {
|
|
24
|
+
context.report({
|
|
25
|
+
node,
|
|
26
|
+
messageId: 'useHas',
|
|
27
|
+
fix(fixer) {
|
|
28
|
+
// 自动修复逻辑
|
|
29
|
+
const sourceCode = context.getSourceCode()
|
|
30
|
+
const objectCode = sourceCode.getText(node.left.object)
|
|
31
|
+
const propertyCode = sourceCode.getText(node.left.property)
|
|
32
|
+
const isNegative = node.operator === '!=='
|
|
33
|
+
const replacement = `${objectCode}.hasOwnProperty(${propertyCode})`
|
|
34
|
+
return fixer.replaceText(node, isNegative ? `!${replacement}` : replacement)
|
|
35
|
+
},
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
}
|