@lsby/eslint-plugin 0.0.9 → 0.0.11
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,45 @@ 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
|
+
// 检查是不是类实例的 null 比较, 比如 this.x === null
|
|
25
|
+
const isClassPropertyCheck = node.left.object.type === 'ThisExpression'
|
|
26
|
+
if (isClassPropertyCheck) {
|
|
27
|
+
// 如果是类实例的 null 检查,则跳过修复
|
|
28
|
+
return
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// 默认的 hasOwnProperty 检查
|
|
24
32
|
context.report({
|
|
25
33
|
node,
|
|
26
34
|
messageId: 'useHas',
|
|
27
35
|
fix(fixer) {
|
|
28
|
-
// 自动修复逻辑
|
|
29
36
|
const sourceCode = context.getSourceCode()
|
|
30
37
|
const objectCode = sourceCode.getText(node.left.object)
|
|
31
38
|
const propertyCode = sourceCode.getText(node.left.property)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
39
|
+
|
|
40
|
+
let replacement = `${objectCode}.hasOwnProperty(${propertyCode})`
|
|
41
|
+
replacement = `!${replacement}`
|
|
42
|
+
|
|
43
|
+
return fixer.replaceText(node, replacement)
|
|
35
44
|
},
|
|
36
45
|
})
|
|
37
46
|
}
|