Skip to content

Commit 5fcd7a5

Browse files
authored
feat: enable initalize and finalize for AstAnalyser.analyseFile (#271)
* feat: enable initalize and finalize for AstAnalyser.analyseFile Signed-off-by: Tony Gorez <[email protected]> * doc: add todo comment Signed-off-by: Tony Gorez <[email protected]> --------- Signed-off-by: Tony Gorez <[email protected]>
1 parent d02cf1b commit 5fcd7a5

File tree

3 files changed

+121
-26
lines changed

3 files changed

+121
-26
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ interface RuntimeFileOptions {
258258
module?: boolean;
259259
removeHTMLComments?: boolean;
260260
packageName?: string;
261+
initialize?: (sourceFile: SourceFile) => void;
262+
finalize?: (sourceFile: SourceFile) => void;
261263
}
262264
```
263265

src/AstAnalyser.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export class AstAnalyser {
4141
});
4242
const source = new SourceFile(str, this.probesOptions);
4343

44+
// TODO: this check should be factorized in a way that we reuse it
45+
// on analyze and anlyseFile
4446
if (initialize) {
4547
if (typeof initialize !== "function") {
4648
throw new TypeError("options.initialize must be a function");
@@ -63,9 +65,11 @@ export class AstAnalyser {
6365
}
6466
});
6567

68+
// TODO: this check should be factorized in a way that we reuse it
69+
// on analyze and anlyseFile
6670
if (finalize) {
6771
if (typeof finalize !== "function") {
68-
throw new TypeError("options.initialize must be a function");
72+
throw new TypeError("options.finalize must be a function");
6973
}
7074
finalize(source);
7175
}
@@ -85,7 +89,9 @@ export class AstAnalyser {
8589
const {
8690
packageName = null,
8791
module = true,
88-
removeHTMLComments = false
92+
removeHTMLComments = false,
93+
initialize,
94+
finalize
8995
} = options;
9096

9197
const str = await fs.readFile(pathToFile, "utf-8");
@@ -95,7 +101,9 @@ export class AstAnalyser {
95101
const data = this.analyse(str, {
96102
isMinified: isMin,
97103
module: path.extname(filePathString) === ".mjs" ? true : module,
98-
removeHTMLComments
104+
removeHTMLComments,
105+
initialize,
106+
finalize
99107
});
100108

101109
if (packageName !== null) {

test/AstAnalyser.spec.js

Lines changed: 108 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ describe("AstAnalyser", (t) => {
185185
analyser.analyse("const foo = 'bar';", {
186186
initialize: "foo"
187187
});
188-
});
188+
}, /options.initialize must be a function/);
189189
});
190190

191191
it("should call the initialize function", (t) => {
@@ -216,7 +216,7 @@ describe("AstAnalyser", (t) => {
216216
analyser.analyse("const foo = 'bar';", {
217217
finalize: "foo"
218218
});
219-
});
219+
}, /options.finalize must be a function/);
220220
});
221221

222222
it("should call the finalize function", (t) => {
@@ -254,30 +254,115 @@ describe("AstAnalyser", (t) => {
254254
});
255255
});
256256

257-
it("remove the packageName from the dependencies list", async () => {
258-
const result = await getAnalyser().analyseFile(
259-
new URL("depName.js", FIXTURE_URL),
260-
{ module: false, packageName: "foobar" }
261-
);
262-
263-
assert.ok(result.ok);
264-
assert.strictEqual(result.warnings.length, 0);
265-
assert.deepEqual([...result.dependencies.keys()],
266-
["open"]
267-
);
268-
});
257+
describe("analyseFile", () => {
258+
it("remove the packageName from the dependencies list", async () => {
259+
const result = await getAnalyser().analyseFile(
260+
new URL("depName.js", FIXTURE_URL),
261+
{ module: false, packageName: "foobar" }
262+
);
263+
264+
assert.ok(result.ok);
265+
assert.strictEqual(result.warnings.length, 0);
266+
assert.deepEqual([...result.dependencies.keys()],
267+
["open"]
268+
);
269+
});
270+
271+
it("should fail with a parsing error", async () => {
272+
const result = await getAnalyser().analyseFile(
273+
new URL("parsingError.js", FIXTURE_URL),
274+
{ module: false, packageName: "foobar" }
275+
);
276+
277+
assert.strictEqual(result.ok, false);
278+
assert.strictEqual(result.warnings.length, 1);
279+
280+
const parsingError = result.warnings[0];
281+
assert.strictEqual(parsingError.kind, "parsing-error");
282+
});
283+
284+
describe("hooks", () => {
285+
const analyser = new AstAnalyser();
286+
const url = new URL("depName.js", FIXTURE_URL);
287+
288+
describe("initialize", () => {
289+
it("should throw if initialize is not a function", async () => {
290+
const res = await analyser.analyseFile(
291+
url, {
292+
initialize: "foo"
293+
});
269294

270-
it("should fail with a parsing error", async () => {
271-
const result = await getAnalyser().analyseFile(
272-
new URL("parsingError.js", FIXTURE_URL),
273-
{ module: false, packageName: "foobar" }
274-
);
295+
assert.strictEqual(res.ok, false);
296+
assert.strictEqual(res.warnings[0].value, "options.initialize must be a function");
297+
assert.strictEqual(res.warnings[0].kind, "parsing-error");
298+
});
275299

276-
assert.strictEqual(result.ok, false);
277-
assert.strictEqual(result.warnings.length, 1);
300+
it("should call the initialize function", async (t) => {
301+
const initialize = t.mock.fn();
302+
303+
await analyser.analyseFile(url, {
304+
initialize
305+
});
306+
307+
assert.strictEqual(initialize.mock.callCount(), 1);
308+
});
278309

279-
const parsingError = result.warnings[0];
280-
assert.strictEqual(parsingError.kind, "parsing-error");
310+
it("should pass the source file as first argument", async (t) => {
311+
const initialize = t.mock.fn();
312+
313+
await analyser.analyseFile(url, {
314+
initialize
315+
});
316+
317+
assert.strictEqual(initialize.mock.calls[0].arguments[0] instanceof SourceFile, true);
318+
});
319+
});
320+
321+
describe("finalize", () => {
322+
it("should throw if finalize is not a function", async () => {
323+
const res = await analyser.analyseFile(
324+
url, {
325+
finalize: "foo"
326+
});
327+
328+
assert.strictEqual(res.ok, false);
329+
assert.strictEqual(res.warnings[0].value, "options.finalize must be a function");
330+
assert.strictEqual(res.warnings[0].kind, "parsing-error");
331+
});
332+
333+
it("should call the finalize function", async (t) => {
334+
const finalize = t.mock.fn();
335+
336+
await analyser.analyseFile(url, {
337+
finalize
338+
});
339+
340+
assert.strictEqual(finalize.mock.callCount(), 1);
341+
});
342+
343+
it("should pass the source file as first argument", async (t) => {
344+
const finalize = t.mock.fn();
345+
346+
await analyser.analyseFile(url, {
347+
finalize
348+
});
349+
350+
assert.strictEqual(finalize.mock.calls[0].arguments[0] instanceof SourceFile, true);
351+
});
352+
});
353+
354+
355+
it("intialize should be called before finalize", async () => {
356+
const calls = [];
357+
358+
await analyser.analyseFile(url, {
359+
initialize: () => calls.push("initialize"),
360+
finalize: () => calls.push("finalize")
361+
});
362+
363+
assert.deepEqual(calls, ["initialize", "finalize"]);
364+
});
365+
});
281366
});
282367

283368
describe("prepareSource", () => {

0 commit comments

Comments
 (0)