@blumintinc/eslint-plugin-blumint 0.1.20 → 0.1.22
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 +66 -18
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -24,36 +24,84 @@ function isHigherOrderFunctionReturningJSX(node) {
|
|
|
24
24
|
}
|
|
25
25
|
return false;
|
|
26
26
|
}
|
|
27
|
+
const isUnmemoizedArrowFunction = (parentNode) => {
|
|
28
|
+
return (parentNode.type === 'VariableDeclarator' &&
|
|
29
|
+
parentNode.id.type === 'Identifier' &&
|
|
30
|
+
!isComponentExplicitlyUnmemoized(parentNode.id.name));
|
|
31
|
+
};
|
|
32
|
+
const isUnmemoizedFunctionComponent = (parentNode, node) => {
|
|
33
|
+
return (node.type === 'FunctionDeclaration' &&
|
|
34
|
+
parentNode.type === 'Program' &&
|
|
35
|
+
node.id &&
|
|
36
|
+
!isComponentExplicitlyUnmemoized(node.id.name));
|
|
37
|
+
};
|
|
38
|
+
const isUnmemoizedExportedFunctionComponent = (parentNode, node) => {
|
|
39
|
+
return (node.type === 'FunctionDeclaration' &&
|
|
40
|
+
parentNode.type === 'ExportNamedDeclaration' &&
|
|
41
|
+
node.id &&
|
|
42
|
+
!isComponentExplicitlyUnmemoized(node.id.name));
|
|
43
|
+
};
|
|
27
44
|
function checkFunction(context, node) {
|
|
28
45
|
const fileName = context.getFilename();
|
|
29
46
|
if (!fileName.endsWith('.tsx')) {
|
|
30
47
|
return;
|
|
31
48
|
}
|
|
32
49
|
if (isHigherOrderFunctionReturningJSX(node)) {
|
|
33
|
-
console.log('Found HOF');
|
|
34
50
|
return;
|
|
35
51
|
}
|
|
36
|
-
const
|
|
52
|
+
const parentNode = node.parent;
|
|
37
53
|
if (node.parent.type === 'CallExpression') {
|
|
38
54
|
return;
|
|
39
55
|
}
|
|
40
|
-
// while (currentNode.type === 'CallExpression') {
|
|
41
|
-
// if (isMemoCallExpression(currentNode) || true) {
|
|
42
|
-
// return;
|
|
43
|
-
// }
|
|
44
|
-
// currentNode = currentNode.parent;
|
|
45
|
-
// }
|
|
46
56
|
if (ASTHelpers_1.ASTHelpers.returnsJSX(node.body) && ASTHelpers_1.ASTHelpers.hasParameters(node)) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
+
const results = [
|
|
58
|
+
isUnmemoizedArrowFunction,
|
|
59
|
+
isUnmemoizedFunctionComponent,
|
|
60
|
+
isUnmemoizedExportedFunctionComponent,
|
|
61
|
+
].map((fn) => fn(parentNode, node));
|
|
62
|
+
if (results.some((result) => !!result)) {
|
|
63
|
+
context.report({
|
|
64
|
+
node,
|
|
65
|
+
messageId: 'requireMemo',
|
|
66
|
+
fix: results[2] || results[1]
|
|
67
|
+
? function fix(fixer) {
|
|
68
|
+
const sourceCode = context.getSourceCode();
|
|
69
|
+
let importFix = null;
|
|
70
|
+
// Search for React import statement
|
|
71
|
+
const importDeclarations = sourceCode.ast.body.filter((node) => node.type === 'ImportDeclaration');
|
|
72
|
+
const reactImport = importDeclarations.find((importDeclaration) => importDeclaration.source.value === 'react');
|
|
73
|
+
if (reactImport) {
|
|
74
|
+
// Check if memo is already imported
|
|
75
|
+
if (!reactImport.specifiers.some((specifier) => specifier.local.name === 'memo')) {
|
|
76
|
+
// Add memo to existing import statement
|
|
77
|
+
const lastSpecifier = reactImport.specifiers[reactImport.specifiers.length - 1];
|
|
78
|
+
importFix = fixer.insertTextAfter(lastSpecifier, ', memo');
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
// Add new import statement for memo
|
|
83
|
+
const importStatement = "import { memo } from 'react';\n";
|
|
84
|
+
importFix = fixer.insertTextBeforeRange([sourceCode.ast.range[0], sourceCode.ast.range[0]], importStatement);
|
|
85
|
+
}
|
|
86
|
+
const functionKeywordRange = [
|
|
87
|
+
node.range[0],
|
|
88
|
+
node.range[0] + 'function'.length,
|
|
89
|
+
];
|
|
90
|
+
const functionKeywordReplacement = `const ${node.id.name} = memo(`;
|
|
91
|
+
// Step 3: Rename function
|
|
92
|
+
const functionNameReplacement = `function ${node.id.name}Unmemoized`;
|
|
93
|
+
const fixes = [
|
|
94
|
+
fixer.replaceTextRange(functionKeywordRange, functionKeywordReplacement),
|
|
95
|
+
fixer.insertTextAfterRange([node.range[1], node.range[1]], ')'),
|
|
96
|
+
fixer.replaceTextRange([node.id.range[0] - 1, node.id.range[1]], functionNameReplacement),
|
|
97
|
+
];
|
|
98
|
+
if (importFix) {
|
|
99
|
+
fixes.push(importFix);
|
|
100
|
+
}
|
|
101
|
+
return fixes;
|
|
102
|
+
}
|
|
103
|
+
: undefined,
|
|
104
|
+
});
|
|
57
105
|
}
|
|
58
106
|
}
|
|
59
107
|
}
|