@@ -24,24 +24,29 @@ import type { SourceFile } from "./SourceFile.js";
2424import type { OptionalWarningName } from "./warnings.js" ;
2525
2626export type ProbeReturn = void | null | symbol ;
27- export type ProbeInitializeCallback = ( sourceFile : SourceFile ) => void ;
28- export type ProbeFinalizeCallback = ( sourceFile : SourceFile ) => void ;
29- export type ProbeMainCallback = (
30- node : any ,
31- options : { sourceFile : SourceFile ; data ?: any ; }
32- ) => ProbeReturn ;
33- export type ProbeTeardownCallback = ( options : { sourceFile : SourceFile ; } ) => void ;
34- export type ProbeValidationCallback = ( node : ESTree . Node , sourceFile : SourceFile ) => [ boolean , any ?] ;
35-
36- export interface Probe {
27+ export type ProbeContextDef = Record < string , any > ;
28+ export type ProbeContext < T extends ProbeContextDef = ProbeContextDef > = {
29+ sourceFile : SourceFile ;
30+ context ?: T ;
31+ } ;
32+
33+ export type ProbeValidationCallback < T extends ProbeContextDef = ProbeContextDef > = (
34+ node : ESTree . Node , ctx : ProbeContext < T >
35+ ) => [ boolean , any ?] ;
36+
37+ export interface Probe < T extends ProbeContextDef = ProbeContextDef > {
3738 name : string ;
38- initialize ?: ProbeInitializeCallback ;
39- finalize ?: ProbeFinalizeCallback ;
40- validateNode : ProbeValidationCallback | ProbeValidationCallback [ ] ;
41- main : ProbeMainCallback ;
42- teardown ?: ProbeTeardownCallback ;
39+ initialize ?: ( ctx : ProbeContext < T > ) => void | ProbeContext ;
40+ finalize ?: ( ctx : ProbeContext < T > ) => void ;
41+ validateNode : ProbeValidationCallback < T > | ProbeValidationCallback < T > [ ] ;
42+ main : (
43+ node : any ,
44+ ctx : ProbeContext < T > & { data ?: any ; }
45+ ) => ProbeReturn ;
46+ teardown ?: ( ctx : ProbeContext < T > ) => void ;
4347 breakOnMatch ?: boolean ;
4448 breakGroup ?: string ;
49+ context ?: ProbeContext < T > ;
4550}
4651
4752export const ProbeSignals = Object . freeze ( {
@@ -97,28 +102,42 @@ export class ProbeRunner {
97102 `Invalid probe ${ probe . name } : initialize must be a function or undefined`
98103 ) ;
99104 if ( probe . initialize ) {
100- probe . initialize ( sourceFile ) ;
105+ const context = probe . initialize ( this . #getProbeContext( probe ) ) ;
106+ if ( context ) {
107+ probe . context = context ;
108+ }
101109 }
102110 }
103111
104112 this . probes = probes ;
105113 }
106114
115+ #getProbeContext(
116+ probe : Probe
117+ ) : ProbeContext {
118+ return {
119+ sourceFile : this . sourceFile ,
120+ context : probe . context
121+ } ;
122+ }
123+
107124 #runProbe(
108125 probe : Probe ,
109126 node : ESTree . Node
110127 ) : ProbeReturn {
111128 const validationFns = Array . isArray ( probe . validateNode ) ?
112129 probe . validateNode : [ probe . validateNode ] ;
130+ const ctx = this . #getProbeContext( probe ) ;
131+
113132 for ( const validateNode of validationFns ) {
114133 const [ isMatching , data = null ] = validateNode (
115134 node ,
116- this . sourceFile
135+ ctx
117136 ) ;
118137
119138 if ( isMatching ) {
120139 return probe . main ( node , {
121- sourceFile : this . sourceFile ,
140+ ... ctx ,
122141 data
123142 } ) ;
124143 }
@@ -158,9 +177,7 @@ export class ProbeRunner {
158177 }
159178 }
160179 finally {
161- if ( probe . teardown ) {
162- probe . teardown ( { sourceFile : this . sourceFile } ) ;
163- }
180+ probe . teardown ?.( this . #getProbeContext( probe ) ) ;
164181 }
165182 }
166183
@@ -169,9 +186,7 @@ export class ProbeRunner {
169186
170187 finalize ( ) : void {
171188 for ( const probe of this . probes ) {
172- if ( probe . finalize ) {
173- probe . finalize ( this . sourceFile ) ;
174- }
189+ probe . finalize ?.( this . #getProbeContext( probe ) ) ;
175190 }
176191 }
177192}
0 commit comments