Skip to content

Commit

Permalink
remove comment-like strings from content before importUrls()
Browse files Browse the repository at this point in the history
this fixes hayd#86 partially.
  • Loading branch information
masnagam committed Oct 6, 2023
1 parent 09f9994 commit 9fbed6d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
9 changes: 8 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ export class Udd {
async run(): Promise<UddResult[]> {
const content: string = await this.content();

const urls: string[] = importUrls(content, this.registries);
// Remove "comment-like" strings from `content`.
//
// This is a naive way and not a proper way to remove comments from
// JavaScript and TypeScript files, but works well for our purpose.
const contentWithoutComments =
content.replace(/\/\*([^*]|\*[^\/])*\*\/|(?<=[^:])\/\/.*|^\/\/.*/gm, '');

const urls: string[] = importUrls(contentWithoutComments, this.registries);
this.progress.n = urls.length;

// from a url we need to extract the current version
Expand Down
32 changes: 32 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,35 @@ import "https://raw.githubusercontent.com/foo/bar/main/mod.ts#=";
const results = await testUdd(contents, expected);
assertEquals(results.length, 0);
});

Deno.test("uddIgnoreComments", async () => {
{
const contents = `//import "https://fakeregistry.com/foo@0.0.1/mod.ts";`;
const expected = contents;
const results = await testUdd(contents, expected);
assertEquals(results.length, 0);
}
{
const contents = `/*import "https://fakeregistry.com/foo@0.0.1/mod.ts";*/`;
const expected = contents;
const results = await testUdd(contents, expected);
assertEquals(results.length, 0);
}
// limitations
//
// TODO: probably, we should use a js parser and collect and modify import
// declarations in the AST. Then, serialize the modified AST to the source
// text.
{
const contents = `
//import "https://fakeregistry.com/foo@0.0.1/mod.ts";
import "https://fakeregistry.com/foo@0.0.1/mod.ts";
`;
const expected = `
//import "https://fakeregistry.com/foo@0.0.2/mod.ts";
import "https://fakeregistry.com/foo@0.0.2/mod.ts";
`;
const results = await testUdd(contents, expected);
assertEquals(results.length, 1);
}
});

0 comments on commit 9fbed6d

Please # to comment.