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

Use JsSourceParser as default parser for AstAnalyser class #227

Merged
merged 3 commits into from
Feb 2, 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
7 changes: 4 additions & 3 deletions src/AstAnalyser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import isMinified from "is-minified-code";
// Import Internal Dependencies
import { SourceFile } from "./SourceFile.js";
import { isOneLineExpressionExport } from "./utils/index.js";
import { JsSourceParser } from "./JsSourceParser.js";

export class AstAnalyser {
/**
* @constructor
* @param { SourceParser } parser
* @param { SourceParser } [parser]
*/
constructor(parser) {
constructor(parser = new JsSourceParser()) {
this.parser = parser;
}

Expand All @@ -35,7 +36,7 @@ export class AstAnalyser {
// we walk each AST Nodes, this is a purely synchronous I/O
walk(body, {
enter(node) {
// Skip the root of the AST.
// Skip the root of the AST.
if (Array.isArray(node)) {
return;
}
Expand Down
12 changes: 12 additions & 0 deletions test/AstAnalyser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,18 @@ describe("AstAnalyser", (t) => {
assert.strictEqual(preparedSource, "\nconst yo = 'foo'\n");
});
});

describe("constructor", () => {
it("should not throw an error when instantiated without a custom parser", () => {
assert.doesNotThrow(() => {
const analyser = new AstAnalyser();
// perform basic operations
const result = analyser.analyse("const foo = 'bar';");
// compare array of keys to an empty array to ensure there are no dependencies in result
assert.deepEqual([...result.dependencies.keys()], []);
});
});
});
});

let analyser = null;
Expand Down
2 changes: 1 addition & 1 deletion types/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ interface SourceParser {
}

declare class AstAnalyser {
constructor(parser: SourceParser);
constructor(parser?: SourceParser);
analyse: (str: string, options?: Omit<RuntimeOptions, "customParser">) => Report;
analyzeFile(pathToFile: string, options?: Omit<RuntimeFileOptions, "customParser">): Promise<ReportOnFile>;
}
Expand Down
Loading