@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 CHANGED
@@ -18,7 +18,7 @@ const require_memo_1 = require("./rules/require-memo");
18
18
  module.exports = {
19
19
  meta: {
20
20
  name: '@blumintinc/eslint-plugin-blumint',
21
- version: '0.1.21',
21
+ version: '0.1.22',
22
22
  },
23
23
  parseOptions: {
24
24
  ecmaVersion: 2020,
@@ -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
- if ([
57
+ const results = [
58
58
  isUnmemoizedArrowFunction,
59
59
  isUnmemoizedFunctionComponent,
60
60
  isUnmemoizedExportedFunctionComponent,
61
- ].some((fn) => fn(parentNode, node))) {
62
- context.report({ node, messageId: 'requireMemo' });
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blumintinc/eslint-plugin-blumint",
3
- "version": "0.1.21",
3
+ "version": "0.1.22",
4
4
  "description": "Custom eslint rules for use at BluMint",
5
5
  "keywords": [
6
6
  "eslint",