Skip to content

Commit

Permalink
refactor: use JsSourceParser as default parser for AstAnalyser class (#…
Browse files Browse the repository at this point in the history
…227)

* Use JsSourceParser as default parser for AstAnalyser class

* update type and test

* fixed test
  • Loading branch information
madina0801 authored Feb 2, 2024
1 parent 045f704 commit 4aeb8b4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
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

0 comments on commit 4aeb8b4

Please sign in to comment.