From b7e5d6351e2e3ec36810ea215ced7f382ac7ddc2 Mon Sep 17 00:00:00 2001 From: j4k0xb <55899582+j4k0xb@users.noreply.github.com> Date: Sun, 29 Sep 2024 00:49:28 +0200 Subject: [PATCH 1/2] fix: invalid identifiers --- src/plugins/local-llm-rename/visit-all-identifiers.test.ts | 6 ++++++ src/plugins/local-llm-rename/visit-all-identifiers.ts | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/plugins/local-llm-rename/visit-all-identifiers.test.ts b/src/plugins/local-llm-rename/visit-all-identifiers.test.ts index 31f33b8..a447e97 100644 --- a/src/plugins/local-llm-rename/visit-all-identifiers.test.ts +++ b/src/plugins/local-llm-rename/visit-all-identifiers.test.ts @@ -207,3 +207,9 @@ e.b; }) ); }); + +test("should handle invalid identifiers", async () => { + const code = `const a = 1`; + const result = await visitAllIdentifiers(code, async () => "this.kLength"); + assert.equal(result, "const thisKLength = 1;"); +}); diff --git a/src/plugins/local-llm-rename/visit-all-identifiers.ts b/src/plugins/local-llm-rename/visit-all-identifiers.ts index 064bd1b..5bca3f3 100644 --- a/src/plugins/local-llm-rename/visit-all-identifiers.ts +++ b/src/plugins/local-llm-rename/visit-all-identifiers.ts @@ -1,6 +1,6 @@ import { parseAsync, transformFromAstAsync, NodePath } from "@babel/core"; import * as babelTraverse from "@babel/traverse"; -import { Identifier, isValidIdentifier, Node } from "@babel/types"; +import { Identifier, toIdentifier, Node } from "@babel/types"; const traverse: typeof babelTraverse.default.default = ( typeof babelTraverse.default === "function" @@ -39,7 +39,7 @@ export async function visitAllIdentifiers( const surroundingCode = await scopeToString(smallestScope); const renamed = await visitor(smallestScopeNode.name, surroundingCode); - let safeRenamed = isValidIdentifier(renamed) ? renamed : `_${renamed}`; + let safeRenamed = toIdentifier(renamed); while (renames.has(safeRenamed)) { safeRenamed = `_${safeRenamed}`; } From a317fd30fc19e1d1df68ce3fb9c11d5dbfd4a566 Mon Sep 17 00:00:00 2001 From: Jesse Luoto Date: Sun, 6 Oct 2024 23:41:35 +0300 Subject: [PATCH 2/2] Add more invalid identifier tests --- .../visit-all-identifiers.test.ts | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/plugins/local-llm-rename/visit-all-identifiers.test.ts b/src/plugins/local-llm-rename/visit-all-identifiers.test.ts index a447e97..08ca2e4 100644 --- a/src/plugins/local-llm-rename/visit-all-identifiers.test.ts +++ b/src/plugins/local-llm-rename/visit-all-identifiers.test.ts @@ -213,3 +213,30 @@ test("should handle invalid identifiers", async () => { const result = await visitAllIdentifiers(code, async () => "this.kLength"); assert.equal(result, "const thisKLength = 1;"); }); + +test("should handle space in identifier name (happens for some reason though it shouldn't)", async () => { + const code = `const a = 1`; + const result = await visitAllIdentifiers(code, async () => "foo bar"); + assert.equal(result, "const fooBar = 1;"); +}); + +test("should handle reserved identifiers", async () => { + const code = `const a = 1`; + const result = await visitAllIdentifiers(code, async () => "static"); + assert.equal(result, "const _static = 1;"); +}); + +test("should handle multiple identifiers named the same", async () => { + const code = ` +const a = 1; +const b = 1; +`.trim(); + const result = await visitAllIdentifiers(code, async () => "foo"); + assert.equal( + result, + ` +const foo = 1; +const _foo = 1; +`.trim() + ); +});