Skip to content

Commit

Permalink
refactor fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyvassallo committed Oct 18, 2021
1 parent b47445e commit 50c0174
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 63 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@types/jest": "^26.0.22",
"@types/lodash": "^4.14.175",
"@typescript-eslint/parser": "^4.19.0",
"@typescript-eslint/types": "^4.19.0",
"@typescript-eslint/types": "^5.0.0",
"eslint": "^7.23.0",
"gitpkg": "^1.0.0-beta.2",
"jest": "^25.0.0",
Expand All @@ -41,5 +41,8 @@
},
"files": [
"dist"
]
],
"dependencies": {
"typescript-eslint": "^0.0.1-alpha.0"
}
}
78 changes: 18 additions & 60 deletions require-usememo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Rule } from "eslint";
import * as ESTree from "estree";
import { TSESTree } from "@typescript-eslint/types";
import { Node } from "@typescript-eslint/types/dist/ast-spec"

import {
getExpressionMemoStatus,
isComplexComponent,
Expand Down Expand Up @@ -70,8 +72,20 @@ const rule: Rule.RuleModule = {
context.report({ node, messageId: messageId as string });
}

const fix = (fixer: Rule.RuleFixer, implicitReturn: boolean = true): Rule.Fix | null => {
const sourceCode = context.getSourceCode();
const references = context.getScope()?.references
const filteredRefs = references?.filter((reference) => reference?.writeExpr)
// @ts-ignore
const definition = filteredRefs?.[0]?.writeExpr?.parent
if (!definition) return null
const [name, value] = sourceCode.getText(definition).split('=')
const fixedCode = `${name.trim()} = useMemo(() => ${implicitReturn ? value.trim() : `(${value.trim()})`}, [])`
return fixer.replaceText(definition, fixedCode)
}

return {
JSXAttribute: (node: ESTree.Node & Rule.NodeParentExtension) => {
JSXAttribute: (node: Rule.Node & Rule.NodeParentExtension) => {
const { parent, value } = (node as unknown) as TSESTree.JSXAttribute &
Rule.NodeParentExtension;
if (value === null) return;
Expand All @@ -81,69 +95,13 @@ const rule: Rule.RuleModule = {
if (expression?.type !== "JSXEmptyExpression") {
switch (getExpressionMemoStatus(context, expression)) {
case MemoStatus.UnmemoizedObject:
// @ts-ignore
context.report({ node, messageId: "object-usememo-props", fix: (fixer): Rule.Fix => {
const sourceCode = context.getSourceCode();
// @ts-ignore
const definition = context.getScope().references.filter((reference) => reference.writeExpr ? reference.writeExpr.properties : null)?.[0]?.writeExpr.parent
// console.warn(definition)
const [name, value] = sourceCode.getText(definition).split('=')
let fixedCode = ''
if(name && value) {
fixedCode = `${name.trim()} = useMemo(() => (${value.trim()}), [])`
}

// return fixer.replaceText(scope, fixedCode);
if(definition) {
return fixer.replaceText(definition, fixedCode)
} else {
console.warn(context.getScope().references)
}
} });
context.report({ node, messageId: "object-usememo-props", fix: (fixer) => fix(fixer, false) });
break;
case MemoStatus.UnmemoizedArray:
// @ts-ignore
context.report({ node, messageId: "array-usememo-props", fix: (fixer): Rule.Fix => {
const sourceCode = context.getSourceCode();
// @ts-ignore
const definition = context.getScope().references.filter((reference) => reference.writeExpr ? reference.writeExpr : null)?.[0]?.writeExpr.parent
// console.warn(definition)
const [name, value] = sourceCode.getText(definition).split('=')
let fixedCode = ''
if(name && value) {
fixedCode = `${name.trim()} = useMemo(() => ${value.trim()}, [])`
}

// return fixer.replaceText(scope, fixedCode);
if(definition) {
return fixer.replaceText(definition, fixedCode)
} else {
// console.warn(context.getScope().references)
}
} });
context.report({ node, messageId: "array-usememo-props", fix });
break;
case MemoStatus.UnmemoizedNew:
// @ts-ignore
context.report({ node, messageId: "instance-usememo-props", fix: (fixer): Rule.Fix => {
const sourceCode = context.getSourceCode();
// @ts-ignore
const definition = context.getScope().references.filter((reference) => reference.writeExpr ? reference.writeExpr : null)?.[0]?.writeExpr.parent
// console.warn(definition)
const [name, value] = sourceCode.getText(definition).split('=')
let fixedCode = ''
if(name && value) {
fixedCode = `${name.trim()} = useMemo(() => ${value.trim()}, [])`
}

// return fixer.replaceText(scope, fixedCode);
if(definition) {
return fixer.replaceText(definition, fixedCode)
} else {
// @ts-ignore
// console.warn(context.getScope().references.filter((reference) => reference.writeExpr ? reference.writeExpr : null)?.[0]?.writeExpr.parent)
// console.warn(context.getScope().references)
}
} });
context.report({ node, messageId: "instance-usememo-props", fix });
break;
case MemoStatus.UnmemoizedFunction:
report(node, "function-usecallback-props");
Expand Down
12 changes: 11 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -737,11 +737,16 @@
"@typescript-eslint/types" "4.19.0"
"@typescript-eslint/visitor-keys" "4.19.0"

"@typescript-eslint/types@4.19.0", "@typescript-eslint/types@^4.19.0":
"@typescript-eslint/types@4.19.0":
version "4.19.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.19.0.tgz#5181d5d2afd02e5b8f149ebb37ffc8bd7b07a568"
integrity sha512-A4iAlexVvd4IBsSTNxdvdepW0D4uR/fwxDrKUa+iEY9UWvGREu2ZyB8ylTENM1SH8F7bVC9ac9+si3LWNxcBuA==

"@typescript-eslint/types@^5.0.0":
version "5.0.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.0.0.tgz#25d93f6d269b2d25fdc51a0407eb81ccba60eb0f"
integrity sha512-dU/pKBUpehdEqYuvkojmlv0FtHuZnLXFBn16zsDmlFF3LXkOpkAQ2vrKc3BidIIve9EMH2zfTlxqw9XM0fFN5w==

"@typescript-eslint/typescript-estree@4.19.0":
version "4.19.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.19.0.tgz#8a709ffa400284ab72df33376df085e2e2f61147"
Expand Down Expand Up @@ -4409,6 +4414,11 @@ typedarray-to-buffer@^3.1.5:
dependencies:
is-typedarray "^1.0.0"

typescript-eslint@^0.0.1-alpha.0:
version "0.0.1-alpha.0"
resolved "https://registry.yarnpkg.com/typescript-eslint/-/typescript-eslint-0.0.1-alpha.0.tgz#285d68a4e96588295cd436278801bcb6a6b916c1"
integrity sha512-1hNKM37dAWML/2ltRXupOq2uqcdRQyDFphl+341NTPXFLLLiDhErXx8VtaSLh3xP7SyHZdcCgpt9boYYVb3fQg==

typescript@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.2.3.tgz#39062d8019912d43726298f09493d598048c1ce3"
Expand Down

0 comments on commit 50c0174

Please sign in to comment.