@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 CHANGED
@@ -4,5 +4,6 @@ module.exports = {
4
4
  'prefer-let': require('./lib/rules/prefer-let'),
5
5
  'no-negation': require('./lib/rules/no-negation'),
6
6
  'no-null': require('./lib/rules/no-null'),
7
+ 'require-has-check': require('./lib/rules/require-has-check'),
7
8
  },
8
9
  }
@@ -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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lsby/eslint-plugin",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "pub:public": "bumpp && npm publish --access public",