@lsby/eslint-plugin 0.0.19 → 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
|
@@ -4,7 +4,6 @@ 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 no_early_return_on_equality_1 = __importDefault(require("./lib/rules/no-early-return-on-equality"));
|
|
8
7
|
const no_else_on_equality_1 = __importDefault(require("./lib/rules/no-else-on-equality"));
|
|
9
8
|
const no_negation_1 = __importDefault(require("./lib/rules/no-negation"));
|
|
10
9
|
const no_switch_default_1 = __importDefault(require("./lib/rules/no-switch-default"));
|
|
@@ -14,7 +13,6 @@ module.exports = {
|
|
|
14
13
|
'prefer-let': prefer_let_1.default,
|
|
15
14
|
'no-negation': no_negation_1.default,
|
|
16
15
|
'no-definite-assignment-assertion': no_definite_assignment_assertion_1.default,
|
|
17
|
-
'no-early-return-on-equality': no_early_return_on_equality_1.default,
|
|
18
16
|
'no-else-on-equality': no_else_on_equality_1.default,
|
|
19
17
|
'no-switch-default': no_switch_default_1.default,
|
|
20
18
|
},
|
package/package.json
CHANGED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// 禁止等于条件的if提前返回,但允许不等于条件的提前返回
|
|
3
|
-
// 使用等于条件的提前返回容易遗漏状态,应该用正向的不等于条件或结构化流程
|
|
4
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
-
const rule = {
|
|
6
|
-
meta: {
|
|
7
|
-
type: 'problem',
|
|
8
|
-
docs: {
|
|
9
|
-
description: '禁止在等于条件下使用提前返回(early return)。使用不等于条件的提前返回会更清晰地表达程序的主要流程',
|
|
10
|
-
},
|
|
11
|
-
messages: {
|
|
12
|
-
noEarlyReturnOnEquality: '禁止在等于条件下使用提前返回。建议改用不等于条件的提前返回,使主流程更清晰。例如:将 if (x === y) return; 改为 if (x !== y) { 主要逻辑 }',
|
|
13
|
-
},
|
|
14
|
-
schema: [],
|
|
15
|
-
},
|
|
16
|
-
defaultOptions: [],
|
|
17
|
-
create(context) {
|
|
18
|
-
/**
|
|
19
|
-
* 检查条件中是否是等于操作符(不包括不等于)
|
|
20
|
-
*/
|
|
21
|
-
function isEqualityOperator(node) {
|
|
22
|
-
if (node.type === 'BinaryExpression') {
|
|
23
|
-
return ['===', '=='].includes(node.operator);
|
|
24
|
-
}
|
|
25
|
-
if (node.type === 'LogicalExpression') {
|
|
26
|
-
return isEqualityOperator(node.left) || isEqualityOperator(node.right);
|
|
27
|
-
}
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* 检查语句是否是提前返回或抛出(仅包含这些语句)
|
|
32
|
-
*/
|
|
33
|
-
function isEarlyExit(node) {
|
|
34
|
-
if (!node)
|
|
35
|
-
return false;
|
|
36
|
-
// 直接是 return 或 throw
|
|
37
|
-
if (node.type === 'ReturnStatement' || node.type === 'ThrowStatement') {
|
|
38
|
-
return true;
|
|
39
|
-
}
|
|
40
|
-
// 是 block statement,检查其中的所有语句是否都是 return 或 throw
|
|
41
|
-
if (node.type === 'BlockStatement') {
|
|
42
|
-
return (node.body.length > 0 &&
|
|
43
|
-
node.body.every((stmt) => stmt.type === 'ReturnStatement' || stmt.type === 'ThrowStatement'));
|
|
44
|
-
}
|
|
45
|
-
return false;
|
|
46
|
-
}
|
|
47
|
-
return {
|
|
48
|
-
IfStatement(node) {
|
|
49
|
-
// 检查:条件是等于 & if 分支是提前返回 & 没有 else 分支
|
|
50
|
-
if (!node.alternate && isEqualityOperator(node.test) && isEarlyExit(node.consequent)) {
|
|
51
|
-
context.report({ node, messageId: 'noEarlyReturnOnEquality' });
|
|
52
|
-
}
|
|
53
|
-
},
|
|
54
|
-
};
|
|
55
|
-
},
|
|
56
|
-
};
|
|
57
|
-
exports.default = rule;
|