eslint-plugin-better-codes 1.1.6 → 1.1.9

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/README.md CHANGED
@@ -59,8 +59,9 @@ module.exports = {
59
59
  不校验的情况:
60
60
 
61
61
  - React组件函数
62
- - 函数体只有一行代码的函数
62
+ - 函数体只有一行代码(一条执行语句)的函数
63
63
  - 用于map/forEach中返回元素/组件用于渲染的箭头函数
64
+ - 嵌套函数内的子函数,,如果父函数已经包含try-catch,则子函数不进行校验
64
65
 
65
66
  <!-- TODO:待解决的问题:针对返回结果为true/false的函数,使用try...catch进入catch分支之后改返回false还是true还是不返回(undefined),因为按照之前的逻辑报错之后后续的代码不应再执行的,但是现在会直接继续走false/true的逻辑,造成逻辑错误 -->
66
67
 
@@ -182,6 +182,29 @@ function isArrayMethodCallback(node) {
182
182
  }
183
183
  }
184
184
 
185
+ // 判断任一父级函数是否包含 try...catch
186
+ function ancestorHasTry(node) {
187
+ try {
188
+ let p = node.parent;
189
+ while (p) {
190
+ if (
191
+ p.type === "FunctionDeclaration" ||
192
+ p.type === "FunctionExpression" ||
193
+ p.type === "ArrowFunctionExpression" ||
194
+ p.type === "MethodDefinition"
195
+ ) {
196
+ // 对于函数节点,检查其 body 是否包含 TryStatement
197
+ const body = p.body || (p.value && p.value.body);
198
+ if (body && containsTryStatement(body)) return true;
199
+ }
200
+ p = p.parent;
201
+ }
202
+ } catch (err) {
203
+ // ignore
204
+ }
205
+ return false;
206
+ }
207
+
185
208
  module.exports = {
186
209
  meta: {
187
210
  type: "suggestion",
@@ -214,6 +237,8 @@ module.exports = {
214
237
  const stmts = body.body || [];
215
238
  // 如果函数体为空或只有注释/空,则跳过
216
239
  if (stmts.length === 0) return;
240
+ // 如果任一父级函数已包含 try/catch,跳过(避免重复校验内部回调)
241
+ if (ancestorHasTry(node)) return;
217
242
  // 如果函数体中包含 TryStatement,说明已处理,跳过
218
243
  if (containsTryStatement(body)) return;
219
244
  context.report({
@@ -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.6",
3
+ "version": "1.1.9",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "npx eslint ./test.js"