@blumintinc/eslint-plugin-blumint 0.1.16 → 0.1.18
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/lib/index.js +1 -1
- package/lib/rules/require-memo.js +25 -27
- package/lib/utils/ASTHelpers.js +15 -2
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -1,31 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.requireMemo = void 0;
|
|
4
|
+
const utils_1 = require("@typescript-eslint/utils");
|
|
4
5
|
const ASTHelpers_1 = require("../utils/ASTHelpers");
|
|
5
6
|
const isComponentExplicitlyUnmemoized = (componentName) => componentName.toLowerCase().includes('unmemoized');
|
|
6
|
-
function
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
if (node.callee.type === 'MemberExpression') {
|
|
10
|
-
const { callee: { object, property }, } = node;
|
|
11
|
-
if (object.type === 'Identifier' &&
|
|
12
|
-
property.type === 'Identifier' &&
|
|
13
|
-
object.name === 'React' &&
|
|
14
|
-
property.name === 'memo') {
|
|
15
|
-
return true;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
else if (node.callee.type === 'Identifier' && node.callee.name === 'memo') {
|
|
19
|
-
return true;
|
|
20
|
-
}
|
|
21
|
-
return false;
|
|
7
|
+
function isFunction(node) {
|
|
8
|
+
return (node.type === utils_1.AST_NODE_TYPES.ArrowFunctionExpression ||
|
|
9
|
+
node.type === utils_1.AST_NODE_TYPES.FunctionExpression);
|
|
22
10
|
}
|
|
23
11
|
function isHigherOrderFunctionReturningJSX(node) {
|
|
24
|
-
if (node
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
12
|
+
if (isFunction(node)) {
|
|
13
|
+
// Check if function takes another function as an argument
|
|
14
|
+
const hasFunctionParam = 'params' in node && node.params.some(isFunction);
|
|
15
|
+
if (node.body && node.body.type === 'BlockStatement') {
|
|
16
|
+
for (const statement of node.body.body) {
|
|
17
|
+
if (statement.type === 'ReturnStatement' && statement.argument) {
|
|
18
|
+
const returnsJSX = ASTHelpers_1.ASTHelpers.returnsJSX(statement.argument);
|
|
19
|
+
const returnsFunction = isFunction(statement.argument);
|
|
20
|
+
return (hasFunctionParam || returnsFunction) && returnsJSX;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
29
23
|
}
|
|
30
24
|
}
|
|
31
25
|
return false;
|
|
@@ -36,15 +30,19 @@ function checkFunction(context, node) {
|
|
|
36
30
|
return;
|
|
37
31
|
}
|
|
38
32
|
if (isHigherOrderFunctionReturningJSX(node)) {
|
|
33
|
+
console.log('Found HOF');
|
|
39
34
|
return;
|
|
40
35
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
currentNode = currentNode.parent;
|
|
36
|
+
const currentNode = node.parent;
|
|
37
|
+
if (node.parent.type === 'CallExpression') {
|
|
38
|
+
return;
|
|
47
39
|
}
|
|
40
|
+
// while (currentNode.type === 'CallExpression') {
|
|
41
|
+
// if (isMemoCallExpression(currentNode) || true) {
|
|
42
|
+
// return;
|
|
43
|
+
// }
|
|
44
|
+
// currentNode = currentNode.parent;
|
|
45
|
+
// }
|
|
48
46
|
if (ASTHelpers_1.ASTHelpers.returnsJSX(node.body) && ASTHelpers_1.ASTHelpers.hasParameters(node)) {
|
|
49
47
|
if (currentNode.type === 'VariableDeclarator' &&
|
|
50
48
|
currentNode.id.type === 'Identifier' &&
|
package/lib/utils/ASTHelpers.js
CHANGED
|
@@ -143,17 +143,30 @@ class ASTHelpers {
|
|
|
143
143
|
return false;
|
|
144
144
|
}
|
|
145
145
|
static returnsJSX(node) {
|
|
146
|
-
if (node.type === 'JSXElement') {
|
|
146
|
+
if (node.type === 'JSXElement' || node.type === 'JSXFragment') {
|
|
147
147
|
return true;
|
|
148
148
|
}
|
|
149
149
|
if (node.type === 'BlockStatement') {
|
|
150
150
|
for (const statement of node.body) {
|
|
151
151
|
if (statement.type === 'ReturnStatement' &&
|
|
152
|
-
statement.argument?.type === 'JSXElement'
|
|
152
|
+
(statement.argument?.type === 'JSXElement' ||
|
|
153
|
+
statement.argument?.type === 'JSXFragment')) {
|
|
153
154
|
return true;
|
|
154
155
|
}
|
|
156
|
+
// Handle conditional returns
|
|
157
|
+
if (statement.type === 'ReturnStatement' &&
|
|
158
|
+
statement.argument?.type === 'ConditionalExpression') {
|
|
159
|
+
const conditionalExpr = statement.argument;
|
|
160
|
+
if (ASTHelpers.returnsJSX(conditionalExpr.consequent) ||
|
|
161
|
+
ASTHelpers.returnsJSX(conditionalExpr.alternate)) {
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
155
165
|
}
|
|
156
166
|
}
|
|
167
|
+
if (node.type === 'ConditionalExpression') {
|
|
168
|
+
return (this.returnsJSX(node.consequent) || this.returnsJSX(node.alternate));
|
|
169
|
+
}
|
|
157
170
|
return false;
|
|
158
171
|
}
|
|
159
172
|
static hasParameters(node) {
|