Skip to content

Commit

Permalink
fix(EntryFilesAnalyser): add missing options and patch some minors is…
Browse files Browse the repository at this point in the history
…sues (#291)
  • Loading branch information
fraxken authored Aug 15, 2024
1 parent 4e268a9 commit 86cb3d9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 33 deletions.
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { JsSourceParser } from "./src/JsSourceParser.js";
import { AstAnalyser } from "./src/AstAnalyser.js";
import { EntryFilesAnalyser } from "./src/EntryFilesAnalyser.js";

/**
* @deprecated
*/
function runASTAnalysis(
str,
options = Object.create(null)
Expand Down Expand Up @@ -32,6 +35,9 @@ function runASTAnalysis(
return analyser.analyse(str, opts);
}

/**
* @deprecated
*/
async function runASTAnalysisOnFile(
pathToFile,
options = {}
Expand Down
42 changes: 16 additions & 26 deletions src/EntryFilesAnalyser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ import { AstAnalyser } from "./AstAnalyser.js";
const kDefaultExtensions = ["js", "cjs", "mjs", "node"];

export class EntryFilesAnalyser {
/**
* @constructor
* @param {object} [options={}]
* @param {AstAnalyser} [options.astAnalyzer=new AstAnalyser()]
* @param {function} [options.loadExtensions]
*/
constructor(options = {}) {
this.astAnalyzer = options.astAnalyzer ?? new AstAnalyser();
const rawAllowedExtensions = options.loadExtensions
Expand All @@ -25,44 +19,40 @@ export class EntryFilesAnalyser {
this.allowedExtensions = new Set(rawAllowedExtensions);
}

/**
* Asynchronously analyze a set of entry files yielding analysis reports.
*
* @param {(string | URL)[]} entryFiles
* @yields {Object} - Yields an object containing the analysis report for each file.
*/
async* analyse(entryFiles) {
async* analyse(
entryFiles,
analyseFileOptions
) {
this.analyzedDeps = new Set();

for (const file of entryFiles) {
yield* this.#analyzeFile(file);
yield* this.#analyseFile(file, analyseFileOptions);
}
}

async* #analyzeFile(file) {
async* #analyseFile(
file,
options
) {
const filePath = file instanceof URL ? fileURLToPath(file) : file;
const report = await this.astAnalyzer.analyseFile(file);
const report = await this.astAnalyzer.analyseFile(file, options);

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

if (!report.ok) {
return;
}

yield* this.#analyzeDeps(
report.dependencies,
path.dirname(filePath)
);
}

async* #analyzeDeps(deps, basePath) {
for (const [name] of deps) {
const depPath = await this.#getInternalDepPath(name, basePath);
for (const [name] of report.dependencies) {
const depPath = await this.#getInternalDepPath(
name,
path.dirname(filePath)
);

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

yield* this.#analyzeFile(depPath);
yield* this.#analyseFile(depPath, options);
}
}
}
Expand Down
41 changes: 34 additions & 7 deletions types/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,18 @@ interface SourceParser {

declare class AstAnalyser {
constructor(options?: AstAnalyserOptions);
analyse: (str: string, options?: RuntimeOptions) => Report;
analyseFile(pathToFile: string, options?: RuntimeFileOptions): Promise<ReportOnFile>;
analyse: (
str: string,
options?: RuntimeOptions
) => Report;
analyseFile(
pathToFile: string,
options?: RuntimeFileOptions
): Promise<ReportOnFile>;
analyseFileSync(
pathToFile: string,
options?: RuntimeFileOptions
): ReportOnFile;
}

interface EntryFilesAnalyserOptions {
Expand All @@ -119,8 +129,16 @@ interface EntryFilesAnalyserOptions {

declare class SourceFile {
constructor(source: string, options: any);
addDependency(name: string, location?: string | null, unsafe?: boolean): void;
addWarning(name: WarningName, value: string, location?: any): void;
addDependency(
name: string,
location?: string | null,
unsafe?: boolean
): void;
addWarning(
name: WarningName,
value: string,
location?: any
): void;
analyzeLiteral(node: any, inArrayExpr?: boolean): void;
getResult(isMinified: boolean): any;
walk(node: any): "skip" | null;
Expand All @@ -132,12 +150,21 @@ declare class EntryFilesAnalyser {
/**
* Asynchronously analyze a set of entry files yielding analysis reports.
*/
analyse(entryFiles: (string | URL)[]): AsyncGenerator<ReportOnFile & { url: string }>;
analyse(
entryFiles: Iterable<string | URL>,
options?: RuntimeFileOptions
): AsyncGenerator<ReportOnFile & { url: string }>;
}

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

declare function runASTAnalysis(str: string, options?: RuntimeOptions & AstAnalyserOptions): Report;
declare function runASTAnalysisOnFile(pathToFile: string, options?: RuntimeFileOptions & AstAnalyserOptions): Promise<ReportOnFile>;
declare function runASTAnalysis(
str: string,
options?: RuntimeOptions & AstAnalyserOptions
): Report;
declare function runASTAnalysisOnFile(
pathToFile: string,
options?: RuntimeFileOptions & AstAnalyserOptions
): Promise<ReportOnFile>;

0 comments on commit 86cb3d9

Please sign in to comment.