@lsby/eslint-plugin 0.0.8 → 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.
- package/index.js +1 -0
- package/lib/rules/prefer-let.js +4 -4
- package/lib/rules/require-has-check.js +42 -0
- package/package.json +1 -1
package/index.js
CHANGED
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,42 @@
|
|
|
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
|
+
context.report({
|
|
25
|
+
node,
|
|
26
|
+
messageId: 'useHas',
|
|
27
|
+
fix(fixer) {
|
|
28
|
+
const sourceCode = context.getSourceCode()
|
|
29
|
+
const objectCode = sourceCode.getText(node.left.object)
|
|
30
|
+
const propertyCode = sourceCode.getText(node.left.property)
|
|
31
|
+
|
|
32
|
+
let replacement = `${objectCode}.hasOwnProperty(${propertyCode})`
|
|
33
|
+
replacement = `!${replacement}`
|
|
34
|
+
|
|
35
|
+
return fixer.replaceText(node, replacement)
|
|
36
|
+
},
|
|
37
|
+
})
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
}
|