diff --git a/src/AstAnalyser.js b/src/AstAnalyser.js index 3ef4a03..d957699 100644 --- a/src/AstAnalyser.js +++ b/src/AstAnalyser.js @@ -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; } @@ -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; } diff --git a/test/AstAnalyser.spec.js b/test/AstAnalyser.spec.js index fc58856..3b792c3 100644 --- a/test/AstAnalyser.spec.js +++ b/test/AstAnalyser.spec.js @@ -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; diff --git a/types/api.d.ts b/types/api.d.ts index 5d05248..f81ea66 100644 --- a/types/api.d.ts +++ b/types/api.d.ts @@ -78,7 +78,7 @@ interface SourceParser { } declare class AstAnalyser { - constructor(parser: SourceParser); + constructor(parser?: SourceParser); analyse: (str: string, options?: Omit) => Report; analyzeFile(pathToFile: string, options?: Omit): Promise; }