@lsby/eslint-plugin 0.0.23 → 0.0.26

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