Skip to content

Commit

Permalink
fix: name generation conflict resolution
Browse files Browse the repository at this point in the history
would create variables with the same name "renameMe"
  • Loading branch information
Arthur Geron committed Oct 31, 2023
1 parent 0305353 commit 6f363cf
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 7 deletions.
52 changes: 52 additions & 0 deletions __tests__/require-usememo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,11 +481,63 @@ describe('Rule - Require-usememo', () => {
const Component = () => {
const userData = undefined;
const _userData = undefined;
const __userData = useMemo(() => ({}), []);
return <Child userData={__userData} />;
}`,
errors: [{ messageId: "object-usememo-props" }],
},
{
code: `
const Component = () => {
const userData = undefined;
const _userData = undefined;
const __userData = undefined;
const ___userData = undefined;
const ____userData = undefined;
const _____userData = undefined;
return <Child userData={{}} />;
}`,
output: `import { useMemo } from 'react';
const Component = () => {
const userData = undefined;
const _userData = undefined;
const __userData = undefined;
const ___userData = undefined;
const ____userData = undefined;
const _____userData = undefined;
const renameMe = useMemo(() => ({}), []);
return <Child userData={renameMe} />;
}`,
errors: [{ messageId: "object-usememo-props" }],
},
{
code: `
const Component = () => {
const userData = undefined;
const _userData = undefined;
const __userData = undefined;
const ___userData = undefined;
const ____userData = undefined;
const _____userData = undefined;
const renameMe = undefined;
return <Child userData={{}} />;
}`,
output: `import { useMemo } from 'react';
const Component = () => {
const userData = undefined;
const _userData = undefined;
const __userData = undefined;
const ___userData = undefined;
const ____userData = undefined;
const _____userData = undefined;
const renameMe = undefined;
const renameMe_99c32a94 = useMemo(() => ({}), []);
return <Child userData={renameMe_99c32a94} />;
}`,
errors: [{ messageId: "object-usememo-props" }],
},
{
code: `
const Component = () => {
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@rollup/plugin-typescript": "8.3.3",
"@types/eslint": "8.4.5",
"@types/jest": "28.1.5",
"@types/uuid": "9.0.6",
"@typescript-eslint/parser": "5.30.6",
"@typescript-eslint/types": "5.30.6",
"eslint": "8.19.0",
Expand All @@ -37,5 +38,8 @@
},
"files": [
"dist"
]
],
"dependencies": {
"uuid": "9.0.1"
}
}
1 change: 1 addition & 0 deletions src/require-usememo/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MemoStatus } from 'src/types';
import type { ExpressionData, MemoErrorHookDictionary } from './types';

export const nameGeneratorUUID = 'b32a4d70-4f64-11eb-89d5-33e6ce8a6c99';
export const jsxEmptyExpressionClassData: ExpressionData = {
[MemoStatus.UnmemoizedObject.toString()]: "object-class-memo-props",
[MemoStatus.UnmemoizedArray.toString()]: "array-class-memo-props",
Expand Down
15 changes: 9 additions & 6 deletions src/require-usememo/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import type * as ESTree from "estree";
import { MessagesRequireUseMemo } from '../constants';
import type { ESNode, ExpressionData, ReactImportInformation } from "./types";
import { MemoStatusToReport } from "src/types";
import { messageIdToHookDict } from "./constants";
import { messageIdToHookDict, nameGeneratorUUID } from "./constants";
import { getVariableInScope } from "src/common";

import { v5 as uuidV5 } from 'uuid';

export function shouldIgnoreNode(node: ESNode, ignoredNames: Record<string,boolean | undefined> ) {
return !!ignoredNames[(node as TSESTree.Node as TSESTree.Identifier)?.name]
Expand Down Expand Up @@ -116,15 +116,18 @@ function fixFunction(node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpre
return fixedCode;
}

function getSafeVariableName(context: Rule.RuleContext, name: string) {
function getSafeVariableName(context: Rule.RuleContext, name:string, attempts = 0): string {
const tempVarPlaceholder = 'renameMe';

if (!getVariableInScope(context, name)) {
return name;
}
if (!getVariableInScope(context, `_${name}`)) {
return `_${name}`;
if (attempts >= 5) {
const nameExtensionIfExists = getVariableInScope(context, tempVarPlaceholder) ? uuidV5(name, nameGeneratorUUID).split('-')[0] : '';
return `${tempVarPlaceholder}${nameExtensionIfExists ? `_${nameExtensionIfExists}` : ''}`;
}
return tempVarPlaceholder;
return getSafeVariableName(context, `_${name}`, ++attempts);

}

// Eslint Auto-fix logic, functional components/hooks only
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,11 @@
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.2.tgz#01284dde9ef4e6d8cef6422798d9a3ad18a66f8b"
integrity sha512-g7CK9nHdwjK2n0ymT2CW698FuWJRIx+RP6embAzZ2Qi8/ilIrA1Imt2LVSeHUzKvpoi7BhmmQcXz95eS0f2JXw==

"@types/[email protected]":
version "9.0.6"
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-9.0.6.tgz#c91ae743d8344a54b2b0c691195f5ff5265f6dfb"
integrity sha512-BT2Krtx4xaO6iwzwMFUYvWBWkV2pr37zD68Vmp1CDV196MzczBRxuEpD6Pr395HAgebC/co7hOphs53r8V7jew==

"@types/yargs-parser@*":
version "21.0.2"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-21.0.2.tgz#7bd04c5da378496ef1695a1008bf8f71847a8b8b"
Expand Down Expand Up @@ -3533,6 +3538,11 @@ util-deprecate@^1.0.1:
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
integrity sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==

[email protected]:
version "9.0.1"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-9.0.1.tgz#e188d4c8853cc722220392c424cd637f32293f30"
integrity sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==

v8-compile-cache-lib@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz#6336e8d71965cb3d35a1bbb7868445a7c05264bf"
Expand Down

0 comments on commit 6f363cf

Please sign in to comment.