@lsby/eslint-plugin 0.0.9 → 0.0.10
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.
|
@@ -2,36 +2,37 @@ module.exports = {
|
|
|
2
2
|
meta: {
|
|
3
3
|
type: 'problem',
|
|
4
4
|
docs: {
|
|
5
|
-
description: 'Enforce using `hasOwnProperty`
|
|
5
|
+
description: 'Enforce using `hasOwnProperty` instead of unsafe `null` checks',
|
|
6
6
|
category: 'Best Practices',
|
|
7
7
|
recommended: false,
|
|
8
8
|
},
|
|
9
9
|
fixable: 'code', // 自动修复支持
|
|
10
10
|
messages: {
|
|
11
|
-
useHas:
|
|
12
|
-
'Avoid comparing with `undefined` or `null` directly. Use `hasOwnProperty` or `Map.has` for existence checks.',
|
|
11
|
+
useHas: 'Avoid comparing with `null` directly. Use `hasOwnProperty` for existence checks.',
|
|
13
12
|
},
|
|
14
13
|
schema: [], // No options
|
|
15
14
|
},
|
|
16
15
|
create(context) {
|
|
17
16
|
return {
|
|
18
17
|
BinaryExpression(node) {
|
|
18
|
+
// 检查是不是 a[x] === null 或 a[x] !== null
|
|
19
19
|
if (
|
|
20
20
|
['===', '!=='].includes(node.operator) &&
|
|
21
21
|
node.left.type === 'MemberExpression' &&
|
|
22
|
-
|
|
22
|
+
node.right.raw === 'null'
|
|
23
23
|
) {
|
|
24
24
|
context.report({
|
|
25
25
|
node,
|
|
26
26
|
messageId: 'useHas',
|
|
27
27
|
fix(fixer) {
|
|
28
|
-
// 自动修复逻辑
|
|
29
28
|
const sourceCode = context.getSourceCode()
|
|
30
29
|
const objectCode = sourceCode.getText(node.left.object)
|
|
31
30
|
const propertyCode = sourceCode.getText(node.left.property)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
|
|
32
|
+
let replacement = `${objectCode}.hasOwnProperty(${propertyCode})`
|
|
33
|
+
replacement = `!${replacement}`
|
|
34
|
+
|
|
35
|
+
return fixer.replaceText(node, replacement)
|
|
35
36
|
},
|
|
36
37
|
})
|
|
37
38
|
}
|