Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/plugins/babel/babel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PluginItem } from "@babel/core";
import * as t from "@babel/types";
import { transformWithPlugins } from "../../babel-utils.js";
import bautifier from "babel-plugin-transform-beautifier";
import beautifier from "babel-plugin-transform-beautifier";
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be renamed everywhere in this file, not just the import


const convertVoidToUndefined: PluginItem = {
visitor: {
Expand Down
86 changes: 44 additions & 42 deletions src/plugins/local-llm-rename/visit-all-identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,61 @@ const traverse: typeof babelTraverse.default.default = (

type Visitor = (name: string, scope: string) => Promise<string>;

export async function visitAllIdentifiers(
export async function visitAllIdentifiers
(
code: string,
visitor: Visitor,
contextWindowSize: number,
onProgress?: (percentageDone: number) => void
) {
const ast = await parseAsync(code, { sourceType: "unambiguous" });
const renames = new Set<string>();
const visited = new Set<string>();

if (!ast) {
throw new Error("Failed to parse code");
}
onProgress?: (percentageDone: number) => void
)
{
const ast = await parseAsync(code, { sourceType: "unambiguous" });
const renames = new Set<string>();
const visited = new Set<string>();

if (!ast) {
throw new Error("Failed to parse code");
}

const scopes = await findScopes(ast);
const numRenamesExpected = scopes.length;
const scopes = await findScopes(ast);
const numRenamesExpected = scopes.length;

for (const smallestScope of scopes) {
if (hasVisited(smallestScope, visited)) continue;
for (const smallestScope of scopes) {
if (hasVisited(smallestScope, visited)) continue;

const smallestScopeNode = smallestScope.node;
if (smallestScopeNode.type !== "Identifier") {
throw new Error("No identifiers found");
}
const smallestScopeNode = smallestScope.node;
if (smallestScopeNode.type !== "Identifier") {
throw new Error("No identifiers found");
}

const surroundingCode = await scopeToString(
smallestScope,
contextWindowSize
);
const renamed = await visitor(smallestScopeNode.name, surroundingCode);
if (renamed !== smallestScopeNode.name) {
let safeRenamed = toIdentifier(renamed);
while (
renames.has(safeRenamed) ||
smallestScope.scope.hasBinding(safeRenamed)
) {
safeRenamed = `_${safeRenamed}`;
const surroundingCode = await scopeToString(
smallestScope,
contextWindowSize
);
const renamed = await visitor(smallestScopeNode.name, surroundingCode);
if (renamed !== smallestScopeNode.name) {
let safeRenamed = toIdentifier(renamed);
while (
renames.has(safeRenamed) ||
smallestScope.scope.hasBinding(safeRenamed)
) {
safeRenamed = `_${safeRenamed}`;
}
renames.add(safeRenamed);

smallestScope.scope.rename(smallestScopeNode.name, safeRenamed);
}
renames.add(safeRenamed);
markVisited(smallestScope, smallestScopeNode.name, visited);

smallestScope.scope.rename(smallestScopeNode.name, safeRenamed);
onProgress?.(visited.size / numRenamesExpected);
}
markVisited(smallestScope, smallestScopeNode.name, visited);

onProgress?.(visited.size / numRenamesExpected);
}
onProgress?.(1);
onProgress?.(1);

const stringified = await transformFromAstAsync(ast);
if (stringified?.code == null) {
throw new Error("Failed to stringify code");
}
return stringified.code;
const stringified = await transformFromAstAsync(ast);
if (stringified?.code == null) {
throw new Error("Failed to stringify code");
}
return stringified.code;
}

function findScopes(ast: Node): NodePath<Identifier>[] {
Expand Down