From 45d5366ca910cd2af05584e47762d8d529fc63d9 Mon Sep 17 00:00:00 2001 From: Tony Gorez Date: Fri, 17 May 2024 16:43:24 +0200 Subject: [PATCH 1/2] feat: enable initalize and finalize for AstAnalyser.analyseFile Signed-off-by: Tony Gorez --- README.md | 2 + src/AstAnalyser.js | 10 ++- test/AstAnalyser.spec.js | 131 ++++++++++++++++++++++++++++++++------- 3 files changed, 117 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 7b87711..220e46f 100644 --- a/README.md +++ b/README.md @@ -258,6 +258,8 @@ interface RuntimeFileOptions { module?: boolean; removeHTMLComments?: boolean; packageName?: string; + initialize?: (sourceFile: SourceFile) => void; + finalize?: (sourceFile: SourceFile) => void; } ``` diff --git a/src/AstAnalyser.js b/src/AstAnalyser.js index 9a497f1..f483dcc 100644 --- a/src/AstAnalyser.js +++ b/src/AstAnalyser.js @@ -65,7 +65,7 @@ export class AstAnalyser { if (finalize) { if (typeof finalize !== "function") { - throw new TypeError("options.initialize must be a function"); + throw new TypeError("options.finalize must be a function"); } finalize(source); } @@ -85,7 +85,9 @@ export class AstAnalyser { const { packageName = null, module = true, - removeHTMLComments = false + removeHTMLComments = false, + initialize, + finalize } = options; const str = await fs.readFile(pathToFile, "utf-8"); @@ -95,7 +97,9 @@ export class AstAnalyser { const data = this.analyse(str, { isMinified: isMin, module: path.extname(filePathString) === ".mjs" ? true : module, - removeHTMLComments + removeHTMLComments, + initialize, + finalize }); if (packageName !== null) { diff --git a/test/AstAnalyser.spec.js b/test/AstAnalyser.spec.js index f41b411..b052749 100644 --- a/test/AstAnalyser.spec.js +++ b/test/AstAnalyser.spec.js @@ -185,7 +185,7 @@ describe("AstAnalyser", (t) => { analyser.analyse("const foo = 'bar';", { initialize: "foo" }); - }); + }, /options.initialize must be a function/); }); it("should call the initialize function", (t) => { @@ -216,7 +216,7 @@ describe("AstAnalyser", (t) => { analyser.analyse("const foo = 'bar';", { finalize: "foo" }); - }); + }, /options.finalize must be a function/); }); it("should call the finalize function", (t) => { @@ -254,30 +254,115 @@ describe("AstAnalyser", (t) => { }); }); - it("remove the packageName from the dependencies list", async () => { - const result = await getAnalyser().analyseFile( - new URL("depName.js", FIXTURE_URL), - { module: false, packageName: "foobar" } - ); - - assert.ok(result.ok); - assert.strictEqual(result.warnings.length, 0); - assert.deepEqual([...result.dependencies.keys()], - ["open"] - ); - }); + describe("analyseFile", () => { + it("remove the packageName from the dependencies list", async () => { + const result = await getAnalyser().analyseFile( + new URL("depName.js", FIXTURE_URL), + { module: false, packageName: "foobar" } + ); + + assert.ok(result.ok); + assert.strictEqual(result.warnings.length, 0); + assert.deepEqual([...result.dependencies.keys()], + ["open"] + ); + }); + + it("should fail with a parsing error", async () => { + const result = await getAnalyser().analyseFile( + new URL("parsingError.js", FIXTURE_URL), + { module: false, packageName: "foobar" } + ); + + assert.strictEqual(result.ok, false); + assert.strictEqual(result.warnings.length, 1); + + const parsingError = result.warnings[0]; + assert.strictEqual(parsingError.kind, "parsing-error"); + }); + + describe("hooks", () => { + const analyser = new AstAnalyser(); + const url = new URL("depName.js", FIXTURE_URL); + + describe("initialize", () => { + it("should throw if initialize is not a function", async () => { + const res = await analyser.analyseFile( + url, { + initialize: "foo" + }); - it("should fail with a parsing error", async () => { - const result = await getAnalyser().analyseFile( - new URL("parsingError.js", FIXTURE_URL), - { module: false, packageName: "foobar" } - ); + assert.strictEqual(res.ok, false); + assert.strictEqual(res.warnings[0].value, "options.initialize must be a function"); + assert.strictEqual(res.warnings[0].kind, "parsing-error"); + }); - assert.strictEqual(result.ok, false); - assert.strictEqual(result.warnings.length, 1); + it("should call the initialize function", async (t) => { + const initialize = t.mock.fn(); + + await analyser.analyseFile(url, { + initialize + }); + + assert.strictEqual(initialize.mock.callCount(), 1); + }); - const parsingError = result.warnings[0]; - assert.strictEqual(parsingError.kind, "parsing-error"); + it("should pass the source file as first argument", async (t) => { + const initialize = t.mock.fn(); + + await analyser.analyseFile(url, { + initialize + }); + + assert.strictEqual(initialize.mock.calls[0].arguments[0] instanceof SourceFile, true); + }); + }); + + describe("finalize", () => { + it("should throw if finalize is not a function", async () => { + const res = await analyser.analyseFile( + url, { + finalize: "foo" + }); + + assert.strictEqual(res.ok, false); + assert.strictEqual(res.warnings[0].value, "options.finalize must be a function"); + assert.strictEqual(res.warnings[0].kind, "parsing-error"); + }); + + it("should call the finalize function", async (t) => { + const finalize = t.mock.fn(); + + await analyser.analyseFile(url, { + finalize + }); + + assert.strictEqual(finalize.mock.callCount(), 1); + }); + + it("should pass the source file as first argument", async (t) => { + const finalize = t.mock.fn(); + + await analyser.analyseFile(url, { + finalize + }); + + assert.strictEqual(finalize.mock.calls[0].arguments[0] instanceof SourceFile, true); + }); + }); + + + it("intialize should be called before finalize", async () => { + const calls = []; + + await analyser.analyseFile(url, { + initialize: () => calls.push("initialize"), + finalize: () => calls.push("finalize") + }); + + assert.deepEqual(calls, ["initialize", "finalize"]); + }); + }); }); describe("prepareSource", () => { From 6ba1cee27626c9e7fcbc5611f63014fba2a6ac0f Mon Sep 17 00:00:00 2001 From: Tony Gorez Date: Fri, 17 May 2024 20:40:02 +0200 Subject: [PATCH 2/2] doc: add todo comment Signed-off-by: Tony Gorez --- src/AstAnalyser.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/AstAnalyser.js b/src/AstAnalyser.js index f483dcc..a162c51 100644 --- a/src/AstAnalyser.js +++ b/src/AstAnalyser.js @@ -41,6 +41,8 @@ export class AstAnalyser { }); const source = new SourceFile(str, this.probesOptions); + // TODO: this check should be factorized in a way that we reuse it + // on analyze and anlyseFile if (initialize) { if (typeof initialize !== "function") { throw new TypeError("options.initialize must be a function"); @@ -63,6 +65,8 @@ export class AstAnalyser { } }); + // TODO: this check should be factorized in a way that we reuse it + // on analyze and anlyseFile if (finalize) { if (typeof finalize !== "function") { throw new TypeError("options.finalize must be a function");