Skip to content

Commit 86cb3d9

Browse files
authored
fix(EntryFilesAnalyser): add missing options and patch some minors issues (#291)
1 parent 4e268a9 commit 86cb3d9

File tree

3 files changed

+56
-33
lines changed

3 files changed

+56
-33
lines changed

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { JsSourceParser } from "./src/JsSourceParser.js";
44
import { AstAnalyser } from "./src/AstAnalyser.js";
55
import { EntryFilesAnalyser } from "./src/EntryFilesAnalyser.js";
66

7+
/**
8+
* @deprecated
9+
*/
710
function runASTAnalysis(
811
str,
912
options = Object.create(null)
@@ -32,6 +35,9 @@ function runASTAnalysis(
3235
return analyser.analyse(str, opts);
3336
}
3437

38+
/**
39+
* @deprecated
40+
*/
3541
async function runASTAnalysisOnFile(
3642
pathToFile,
3743
options = {}

src/EntryFilesAnalyser.js

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ import { AstAnalyser } from "./AstAnalyser.js";
1010
const kDefaultExtensions = ["js", "cjs", "mjs", "node"];
1111

1212
export class EntryFilesAnalyser {
13-
/**
14-
* @constructor
15-
* @param {object} [options={}]
16-
* @param {AstAnalyser} [options.astAnalyzer=new AstAnalyser()]
17-
* @param {function} [options.loadExtensions]
18-
*/
1913
constructor(options = {}) {
2014
this.astAnalyzer = options.astAnalyzer ?? new AstAnalyser();
2115
const rawAllowedExtensions = options.loadExtensions
@@ -25,44 +19,40 @@ export class EntryFilesAnalyser {
2519
this.allowedExtensions = new Set(rawAllowedExtensions);
2620
}
2721

28-
/**
29-
* Asynchronously analyze a set of entry files yielding analysis reports.
30-
*
31-
* @param {(string | URL)[]} entryFiles
32-
* @yields {Object} - Yields an object containing the analysis report for each file.
33-
*/
34-
async* analyse(entryFiles) {
22+
async* analyse(
23+
entryFiles,
24+
analyseFileOptions
25+
) {
3526
this.analyzedDeps = new Set();
3627

3728
for (const file of entryFiles) {
38-
yield* this.#analyzeFile(file);
29+
yield* this.#analyseFile(file, analyseFileOptions);
3930
}
4031
}
4132

42-
async* #analyzeFile(file) {
33+
async* #analyseFile(
34+
file,
35+
options
36+
) {
4337
const filePath = file instanceof URL ? fileURLToPath(file) : file;
44-
const report = await this.astAnalyzer.analyseFile(file);
38+
const report = await this.astAnalyzer.analyseFile(file, options);
4539

4640
yield { url: filePath, ...report };
4741

4842
if (!report.ok) {
4943
return;
5044
}
5145

52-
yield* this.#analyzeDeps(
53-
report.dependencies,
54-
path.dirname(filePath)
55-
);
56-
}
57-
58-
async* #analyzeDeps(deps, basePath) {
59-
for (const [name] of deps) {
60-
const depPath = await this.#getInternalDepPath(name, basePath);
46+
for (const [name] of report.dependencies) {
47+
const depPath = await this.#getInternalDepPath(
48+
name,
49+
path.dirname(filePath)
50+
);
6151

6252
if (depPath && !this.analyzedDeps.has(depPath)) {
6353
this.analyzedDeps.add(depPath);
6454

65-
yield* this.#analyzeFile(depPath);
55+
yield* this.#analyseFile(depPath, options);
6656
}
6757
}
6858
}

types/api.d.ts

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,18 @@ interface SourceParser {
108108

109109
declare class AstAnalyser {
110110
constructor(options?: AstAnalyserOptions);
111-
analyse: (str: string, options?: RuntimeOptions) => Report;
112-
analyseFile(pathToFile: string, options?: RuntimeFileOptions): Promise<ReportOnFile>;
111+
analyse: (
112+
str: string,
113+
options?: RuntimeOptions
114+
) => Report;
115+
analyseFile(
116+
pathToFile: string,
117+
options?: RuntimeFileOptions
118+
): Promise<ReportOnFile>;
119+
analyseFileSync(
120+
pathToFile: string,
121+
options?: RuntimeFileOptions
122+
): ReportOnFile;
113123
}
114124

115125
interface EntryFilesAnalyserOptions {
@@ -119,8 +129,16 @@ interface EntryFilesAnalyserOptions {
119129

120130
declare class SourceFile {
121131
constructor(source: string, options: any);
122-
addDependency(name: string, location?: string | null, unsafe?: boolean): void;
123-
addWarning(name: WarningName, value: string, location?: any): void;
132+
addDependency(
133+
name: string,
134+
location?: string | null,
135+
unsafe?: boolean
136+
): void;
137+
addWarning(
138+
name: WarningName,
139+
value: string,
140+
location?: any
141+
): void;
124142
analyzeLiteral(node: any, inArrayExpr?: boolean): void;
125143
getResult(isMinified: boolean): any;
126144
walk(node: any): "skip" | null;
@@ -132,12 +150,21 @@ declare class EntryFilesAnalyser {
132150
/**
133151
* Asynchronously analyze a set of entry files yielding analysis reports.
134152
*/
135-
analyse(entryFiles: (string | URL)[]): AsyncGenerator<ReportOnFile & { url: string }>;
153+
analyse(
154+
entryFiles: Iterable<string | URL>,
155+
options?: RuntimeFileOptions
156+
): AsyncGenerator<ReportOnFile & { url: string }>;
136157
}
137158

138159
declare class JsSourceParser implements SourceParser {
139160
parse(source: string, options: unknown): Statement[];
140161
}
141162

142-
declare function runASTAnalysis(str: string, options?: RuntimeOptions & AstAnalyserOptions): Report;
143-
declare function runASTAnalysisOnFile(pathToFile: string, options?: RuntimeFileOptions & AstAnalyserOptions): Promise<ReportOnFile>;
163+
declare function runASTAnalysis(
164+
str: string,
165+
options?: RuntimeOptions & AstAnalyserOptions
166+
): Report;
167+
declare function runASTAnalysisOnFile(
168+
pathToFile: string,
169+
options?: RuntimeFileOptions & AstAnalyserOptions
170+
): Promise<ReportOnFile>;

0 commit comments

Comments
 (0)