Skip to content

Commit 53dbef0

Browse files
committed
feat(probes) add finalize
1 parent 8f64e95 commit 53dbef0

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-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/ProbeRunner.ts

Lines changed: 5 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;
@@ -137,6 +139,9 @@ export class ProbeRunner {
137139

138140
try {
139141
const result = this.#runProbe(probe, node);
142+
if (probe.finalize) {
143+
probe.finalize(this.sourceFile);
144+
}
140145
if (result === null) {
141146
continue;
142147
}

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,57 @@ 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 fakeProbeNeverValid = {
172+
validateNode: (_: ESTree.Node) => [false],
173+
main: () => ProbeSignals.Skip,
174+
finalize: mock.fn()
175+
};
176+
177+
const probes = [fakeProbe, fakeProbeBreak, fakeProbeSkip, fakeProbeNeverValid];
178+
179+
for (const probe of probes) {
180+
const sourceFile = new SourceFile("");
181+
182+
const pr = new ProbeRunner(
183+
sourceFile,
184+
// @ts-expect-error
185+
[probe]
186+
);
187+
188+
const astNode: ESTree.Node = {
189+
type: "Literal",
190+
value: "test"
191+
};
192+
pr.walk(astNode);
193+
194+
assert.strictEqual(probe.finalize.mock.calls.length, 1);
195+
assert.deepEqual(probe.finalize.mock.calls.at(0)?.arguments, [
196+
sourceFile
197+
]);
198+
}
199+
});
200+
});
148201
});

0 commit comments

Comments
 (0)