@lsby/eslint-plugin 0.0.18 → 0.0.20

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/dist/index.js CHANGED
@@ -8,7 +8,6 @@ const no_else_on_equality_1 = __importDefault(require("./lib/rules/no-else-on-eq
8
8
  const no_negation_1 = __importDefault(require("./lib/rules/no-negation"));
9
9
  const no_switch_default_1 = __importDefault(require("./lib/rules/no-switch-default"));
10
10
  const prefer_let_1 = __importDefault(require("./lib/rules/prefer-let"));
11
- const prefer_switch_over_multi_if_1 = __importDefault(require("./lib/rules/prefer-switch-over-multi-if"));
12
11
  module.exports = {
13
12
  rules: {
14
13
  'prefer-let': prefer_let_1.default,
@@ -16,6 +15,5 @@ module.exports = {
16
15
  'no-definite-assignment-assertion': no_definite_assignment_assertion_1.default,
17
16
  'no-else-on-equality': no_else_on_equality_1.default,
18
17
  'no-switch-default': no_switch_default_1.default,
19
- 'prefer-switch-over-multi-if': prefer_switch_over_multi_if_1.default,
20
18
  },
21
19
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lsby/eslint-plugin",
3
- "version": "0.0.18",
3
+ "version": "0.0.20",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [
@@ -1,3 +0,0 @@
1
- import type { TSESLint } from '@typescript-eslint/utils';
2
- declare const rule: TSESLint.RuleModule<'preferSwitch', []>;
3
- export default rule;
@@ -1,108 +0,0 @@
1
- "use strict";
2
- // 建议使用 switch 替代多个 if 判断同一变量
3
- // 当有多个连续的 if 语句判断同一个表达式时, 应该使用 switch 来提高代码的可读性
4
- Object.defineProperty(exports, "__esModule", { value: true });
5
- const rule = {
6
- meta: {
7
- type: 'suggestion',
8
- docs: { description: '检测多个连续 if 判断同一个表达式,建议使用 switch 提高可读性' },
9
- messages: { preferSwitch: '检测到 {{count}} 个连续的 if 判断同一个表达式,可以考虑使用 switch 提高可读性' },
10
- schema: [],
11
- },
12
- defaultOptions: [],
13
- create(context) {
14
- const sourceCode = context.sourceCode;
15
- const minIfs = 2;
16
- function getText(node) {
17
- return sourceCode.getText(node);
18
- }
19
- function isValidCaseValue(node) {
20
- return (node.type === 'Literal' ||
21
- node.type === 'TemplateLiteral' ||
22
- node.type === 'Identifier' ||
23
- node.type === 'MemberExpression');
24
- }
25
- function getSwitchInfo(test) {
26
- if (test.type !== 'BinaryExpression')
27
- return null;
28
- // 支持所有的二元比较操作符
29
- const comparisonOps = ['===', '==', '!==', '!=', '>', '<', '>=', '<='];
30
- if (!comparisonOps.includes(test.operator))
31
- return null;
32
- const left = test.left;
33
- const right = test.right;
34
- if ((left.type === 'Identifier' || left.type === 'MemberExpression') && isValidCaseValue(right)) {
35
- return { variable: getText(left), value: getText(right) };
36
- }
37
- if ((right.type === 'Identifier' || right.type === 'MemberExpression') && isValidCaseValue(left)) {
38
- return { variable: getText(right), value: getText(left) };
39
- }
40
- return null;
41
- }
42
- function collectIfChain(node) {
43
- const chain = [node];
44
- let current = node;
45
- // 收集 else-if 链
46
- while (current.alternate && current.alternate.type === 'IfStatement') {
47
- chain.push(current.alternate);
48
- current = current.alternate;
49
- }
50
- // 如果没有 else-if,尝试收集相邻的 if 语句
51
- if (chain.length === 1 && node.parent) {
52
- const parent = node.parent;
53
- let body = null;
54
- if (parent.type === 'BlockStatement') {
55
- body = parent.body;
56
- }
57
- else if (parent.type === 'Program') {
58
- body = parent.body;
59
- }
60
- if (body) {
61
- const nodeIndex = body.indexOf(node);
62
- if (nodeIndex !== -1) {
63
- const firstInfo = getSwitchInfo(node.test);
64
- if (firstInfo) {
65
- let nextIndex = nodeIndex + 1;
66
- while (nextIndex < body.length && body[nextIndex].type === 'IfStatement') {
67
- const nextIfStmt = body[nextIndex];
68
- if (nextIfStmt.alternate)
69
- break; // 如果有 else/else-if,停止
70
- const nextInfo = getSwitchInfo(nextIfStmt.test);
71
- if (!nextInfo || nextInfo.variable !== firstInfo.variable)
72
- break; // 变量不同,停止
73
- chain.push(nextIfStmt);
74
- nextIndex++;
75
- }
76
- }
77
- }
78
- }
79
- }
80
- return chain;
81
- }
82
- function allSameVariable(chain) {
83
- if (chain.length < minIfs)
84
- return null;
85
- const firstInfo = getSwitchInfo(chain[0].test);
86
- if (!firstInfo)
87
- return null;
88
- for (const stmt of chain.slice(1)) {
89
- const info = getSwitchInfo(stmt.test);
90
- if (!info || info.variable !== firstInfo.variable)
91
- return null;
92
- }
93
- return { variable: firstInfo.variable, count: chain.length };
94
- }
95
- return {
96
- IfStatement(node) {
97
- if (node.parent?.type === 'IfStatement' && node.parent.alternate === node)
98
- return;
99
- const chain = collectIfChain(node);
100
- const info = allSameVariable(chain);
101
- if (!info)
102
- return;
103
- context.report({ node, messageId: 'preferSwitch', data: { count: info.count } });
104
- },
105
- };
106
- },
107
- };
108
- exports.default = rule;