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: add intialize and finalize for AstAnalyser.analyze API #269

Merged
merged 5 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -223,6 +223,8 @@ interface RuntimeOptions {
module?: boolean;
removeHTMLComments?: boolean;
isMinified?: boolean;
initialize?: (sourceFile: SourceFile) => void;
finalize?: (sourceFile: SourceFile) => void;
tony-go marked this conversation as resolved.
Show resolved Hide resolved
}
```

Expand Down
80 changes: 76 additions & 4 deletions test/AstAnalyser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,81 @@ describe("AstAnalyser", (t) => {
assert.equal(result.warnings[0].kind, kWarningUnsafeDanger);
assert.equal(result.warnings.length, 1);
});

describe("intialize", () => {
const analyser = new AstAnalyser();
it("should throw if initialize is not a function", () => {
assert.throws(() => {
analyser.analyse("const foo = 'bar';", {
initialize: "foo"
});
});
});

it("should call the initialize function", () => {
let hasBeenCalled = false;
const initialize = () => {
assert.strictEqual(hasBeenCalled, false);
hasBeenCalled = true;
};

analyser.analyse("const foo = 'bar';", {
initialize
});
assert.strictEqual(hasBeenCalled, true);
});

it("should pass the source file as first argument", () => {
let hasBeenCalled = false;
const initialize = (source) => {
assert.strictEqual(source instanceof SourceFile, true);
};

analyser.analyse("const foo = 'bar';", {
initialize
});
assert.strictEqual(hasBeenCalled, true);
});
});

describe("finalize", () => {
const analyser = new AstAnalyser();
it("should throw if finalize is not a function", () => {
assert.throws(() => {
analyser.analyse("const foo = 'bar';", {
finalize: "foo"
});
});
});

it("should call the finalize function", () => {
let hasBeenCalled = false;
const finalize = () => {
assert.strictEqual(hasBeenCalled, false);
hasBeenCalled = true;
};

analyser.analyse("const foo = 'bar';", {
finalize
});
assert.strictEqual(hasBeenCalled, true);
});

it("should pass the source file as first argument", () => {
let hasBeenCalled = false;
const finalize = (source) => {
assert.strictEqual(source instanceof SourceFile, true);
};

analyser.analyse("const foo = 'bar';", {
finalize
});
assert.strictEqual(hasBeenCalled, true);
});
});
});

it("remove the packageName from the dependencies list", async() => {
it("remove the packageName from the dependencies list", async () => {
const result = await getAnalyser().analyseFile(
new URL("depName.js", FIXTURE_URL),
{ module: false, packageName: "foobar" }
Expand All @@ -189,7 +261,7 @@ describe("AstAnalyser", (t) => {
);
});

it("should fail with a parsing error", async() => {
it("should fail with a parsing error", async () => {
const result = await getAnalyser().analyseFile(
new URL("parsingError.js", FIXTURE_URL),
{ module: false, packageName: "foobar" }
Expand Down Expand Up @@ -248,8 +320,8 @@ describe("AstAnalyser", (t) => {
it("should remove multiple HTML comments", () => {
const preparedSource = getAnalyser().prepareSource(
"<!-- const yo = 5; -->\nconst yo = 'foo'\n<!-- const yo = 5; -->", {
removeHTMLComments: true
});
removeHTMLComments: true
});

assert.strictEqual(preparedSource, "\nconst yo = 'foo'\n");
});
Expand Down
2 changes: 2 additions & 0 deletions types/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ interface RuntimeOptions {
* @default false
*/
isMinified?: boolean;
initialize?: (sourceFile: SourceFile) => void;
finalize?: (sourceFile: SourceFile) => void;
}

interface RuntimeFileOptions extends Omit<RuntimeOptions, "isMinified"> {
Expand Down
Loading