eslint-plugin-better-codes 1.0.2 → 1.0.4

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
@@ -55,7 +55,12 @@ module.exports = {
55
55
  - 箭头函数
56
56
  - 方法函数
57
57
 
58
- 除去React组件函数
58
+ 不校验的情况:
59
+ - React组件函数
60
+ - 函数体只有一行代码的函数
61
+ - 用于map/forEach中返回元素/组件用于渲染的箭头函数
62
+
63
+ <!-- TODO:待解决的问题:针对返回结果为true/false的函数,使用try...catch进入catch分支之后改返回false还是true还是不返回(undefined),因为按照之前的逻辑报错之后后续的代码不应再执行的,但是现在会直接继续走false/true的逻辑,造成逻辑错误 -->
59
64
 
60
65
  ## 许可证
61
66
 
@@ -136,7 +136,53 @@ function isReactComponentFunction(node) {
136
136
  }
137
137
  }
138
138
 
139
- export default {
139
+ // 判断函数体是否只有一行
140
+ function isOnlyOneLine(body) {
141
+ try {
142
+ if (!body || body.type !== "BlockStatement") return false;
143
+ const stmts = body.body || [];
144
+ return stmts.length === 1 && stmts[0].type === "ExpressionStatement";
145
+ } catch (err) {
146
+ return false;
147
+ }
148
+ }
149
+
150
+ // 判断函数是否作为 array methods 的回调(如 map/forEach)直接返回用于渲染的元素/组件
151
+ function isArrayMethodCallback(node) {
152
+ try {
153
+ if (!node || !node.parent) return false;
154
+ // 父节点应为 CallExpression,且当前函数是该调用的一个参数
155
+ const p = node.parent;
156
+ if (p.type !== "CallExpression") return false;
157
+ const args = p.arguments || [];
158
+ if (!args.includes(node)) return false;
159
+ const callee = p.callee;
160
+ if (!callee) return false;
161
+ // 支持形如 data.map(...) 或 data?.map(...)
162
+ if (callee.type === "MemberExpression") {
163
+ const prop = callee.property;
164
+ const name = prop && (prop.name || (prop.value && String(prop.value)));
165
+ // 判断返回语句
166
+ const returnNode = node.body?.body?.at(-1);
167
+ if (
168
+ name &&
169
+ /^(map|forEach)$/i.test(name) &&
170
+ returnNode &&
171
+ returnNode.type === "ReturnStatement" &&
172
+ JSON.stringify(returnNode.argument?.raw || "").startsWith("<") &&
173
+ JSON.stringify(returnNode.argument?.raw || "").endsWith(">")
174
+ ) {
175
+ return true;
176
+ }
177
+ }
178
+
179
+ return false;
180
+ } catch (err) {
181
+ console.log(err);
182
+ }
183
+ }
184
+
185
+ module.exports = {
140
186
  meta: {
141
187
  type: "suggestion",
142
188
  docs: {
@@ -154,7 +200,12 @@ export default {
154
200
  (node.type === "ArrowFunctionExpression" &&
155
201
  node.body &&
156
202
  node.body.type !== "BlockStatement") ||
157
- isReactComponentFunction(node)
203
+ // 作为 React 组件/组件函数跳过
204
+ isReactComponentFunction(node) ||
205
+ // 只有一行表达式的函数跳过
206
+ isOnlyOneLine(node.body) ||
207
+ // 作为 array 方法回调(如 map/forEach)用于渲染时跳过
208
+ isArrayMethodCallback(node)
158
209
  ) {
159
210
  return;
160
211
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-better-codes",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "npx eslint ./test.js"