@lsby/eslint-plugin 0.0.22 → 0.0.25

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.
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  // 禁止对于等于/不等于条件的 else 处理多种状态
3
- // 因为对于这些条件,else 表示"除给定状态外的所有可能"
3
+ // 因为对于这些条件, else 表示"除给定状态外的所有可能"
4
4
  // 对于状态小于等于两个的条件, else 只会兜底一种情况, 这是正确的
5
5
  // 但如果状态继续增加, else 兜底必然匹配一个以上种状态
6
6
  // 此时 else 部分会默默吃掉新增状态却无任何提示, 这很容易造成意外的状态逻辑遗漏
7
- // 这种情况下应当使用提早返回 或 switch + 穷尽检查
7
+ // 这种情况下应当使用提早返回(early return)或 switch 穷尽
8
8
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
9
9
  if (k2 === undefined) k2 = k;
10
10
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -44,9 +44,11 @@ const rule = {
44
44
  meta: {
45
45
  type: 'problem',
46
46
  docs: {
47
- description: '禁止对等于/不等于条件使用 else。当新增状态时,else 会隐匿地处理新状态,容易造成状态逻辑遗漏。应使用提早返回或 switch + 穷尽检查替代。',
47
+ description: '禁止在非二值状态的等于/不等于条件中使用 else. 当状态扩张时, else 会隐匿地处理新状态, 容易造成状态逻辑遗漏. ',
48
+ },
49
+ messages: {
50
+ noElse: '禁止在非二值状态的等于/不等于条件中使用 else. 若状态在语义上是二值的(例如"是否为空"), 请先通过辅助变量表示为布尔值;若为多状态逻辑, 请使用提早返回(early return)或 switch 穷尽. ',
48
51
  },
49
- messages: { noElse: '禁止在等于/不等于条件中使用 else,请使用提早返回或 switch 语句进行穷尽检查。' },
50
52
  schema: [],
51
53
  },
52
54
  defaultOptions: [],
@@ -57,7 +59,7 @@ const rule = {
57
59
  * 计算类型中的字面量值个数
58
60
  */
59
61
  function countLiteralVariants(type) {
60
- // 先检查联合类型,因为它可能包含其他标志
62
+ // 先检查联合类型, 因为它可能包含其他标志
61
63
  if ((type.flags & ts.TypeFlags.Union) !== 0) {
62
64
  const union = type;
63
65
  // Count distinct types in union
@@ -91,7 +93,7 @@ const rule = {
91
93
  return Infinity;
92
94
  }
93
95
  /**
94
- * 检查条件中是否包含等于或不等于操作符,以及被比较的值
96
+ * 检查条件中是否包含等于或不等于操作符, 以及被比较的值
95
97
  * 返回 [operand1, operand2] 或 null
96
98
  */
97
99
  function getEqualityOperands(node) {
@@ -112,11 +114,11 @@ const rule = {
112
114
  }
113
115
  /**
114
116
  * 检查是否应该允许 else(状态数 ≤ 2)
115
- * 返回 true 表示允许 elsefalse 表示不允许
117
+ * 返回 true 表示允许 else, false 表示不允许
116
118
  */
117
119
  function shouldAllowElse(leftExpr, rightExpr) {
118
120
  if (!typeChecker || !parserServices?.esTreeNodeToTSNodeMap) {
119
- // 无法获取类型信息时,按原始规则:不允许 else
121
+ // 无法获取类型信息时, 按原始规则:不允许 else
120
122
  return false;
121
123
  }
122
124
  try {
@@ -124,13 +126,13 @@ const rule = {
124
126
  const leftNode = parserServices.esTreeNodeToTSNodeMap.get(leftExpr);
125
127
  const rightNode = parserServices.esTreeNodeToTSNodeMap.get(rightExpr);
126
128
  if (!leftNode || !rightNode) {
127
- // 无法映射节点时,按原始规则:不允许 else
129
+ // 无法映射节点时, 按原始规则:不允许 else
128
130
  return false;
129
131
  }
130
132
  // 获取两个操作数的类型
131
133
  let leftType = typeChecker.getTypeAtLocation(leftNode);
132
134
  let rightType = typeChecker.getTypeAtLocation(rightNode);
133
- // 对于标识符,尝试获取声明的类型
135
+ // 对于标识符, 尝试获取声明的类型
134
136
  if (leftExpr.type === 'Identifier') {
135
137
  const leftSymbol = typeChecker.getSymbolAtLocation(leftNode);
136
138
  if (leftSymbol && leftSymbol.valueDeclaration) {
@@ -146,19 +148,19 @@ const rule = {
146
148
  // 计算每边的字面量值个数
147
149
  const leftCount = countLiteralVariants(leftType);
148
150
  const rightCount = countLiteralVariants(rightType);
149
- // 只有当两边都是有限的且都 ≤ 2 个状态时,才允许 else
151
+ // 只有当两边都是有限的且都 ≤ 2 个状态时, 才允许 else
150
152
  const bothFinite = isFinite(leftCount) && isFinite(rightCount);
151
153
  const allow = bothFinite && leftCount <= 2 && rightCount <= 2;
152
154
  return allow;
153
155
  }
154
156
  catch {
155
- // 如果出错,按原始规则:不允许 else
157
+ // 如果出错, 按原始规则:不允许 else
156
158
  return false;
157
159
  }
158
160
  }
159
161
  return {
160
162
  IfStatement(node) {
161
- // 仅在 if 条件是等于或不等于的情况下,禁止 else
163
+ // 仅在 if 条件是等于或不等于的情况下, 禁止 else
162
164
  if (node.alternate !== null) {
163
165
  const operands = getEqualityOperands(node.test);
164
166
  if (operands) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lsby/eslint-plugin",
3
- "version": "0.0.22",
3
+ "version": "0.0.25",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [