@lsby/eslint-plugin 0.0.17 → 0.0.18
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
|
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const no_definite_assignment_assertion_1 = __importDefault(require("./lib/rules/no-definite-assignment-assertion"));
|
|
7
|
-
const
|
|
7
|
+
const no_else_on_equality_1 = __importDefault(require("./lib/rules/no-else-on-equality"));
|
|
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"));
|
|
@@ -14,7 +14,7 @@ module.exports = {
|
|
|
14
14
|
'prefer-let': prefer_let_1.default,
|
|
15
15
|
'no-negation': no_negation_1.default,
|
|
16
16
|
'no-definite-assignment-assertion': no_definite_assignment_assertion_1.default,
|
|
17
|
-
'no-else':
|
|
17
|
+
'no-else-on-equality': no_else_on_equality_1.default,
|
|
18
18
|
'no-switch-default': no_switch_default_1.default,
|
|
19
19
|
'prefer-switch-over-multi-if': prefer_switch_over_multi_if_1.default,
|
|
20
20
|
},
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// 禁止 else(当条件是等于或不等于时)
|
|
3
|
+
// 对于等于/不等于的条件判断,else 表示"除当前条件外的所有可能"
|
|
4
|
+
// 当状态集合未来扩展时, 依然会被包含在else分支里, 导致状态遗漏却无任何报错
|
|
5
|
+
// 应当使用提早返回 或 switch + 穷尽检查
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
const rule = {
|
|
8
|
+
meta: {
|
|
9
|
+
type: 'problem',
|
|
10
|
+
docs: { description: '当条件是等于或不等于时,禁止使用 else 分支,防止隐含的状态遗漏' },
|
|
11
|
+
messages: {
|
|
12
|
+
noElse: '禁止在等于/不等于条件下使用 else 分支。当条件状态扩展时, else 分支可能遗漏新的状态。请改用提前返回(early return)或 switch 语句',
|
|
13
|
+
},
|
|
14
|
+
schema: [],
|
|
15
|
+
},
|
|
16
|
+
defaultOptions: [],
|
|
17
|
+
create(context) {
|
|
18
|
+
/**
|
|
19
|
+
* 检查条件中是否包含等于或不等于操作符
|
|
20
|
+
*/
|
|
21
|
+
function hasEqualityOperator(node) {
|
|
22
|
+
if (node.type === 'BinaryExpression') {
|
|
23
|
+
return ['===', '!==', '==', '!='].includes(node.operator);
|
|
24
|
+
}
|
|
25
|
+
if (node.type === 'LogicalExpression') {
|
|
26
|
+
return hasEqualityOperator(node.left) || hasEqualityOperator(node.right);
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
IfStatement(node) {
|
|
32
|
+
// 仅在 if 条件是等于或不等于的情况下,禁止 else
|
|
33
|
+
if (node.alternate !== null && hasEqualityOperator(node.test)) {
|
|
34
|
+
context.report({ node: node.alternate, messageId: 'noElse' });
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
exports.default = rule;
|
package/package.json
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lsby/eslint-plugin",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.18",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist"
|
|
8
8
|
],
|
|
9
9
|
"scripts": {
|
|
10
|
-
"build": "tsc",
|
|
10
|
+
"build": "rimraf dist && tsc",
|
|
11
11
|
"check:all": "npm run check:format && npm run check:type",
|
|
12
12
|
"check:format": "prettier --write .",
|
|
13
13
|
"check:type": "tsc --noEmit",
|
|
14
14
|
"dev": "tsc --watch",
|
|
15
15
|
"pub:public": "npm run build && npm run test && bumpp && npm publish --access public",
|
|
16
|
-
"test": "mocha test/**/*.test.js"
|
|
16
|
+
"test": "npm run build && mocha test/**/*.test.js"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
19
|
"@ianvs/prettier-plugin-sort-imports": "^4.2.1",
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"mocha": "^10.7.3",
|
|
28
28
|
"prettier": "^3.7.4",
|
|
29
29
|
"prettier-plugin-packagejson": "^2.5.20",
|
|
30
|
+
"rimraf": "^6.1.2",
|
|
30
31
|
"typescript": "^5.3.0"
|
|
31
32
|
},
|
|
32
33
|
"peerDependencies": {
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// 禁止 else
|
|
3
|
-
// else 表示"除当前条件外的所有可能"
|
|
4
|
-
// 当状态集合未来扩展时, 依然会被包含在else分支里, 导致状态遗漏却无任何报错
|
|
5
|
-
// 应当使用提早返回 或 switch + 穷尽检查
|
|
6
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
const rule = {
|
|
8
|
-
meta: {
|
|
9
|
-
type: 'problem',
|
|
10
|
-
docs: { description: '禁止使用 else 分支, 防止隐含的状态遗漏' },
|
|
11
|
-
messages: {
|
|
12
|
-
noElse: '禁止使用 else 分支。当条件状态扩展时, else 分支可能遗漏新的状态。请改用提前返回(early return)或 switch 语句',
|
|
13
|
-
},
|
|
14
|
-
schema: [],
|
|
15
|
-
},
|
|
16
|
-
defaultOptions: [],
|
|
17
|
-
create(context) {
|
|
18
|
-
return {
|
|
19
|
-
IfStatement(node) {
|
|
20
|
-
if (node.alternate !== null) {
|
|
21
|
-
context.report({ node: node.alternate, messageId: 'noElse' });
|
|
22
|
-
}
|
|
23
|
-
},
|
|
24
|
-
};
|
|
25
|
-
},
|
|
26
|
-
};
|
|
27
|
-
exports.default = rule;
|
|
File without changes
|