Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: empty code error #134

Merged
merged 2 commits into from
Oct 6, 2024
Merged
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
5 changes: 5 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 @@ -7,6 +7,11 @@ test("no-op returns the same code", async () => {
assert.equal(code, await visitAllIdentifiers(code, async (name) => name));
});

test("no-op returns the same empty code", async () => {
const code = "";
assert.equal(code, await visitAllIdentifiers(code, async (name) => name));
});

test("renames a simple variable", async () => {
const code = `const a = 1;`;
assert.equal(
Expand Down
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
Expand Up @@ -53,10 +53,10 @@ export async function visitAllIdentifiers(
onProgress?.(1);

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

function findScopes(ast: Node): NodePath<Identifier>[] {
Expand Down
6 changes: 6 additions & 0 deletions src/unminify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ export async function unminify(

const file = extractedFiles[i];
const code = await fs.readFile(file.path, "utf-8");

if (code.trim().length === 0) {
verbose.log(`Skipping empty file ${file.path}`);
continue;
}
Comment on lines +21 to +24
Copy link
Contributor

Choose a reason for hiding this comment

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

@0xdevalias thanks for pointing this out, added a commit to verify that there's no extra processing on the top level

Originally posted by @jehna in #134 (comment)

@jehna This was a suggestion I made only as a quick/dirty hacky workaround. By skipping files at the unminify level you technically weaken the flexibility of the plugins; as it means a plugin can no longer take potentially empty code as input, and have valid code as output. While that's technically not an issue with how things are currently implemented; the other solutions I mentioned would solve this better/more flexibly.


const formattedCode = await plugins.reduce(
(p, next) => p.then(next),
Promise.resolve(code)
Expand Down
Loading