Skip to content

Commit

Permalink
fix: invalid identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
j4k0xb committed Sep 28, 2024
1 parent 64a1b95 commit 4eca63a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/plugins/local-llm-rename/visit-all-identifiers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;");
});
4 changes: 2 additions & 2 deletions src/plugins/local-llm-rename/visit-all-identifiers.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -41,7 +41,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}`;
}
Expand Down

0 comments on commit 4eca63a

Please sign in to comment.