@@ -9,14 +9,18 @@ import type { ESTree } from "meriyah";
99import isMinified from "is-minified-code" ;
1010
1111// Import Internal Dependencies
12- import { generateWarning , type Warning } from "./warnings.js" ;
12+ import {
13+ generateWarning ,
14+ type Warning ,
15+ type OptionalWarningName
16+ } from "./warnings.js" ;
1317import {
1418 SourceFile ,
15- type ProbesOptions ,
1619 type SourceFlags
1720} from "./SourceFile.js" ;
1821import { isOneLineExpressionExport } from "./utils/index.js" ;
1922import { JsSourceParser , type SourceParser } from "./JsSourceParser.js" ;
23+ import { ProbeRunner , type Probe } from "./ProbeRunner.js" ;
2024
2125export interface Dependency {
2226 unsafe : boolean ;
@@ -63,11 +67,23 @@ export type ReportOnFile = {
6367 warnings : Warning [ ] ;
6468} ;
6569
66- export interface AstAnalyserOptions extends ProbesOptions {
70+ export interface AstAnalyserOptions {
6771 /**
6872 * @default JsSourceParser
6973 */
7074 customParser ?: SourceParser ;
75+ /**
76+ * @default []
77+ */
78+ customProbes ?: Probe [ ] ;
79+ /**
80+ * @default false
81+ */
82+ skipDefaultProbes ?: boolean ;
83+ /**
84+ * @default false
85+ */
86+ optionalWarnings ?: boolean | Iterable < OptionalWarningName > ;
7187}
7288
7389export interface PrepareSourceOptions {
@@ -76,15 +92,40 @@ export interface PrepareSourceOptions {
7692
7793export class AstAnalyser {
7894 parser : SourceParser ;
79- probesOptions : ProbesOptions ;
95+ probes : Probe [ ] ;
8096
8197 constructor ( options : AstAnalyserOptions = { } ) {
98+ const {
99+ customProbes = [ ] ,
100+ optionalWarnings = false ,
101+ skipDefaultProbes = false
102+ } = options ;
103+
82104 this . parser = options . customParser ?? new JsSourceParser ( ) ;
83- this . probesOptions = {
84- customProbes : options . customProbes ?? [ ] ,
85- skipDefaultProbes : options . skipDefaultProbes ?? false ,
86- optionalWarnings : options . optionalWarnings ?? false
87- } ;
105+
106+ let probes = ProbeRunner . Defaults ;
107+ if (
108+ Array . isArray ( customProbes ) &&
109+ customProbes . length > 0
110+ ) {
111+ probes = skipDefaultProbes === true ?
112+ customProbes :
113+ [ ...probes , ...customProbes ] ;
114+ }
115+
116+ if ( typeof optionalWarnings === "boolean" ) {
117+ if ( optionalWarnings ) {
118+ probes = [ ...probes , ...Object . values ( ProbeRunner . Optionals ) ] ;
119+ }
120+ }
121+ else {
122+ const optionalProbes = Array . from ( optionalWarnings ?? [ ] )
123+ . flatMap ( ( warning ) => ProbeRunner . Optionals [ warning ] ?? [ ] ) ;
124+
125+ probes = [ ...probes , ...optionalProbes ] ;
126+ }
127+
128+ this . probes = probes ;
88129 }
89130
90131 analyse (
@@ -102,7 +143,8 @@ export class AstAnalyser {
102143 const body = this . parser . parse ( this . prepareSource ( str , { removeHTMLComments } ) , {
103144 isEcmaScriptModule : Boolean ( module )
104145 } ) ;
105- const source = new SourceFile ( str , this . probesOptions ) ;
146+ const source = new SourceFile ( str ) ;
147+ const runner = new ProbeRunner ( source , this . probes ) ;
106148
107149 // TODO: this check should be factorized in a way that we reuse it
108150 // on analyze and anlyseFile
@@ -116,13 +158,14 @@ export class AstAnalyser {
116158 // we walk each AST Nodes, this is a purely synchronous I/O
117159 // @ts -expect-error
118160 walk ( body , {
119- enter ( node ) {
161+ enter ( node : any ) {
120162 // Skip the root of the AST.
121163 if ( Array . isArray ( node ) ) {
122164 return ;
123165 }
124166
125- const action = source . walk ( node as ESTree . Node ) ;
167+ source . walk ( node ) ;
168+ const action = runner . walk ( node ) ;
126169 if ( action === "skip" ) {
127170 this . skip ( ) ;
128171 }
@@ -137,7 +180,7 @@ export class AstAnalyser {
137180 }
138181 finalize ( source ) ;
139182 }
140- source . probesRunner . finalize ( ) ;
183+ runner . finalize ( ) ;
141184
142185 // Add oneline-require flag if this is a one-line require expression
143186 if ( isOneLineExpressionExport ( body ) ) {
0 commit comments