Skip to content

Commit 6f363cf

Browse files
author
Arthur Geron
committed
fix: name generation conflict resolution
would create variables with the same name "renameMe"
1 parent 0305353 commit 6f363cf

File tree

5 files changed

+77
-7
lines changed

5 files changed

+77
-7
lines changed

__tests__/require-usememo.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,11 +481,63 @@ describe('Rule - Require-usememo', () => {
481481
const Component = () => {
482482
const userData = undefined;
483483
const _userData = undefined;
484+
const __userData = useMemo(() => ({}), []);
485+
return <Child userData={__userData} />;
486+
}`,
487+
errors: [{ messageId: "object-usememo-props" }],
488+
},
489+
{
490+
code: `
491+
const Component = () => {
492+
const userData = undefined;
493+
const _userData = undefined;
494+
const __userData = undefined;
495+
const ___userData = undefined;
496+
const ____userData = undefined;
497+
const _____userData = undefined;
498+
return <Child userData={{}} />;
499+
}`,
500+
output: `import { useMemo } from 'react';
501+
502+
const Component = () => {
503+
const userData = undefined;
504+
const _userData = undefined;
505+
const __userData = undefined;
506+
const ___userData = undefined;
507+
const ____userData = undefined;
508+
const _____userData = undefined;
484509
const renameMe = useMemo(() => ({}), []);
485510
return <Child userData={renameMe} />;
486511
}`,
487512
errors: [{ messageId: "object-usememo-props" }],
488513
},
514+
{
515+
code: `
516+
const Component = () => {
517+
const userData = undefined;
518+
const _userData = undefined;
519+
const __userData = undefined;
520+
const ___userData = undefined;
521+
const ____userData = undefined;
522+
const _____userData = undefined;
523+
const renameMe = undefined;
524+
return <Child userData={{}} />;
525+
}`,
526+
output: `import { useMemo } from 'react';
527+
528+
const Component = () => {
529+
const userData = undefined;
530+
const _userData = undefined;
531+
const __userData = undefined;
532+
const ___userData = undefined;
533+
const ____userData = undefined;
534+
const _____userData = undefined;
535+
const renameMe = undefined;
536+
const renameMe_99c32a94 = useMemo(() => ({}), []);
537+
return <Child userData={renameMe_99c32a94} />;
538+
}`,
539+
errors: [{ messageId: "object-usememo-props" }],
540+
},
489541
{
490542
code: `
491543
const Component = () => {

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"@rollup/plugin-typescript": "8.3.3",
1818
"@types/eslint": "8.4.5",
1919
"@types/jest": "28.1.5",
20+
"@types/uuid": "9.0.6",
2021
"@typescript-eslint/parser": "5.30.6",
2122
"@typescript-eslint/types": "5.30.6",
2223
"eslint": "8.19.0",
@@ -37,5 +38,8 @@
3738
},
3839
"files": [
3940
"dist"
40-
]
41+
],
42+
"dependencies": {
43+
"uuid": "9.0.1"
44+
}
4145
}

src/require-usememo/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { MemoStatus } from 'src/types';
22
import type { ExpressionData, MemoErrorHookDictionary } from './types';
33

4+
export const nameGeneratorUUID = 'b32a4d70-4f64-11eb-89d5-33e6ce8a6c99';
45
export const jsxEmptyExpressionClassData: ExpressionData = {
56
[MemoStatus.UnmemoizedObject.toString()]: "object-class-memo-props",
67
[MemoStatus.UnmemoizedArray.toString()]: "array-class-memo-props",

src/require-usememo/utils.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import type * as ESTree from "estree";
44
import { MessagesRequireUseMemo } from '../constants';
55
import type { ESNode, ExpressionData, ReactImportInformation } from "./types";
66
import { MemoStatusToReport } from "src/types";
7-
import { messageIdToHookDict } from "./constants";
7+
import { messageIdToHookDict, nameGeneratorUUID } from "./constants";
88
import { getVariableInScope } from "src/common";
9-
9+
import { v5 as uuidV5 } from 'uuid';
1010

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

119-
function getSafeVariableName(context: Rule.RuleContext, name: string) {
119+
function getSafeVariableName(context: Rule.RuleContext, name:string, attempts = 0): string {
120120
const tempVarPlaceholder = 'renameMe';
121+
121122
if (!getVariableInScope(context, name)) {
122123
return name;
123124
}
124-
if (!getVariableInScope(context, `_${name}`)) {
125-
return `_${name}`;
125+
if (attempts >= 5) {
126+
const nameExtensionIfExists = getVariableInScope(context, tempVarPlaceholder) ? uuidV5(name, nameGeneratorUUID).split('-')[0] : '';
127+
return `${tempVarPlaceholder}${nameExtensionIfExists ? `_${nameExtensionIfExists}` : ''}`;
126128
}
127-
return tempVarPlaceholder;
129+
return getSafeVariableName(context, `_${name}`, ++attempts);
130+
128131
}
129132

130133
// Eslint Auto-fix logic, functional components/hooks only

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,11 @@
974974
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.2.tgz#01284dde9ef4e6d8cef6422798d9a3ad18a66f8b"
975975
integrity sha512-g7CK9nHdwjK2n0ymT2CW698FuWJRIx+RP6embAzZ2Qi8/ilIrA1Imt2LVSeHUzKvpoi7BhmmQcXz95eS0f2JXw==
976976

977+
978+
version "9.0.6"
979+
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-9.0.6.tgz#c91ae743d8344a54b2b0c691195f5ff5265f6dfb"
980+
integrity sha512-BT2Krtx4xaO6iwzwMFUYvWBWkV2pr37zD68Vmp1CDV196MzczBRxuEpD6Pr395HAgebC/co7hOphs53r8V7jew==
981+
977982
"@types/yargs-parser@*":
978983
version "21.0.2"
979984
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-21.0.2.tgz#7bd04c5da378496ef1695a1008bf8f71847a8b8b"
@@ -3533,6 +3538,11 @@ util-deprecate@^1.0.1:
35333538
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
35343539
integrity sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==
35353540

3541+
3542+
version "9.0.1"
3543+
resolved "https://registry.yarnpkg.com/uuid/-/uuid-9.0.1.tgz#e188d4c8853cc722220392c424cd637f32293f30"
3544+
integrity sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==
3545+
35363546
v8-compile-cache-lib@^3.0.1:
35373547
version "3.0.1"
35383548
resolved "https://registry.yarnpkg.com/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz#6336e8d71965cb3d35a1bbb7868445a7c05264bf"

0 commit comments

Comments
 (0)