@blumintinc/eslint-plugin-blumint 0.1.21 → 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 +45 -3
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -54,12 +54,54 @@ function checkFunction(context, node) {
|
|
|
54
54
|
return;
|
|
55
55
|
}
|
|
56
56
|
if (ASTHelpers_1.ASTHelpers.returnsJSX(node.body) && ASTHelpers_1.ASTHelpers.hasParameters(node)) {
|
|
57
|
-
|
|
57
|
+
const results = [
|
|
58
58
|
isUnmemoizedArrowFunction,
|
|
59
59
|
isUnmemoizedFunctionComponent,
|
|
60
60
|
isUnmemoizedExportedFunctionComponent,
|
|
61
|
-
].
|
|
62
|
-
|
|
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
|
+
});
|
|
63
105
|
}
|
|
64
106
|
}
|
|
65
107
|
}
|