Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(EntryFilesAnalyser): add missing options and patch some minors issues #291

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>;
Loading