eslint-plugin-better-codes 1.1.8 → 1.1.10

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.
@@ -185,6 +185,21 @@ function isArrayMethodCallback(node) {
185
185
  // 判断任一父级函数是否包含 try...catch
186
186
  function ancestorHasTry(node) {
187
187
  try {
188
+ // Only consider a TryStatement as handling errors for this node
189
+ // if the TryStatement actually encloses the node in the AST path.
190
+ function isEnclosedByTry(n, ancestor) {
191
+ try {
192
+ let cur = n.parent;
193
+ while (cur && cur !== ancestor) {
194
+ if (cur.type === "TryStatement") return true;
195
+ cur = cur.parent;
196
+ }
197
+ } catch (err) {
198
+ // ignore
199
+ }
200
+ return false;
201
+ }
202
+
188
203
  let p = node.parent;
189
204
  while (p) {
190
205
  if (
@@ -193,9 +208,8 @@ function ancestorHasTry(node) {
193
208
  p.type === "ArrowFunctionExpression" ||
194
209
  p.type === "MethodDefinition"
195
210
  ) {
196
- // 对于函数节点,检查其 body 是否包含 TryStatement
197
- const body = p.body || (p.value && p.value.body);
198
- if (body && containsTryStatement(body)) return true;
211
+ // 对于函数节点,只认定在从当前节点到该父函数路径上存在 TryStatement 的情况
212
+ if (isEnclosedByTry(node, p)) return true;
199
213
  }
200
214
  p = p.parent;
201
215
  }
@@ -205,7 +219,7 @@ function ancestorHasTry(node) {
205
219
  return false;
206
220
  }
207
221
 
208
- module.exports = {
222
+ module.exports = {
209
223
  meta: {
210
224
  type: "suggestion",
211
225
  docs: {
@@ -91,78 +91,82 @@ module.exports = {
91
91
  Program() {
92
92
  const comments = sourceCode.getAllComments();
93
93
  comments.forEach(comment => {
94
- if (comment.loc) {
95
- // 单行注释
96
- if (comment.type === "Line") {
97
- if (isFunctionComment(comment.value)) {
98
- return;
99
- }
100
- singleLineCount++;
94
+ try {
95
+ if (comment.loc) {
96
+ // 单行注释
97
+ if (comment.type === "Line") {
98
+ if (isFunctionComment(comment.value)) {
99
+ return;
100
+ }
101
+ singleLineCount++;
101
102
 
102
- const lines = comment.value.split("\n");
103
- // 过滤空行,只计算有实际内容的行
104
- const codeLines = lines.filter(line => {
105
- const trimmed = line.trim();
106
- return trimmed.length > 0;
107
- });
108
- const isIgnored = codeLines[0].endsWith("ignore-eslint"); // 因默认添加了ignore-eslint,进行忽略校验
109
- if (isIgnored && ignoredCount < minLines) {
110
- ignoredCount++;
111
- singleLineCount--;
112
- }
113
- // 优先检查忽略次数:当忽略累计次数达到阈值时,优先提示并禁止继续忽略
114
- if (ignoredCount >= minLines) {
115
- context.report({
116
- node: comment,
117
- message: `总忽略次数大于等于${minLines}次,请删除或保留为有效代码`,
118
- data: {
119
- lines: ignoredCount,
120
- },
121
- });
122
- return;
123
- }
124
- // 其次检查单行注释总数
125
- if (singleLineCount >= minLines) {
126
- context.report({
127
- node: comment,
128
- message: `单行注释大于等于 ${minLines} 行代码,请删除或保留为有效代码`,
129
- data: {
130
- lines: singleLineCount,
131
- },
103
+ const lines = comment.value.split("\n");
104
+ // 过滤空行,只计算有实际内容的行
105
+ const codeLines = lines.filter(line => {
106
+ const trimmed = line.trim();
107
+ return trimmed.length > 0;
132
108
  });
133
- return;
134
- }
135
- // 如果注释块包含多行代码逻辑
136
- if (codeLines.length >= minLines) {
137
- context.report({
138
- node: comment,
139
- message: `注释掉了 ${codeLines.length} 行代码,请删除或保留为有效代码`,
140
- data: {
141
- lines: codeLines.length,
142
- },
143
- });
144
- }
145
- }
146
- // 多行注释
147
- if (comment.type === "Block") {
148
- if (isFunctionComment(comment.value, true)) {
149
- return;
109
+ const isIgnored = codeLines[0]?.endsWith("ignore-eslint"); // 因默认添加了ignore-eslint,进行忽略校验
110
+ if (isIgnored && ignoredCount < minLines) {
111
+ ignoredCount++;
112
+ singleLineCount--;
113
+ }
114
+ // 优先检查忽略次数:当忽略累计次数达到阈值时,优先提示并禁止继续忽略
115
+ if (ignoredCount >= minLines) {
116
+ context.report({
117
+ node: comment,
118
+ message: `总忽略次数大于等于${minLines}次,请删除或保留为有效代码`,
119
+ data: {
120
+ lines: ignoredCount,
121
+ },
122
+ });
123
+ return;
124
+ }
125
+ // 其次检查单行注释总数
126
+ if (singleLineCount >= minLines) {
127
+ context.report({
128
+ node: comment,
129
+ message: `单行注释大于等于 ${minLines} 行代码,请删除或保留为有效代码`,
130
+ data: {
131
+ lines: singleLineCount,
132
+ },
133
+ });
134
+ return;
135
+ }
136
+ // 如果注释块包含多行代码逻辑
137
+ if (codeLines.length >= minLines) {
138
+ context.report({
139
+ node: comment,
140
+ message: `注释掉了 ${codeLines.length} 行代码,请删除或保留为有效代码`,
141
+ data: {
142
+ lines: codeLines.length,
143
+ },
144
+ });
145
+ }
150
146
  }
151
- const lines = comment.value.split("\n");
152
- const codeLines = lines.filter(line => {
153
- const trimmed = line.trim();
154
- return trimmed !== "*" && trimmed.length > 0 && trimmed; // 过滤掉空行和只包含*的行
155
- });
156
- if (codeLines.length >= minLines) {
157
- context.report({
158
- node: comment,
159
- message: `注释掉了 ${codeLines.length} 行代码,请删除或保留为有效代码`,
160
- data: {
161
- lines: codeLines.length,
162
- },
147
+ // 多行注释
148
+ if (comment.type === "Block") {
149
+ if (isFunctionComment(comment.value, true)) {
150
+ return;
151
+ }
152
+ const lines = comment.value.split("\n");
153
+ const codeLines = lines.filter(line => {
154
+ const trimmed = line.trim();
155
+ return trimmed !== "*" && trimmed.length > 0 && trimmed; // 过滤掉空行和只包含*的行
163
156
  });
157
+ if (codeLines.length >= minLines) {
158
+ context.report({
159
+ node: comment,
160
+ message: `注释掉了 ${codeLines.length} 行代码,请删除或保留为有效代码`,
161
+ data: {
162
+ lines: codeLines.length,
163
+ },
164
+ });
165
+ }
164
166
  }
165
167
  }
168
+ } catch (err) {
169
+ console.error(err);
166
170
  }
167
171
  });
168
172
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-better-codes",
3
- "version": "1.1.8",
3
+ "version": "1.1.10",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "npx eslint ./test.js"