Skip to content

Commit 4aeb8b4

Browse files
authored
refactor: use JsSourceParser as default parser for AstAnalyser class (#227)
* Use JsSourceParser as default parser for AstAnalyser class * update type and test * fixed test
1 parent 045f704 commit 4aeb8b4

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

src/AstAnalyser.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import isMinified from "is-minified-code";
99
// Import Internal Dependencies
1010
import { SourceFile } from "./SourceFile.js";
1111
import { isOneLineExpressionExport } from "./utils/index.js";
12+
import { JsSourceParser } from "./JsSourceParser.js";
1213

1314
export class AstAnalyser {
1415
/**
1516
* @constructor
16-
* @param { SourceParser } parser
17+
* @param { SourceParser } [parser]
1718
*/
18-
constructor(parser) {
19+
constructor(parser = new JsSourceParser()) {
1920
this.parser = parser;
2021
}
2122

@@ -35,7 +36,7 @@ export class AstAnalyser {
3536
// we walk each AST Nodes, this is a purely synchronous I/O
3637
walk(body, {
3738
enter(node) {
38-
// Skip the root of the AST.
39+
// Skip the root of the AST.
3940
if (Array.isArray(node)) {
4041
return;
4142
}

test/AstAnalyser.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,18 @@ describe("AstAnalyser", (t) => {
225225
assert.strictEqual(preparedSource, "\nconst yo = 'foo'\n");
226226
});
227227
});
228+
229+
describe("constructor", () => {
230+
it("should not throw an error when instantiated without a custom parser", () => {
231+
assert.doesNotThrow(() => {
232+
const analyser = new AstAnalyser();
233+
// perform basic operations
234+
const result = analyser.analyse("const foo = 'bar';");
235+
// compare array of keys to an empty array to ensure there are no dependencies in result
236+
assert.deepEqual([...result.dependencies.keys()], []);
237+
});
238+
});
239+
});
228240
});
229241

230242
let analyser = null;

types/api.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ interface SourceParser {
7878
}
7979

8080
declare class AstAnalyser {
81-
constructor(parser: SourceParser);
81+
constructor(parser?: SourceParser);
8282
analyse: (str: string, options?: Omit<RuntimeOptions, "customParser">) => Report;
8383
analyzeFile(pathToFile: string, options?: Omit<RuntimeFileOptions, "customParser">): Promise<ReportOnFile>;
8484
}

0 commit comments

Comments
 (0)