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

feat: enable initalize and finalize for AstAnalyser.analyseFile #271

Merged
merged 2 commits into from
May 17, 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ interface RuntimeFileOptions {
module?: boolean;
removeHTMLComments?: boolean;
packageName?: string;
initialize?: (sourceFile: SourceFile) => void;
finalize?: (sourceFile: SourceFile) => void;
}
```

Expand Down
14 changes: 11 additions & 3 deletions src/AstAnalyser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -63,9 +65,11 @@ 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.initialize must be a function");
throw new TypeError("options.finalize must be a function");
}
finalize(source);
}
Expand All @@ -85,7 +89,9 @@ export class AstAnalyser {
const {
packageName = null,
module = true,
removeHTMLComments = false
removeHTMLComments = false,
initialize,
finalize
} = options;

const str = await fs.readFile(pathToFile, "utf-8");
Expand All @@ -95,7 +101,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) {
Expand Down
131 changes: 108 additions & 23 deletions test/AstAnalyser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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");
tony-go marked this conversation as resolved.
Show resolved Hide resolved
});

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", () => {
Expand Down
Loading