Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/fiery-guests-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nodesecure/js-x-ray": minor
---

Add stats (numberOfImportsDetected, numberOfFilesProcessed) to EntryFilesAnalyser, including tests and documentation updates.
20 changes: 20 additions & 0 deletions workspaces/js-x-ray/docs/EntryFilesAnalyser.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@ Default files extensions are `.js`, `.cjs`, `.mjs` and `.node`

## API

### Stats

The `stats` property of the EntryFilesAnalyser instance:

```ts
stats = {
numberOfImportsDetected: 0,
numberOfFilesProcessed: 0
};
```
The stats will provide :
- the number of unique internal dependencies detected (after resolution)
- the total number of files processed during the analysis

Note:
- imports are deduplicated based on their resolved file path, meaning that different import statements pointing to the same file will be counted once.
- external dependencies are not included in the imports count.

### EntryFilesAnalyser Class

```ts
type ReportOnEntryFile = ReportOnFile & {
file: string;
Expand Down
13 changes: 11 additions & 2 deletions workspaces/js-x-ray/src/EntryFilesAnalyser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ export class EntryFilesAnalyser {
allowedExtensions: Set<string>;
dependencies: DiGraph<VertexDefinition<VertexBody>>;
ignoreENOENT: boolean;
stats = {
numberOfImportsDetected: 0,
numberOfFilesProcessed: 0
};
uniqueImports = new Set<string>();

constructor(
options: EntryFilesAnalyserOptions = {}
Expand Down Expand Up @@ -118,6 +123,9 @@ export class EntryFilesAnalyser {
options: EntryFilesRuntimeOptions = {}
): AsyncGenerator<ReportOnEntryFile> {
this.dependencies = new DiGraph();
this.stats.numberOfImportsDetected = 0;
this.stats.numberOfFilesProcessed = 0;
this.uniqueImports.clear();
this.#depPathCache.clear();

const generators: AsyncGenerator<ReportOnEntryFile>[] = [];
Expand All @@ -141,6 +149,7 @@ export class EntryFilesAnalyser {
if (generators.length > 0) {
yield* combineAsyncIterators(...generators);
}
this.stats.numberOfImportsDetected = this.uniqueImports.size;
}

#normalizeAndCleanEntryFile(
Expand Down Expand Up @@ -188,7 +197,6 @@ export class EntryFilesAnalyser {
if (file.includes("d.ts")) {
return;
}

this.dependencies.addVertex({
id: relativeFile,
adjacentTo: [],
Expand Down Expand Up @@ -221,8 +229,8 @@ export class EntryFilesAnalyser {
}
}
);
this.stats.numberOfFilesProcessed++;
yield { file: relativeFile, ...report };

if (!report.ok) {
return;
}
Expand All @@ -238,6 +246,7 @@ export class EntryFilesAnalyser {
if (depFile === null) {
continue;
}
this.uniqueImports.add(depFile);

const depRelativeFile = this.#getRelativeFilePath(depFile);
if (!this.dependencies.hasVertex(depRelativeFile)) {
Expand Down
15 changes: 15 additions & 0 deletions workspaces/js-x-ray/test/EntryFilesAnalyser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,19 @@ describe("EntryFilesAnalyser", () => {
await Array.fromAsync(generator);
});
});

it("should return the number of files detected and the number of internal dependencies", async() => {
const entryFilesAnalyser = new EntryFilesAnalyser();
const entryUrl = new URL("entry.js", kFixtureURL);
const deepEntryUrl = new URL("deps/deepEntry.js", kFixtureURL);

const generator = entryFilesAnalyser.analyse([
entryUrl,
deepEntryUrl
]);
// we need to yield all reports to have the stats
await Array.fromAsync(generator);
assert.equal(entryFilesAnalyser.stats.numberOfFilesProcessed, 6);
assert.equal(entryFilesAnalyser.stats.numberOfImportsDetected, 4);
});
});
Loading