Skip to content

Commit bc62d3e

Browse files
feat(probes) add finalize callback (#382)
* feat(probes) add finalize * Update workspaces/js-x-ray/test/AstAnalyser.spec.ts Co-authored-by: PierreDemailly <[email protected]> --------- Co-authored-by: PierreDemailly <[email protected]>
1 parent 99fd4fe commit bc62d3e

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

.changeset/lazy-weeks-roll.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nodesecure/js-x-ray": minor
3+
---
4+
5+
feat(probes): add finalize callback

workspaces/js-x-ray/src/AstAnalyser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export class AstAnalyser {
137137
}
138138
finalize(source);
139139
}
140+
source.probesRunner.finalize();
140141

141142
// Add oneline-require flag if this is a one-line require expression
142143
if (isOneLineExpressionExport(body)) {

workspaces/js-x-ray/src/ProbeRunner.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import type { OptionalWarningName } from "./warnings.js";
2525

2626
export type ProbeReturn = void | null | symbol;
2727
export type ProbeInitializeCallback = (sourceFile: SourceFile) => void;
28+
export type ProbeFinalizeCallback = (sourceFile: SourceFile) => void;
2829
export type ProbeMainCallback = (
2930
node: any,
3031
options: { sourceFile: SourceFile; data?: any; }
@@ -35,6 +36,7 @@ export type ProbeValidationCallback = (node: ESTree.Node, sourceFile: SourceFile
3536
export interface Probe {
3637
name: string;
3738
initialize?: ProbeInitializeCallback;
39+
finalize?: ProbeFinalizeCallback;
3840
validateNode: ProbeValidationCallback | ProbeValidationCallback[];
3941
main: ProbeMainCallback;
4042
teardown?: ProbeTeardownCallback;
@@ -164,4 +166,12 @@ export class ProbeRunner {
164166

165167
return null;
166168
}
169+
170+
finalize(): void {
171+
for (const probe of this.probes) {
172+
if (probe.finalize) {
173+
probe.finalize(this.sourceFile);
174+
}
175+
}
176+
}
167177
}

workspaces/js-x-ray/test/AstAnalyser.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,33 @@ describe("AstAnalyser", () => {
355355
assert.equal(result.warnings.length, 1);
356356
});
357357

358+
it("should call initialize and finalize of every probes at the end", async(t) => {
359+
const calls: string[] = [];
360+
await new AstAnalyser(
361+
{
362+
customParser: new JsSourceParser(),
363+
customProbes: [
364+
{
365+
name: "name",
366+
initialize: () => calls.push("initialize"),
367+
validateNode: () => [true],
368+
main: t.mock.fn(),
369+
finalize: () => calls.push("finalize")
370+
},
371+
{
372+
name: "classic probe",
373+
validateNode: () => [true],
374+
main: t.mock.fn()
375+
}
376+
377+
],
378+
skipDefaultProbes: true
379+
}
380+
).analyseFile(new URL("customProbe.js", kFixtureURL));
381+
382+
assert.deepEqual(calls, ["initialize", "finalize"]);
383+
});
384+
358385
describe("hooks", () => {
359386
const analyser = new AstAnalyser();
360387
const url = new URL("depName.js", kFixtureURL);

workspaces/js-x-ray/test/ProbeRunner.spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,47 @@ describe("ProbeRunner", () => {
145145
assert.strictEqual(fakeProbe.teardown.mock.calls.length, 1);
146146
});
147147
});
148+
149+
describe("finalize", () => {
150+
it("should call the finalize methods", () => {
151+
const fakeProbe = {
152+
validateNode: (_: ESTree.Node) => [true],
153+
main: () => ProbeSignals.Skip,
154+
finalize: mock.fn()
155+
};
156+
157+
const fakeProbeSkip = {
158+
validateNode: (_: ESTree.Node) => [true],
159+
main: () => ProbeSignals.Skip,
160+
teardown: mock.fn(),
161+
finalize: mock.fn()
162+
};
163+
164+
const fakeProbeBreak = {
165+
validateNode: (_: ESTree.Node) => [true],
166+
main: () => ProbeSignals.Break,
167+
teardown: mock.fn(),
168+
finalize: mock.fn()
169+
};
170+
171+
const probes = [fakeProbe, fakeProbeBreak, fakeProbeSkip];
172+
173+
const sourceFile = new SourceFile("");
174+
175+
const pr = new ProbeRunner(
176+
sourceFile,
177+
// @ts-expect-error
178+
probes
179+
);
180+
181+
pr.finalize();
182+
183+
probes.forEach((probe) => {
184+
assert.strictEqual(probe.finalize.mock.calls.length, 1);
185+
assert.deepEqual(probe.finalize.mock.calls.at(0)?.arguments, [
186+
sourceFile
187+
]);
188+
});
189+
});
190+
});
148191
});

0 commit comments

Comments
 (0)