@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 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
  }
@@ -1,31 +1,27 @@
1
1
  module.exports = {
2
- rules: {
3
- 'no-null': {
4
- meta: {
5
- type: 'suggestion', // 表示这是个建议类型的规则
6
- docs: {
7
- description: 'Disallow usage of null and replace it with undefined',
8
- category: 'Best Practices',
9
- recommended: false,
10
- },
11
- fixable: 'code', // 支持自动修复
12
- schema: [], // 无额外配置
13
- },
14
- create(context) {
15
- return {
16
- Literal(node) {
17
- if (node.value === null) {
18
- context.report({
19
- node,
20
- message: '不允许使用‘null’。使用‘undefined’代替。',
21
- fix(fixer) {
22
- return fixer.replaceText(node, 'undefined')
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
  }
@@ -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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lsby/eslint-plugin",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "pub:public": "bumpp && npm publish --access public",