@@ -84,11 +84,17 @@ const EXPORT_DEFAULT_RE = /\bexport\s+default\s+/g;
84
84
const TYPE_RE = / ^ \s * ?t y p e \s / ;
85
85
86
86
export function findStaticImports ( code : string ) : StaticImport [ ] {
87
- return matchAll ( ESM_STATIC_IMPORT_RE , code , { type : "static" } ) ;
87
+ return _filterStatement (
88
+ _tryGetLocations ( code , "import" ) ,
89
+ matchAll ( ESM_STATIC_IMPORT_RE , code , { type : "static" } ) ,
90
+ ) ;
88
91
}
89
92
90
93
export function findDynamicImports ( code : string ) : DynamicImport [ ] {
91
- return matchAll ( DYNAMIC_IMPORT_RE , code , { type : "dynamic" } ) ;
94
+ return _filterStatement (
95
+ _tryGetLocations ( code , "import" ) ,
96
+ matchAll ( DYNAMIC_IMPORT_RE , code , { type : "dynamic" } ) ,
97
+ ) ;
92
98
}
93
99
94
100
export function findTypeImports ( code : string ) : TypeImport [ ] {
@@ -217,17 +223,14 @@ export function findExports(code: string): ESMExport[] {
217
223
if ( exports . length === 0 ) {
218
224
return [ ] ;
219
225
}
220
- const exportLocations = _tryGetExportLocations ( code ) ;
226
+ const exportLocations = _tryGetLocations ( code , "export" ) ;
221
227
if ( exportLocations && exportLocations . length === 0 ) {
222
228
return [ ] ;
223
229
}
224
230
225
231
return (
226
- exports
227
- // Filter false positive export matches
228
- . filter (
229
- ( exp ) => ! exportLocations || _isExportStatement ( exportLocations , exp ) ,
230
- )
232
+ // Filter false positive export matches
233
+ _filterStatement ( exportLocations , exports )
231
234
// Prevent multiple exports of same function, only keep latest iteration of signatures
232
235
. filter ( ( exp , index , exports ) => {
233
236
const nextExport = exports [ index + 1 ] ;
@@ -266,17 +269,14 @@ export function findTypeExports(code: string): ESMExport[] {
266
269
if ( exports . length === 0 ) {
267
270
return [ ] ;
268
271
}
269
- const exportLocations = _tryGetExportLocations ( code ) ;
272
+ const exportLocations = _tryGetLocations ( code , "export" ) ;
270
273
if ( exportLocations && exportLocations . length === 0 ) {
271
274
return [ ] ;
272
275
}
273
276
274
277
return (
275
- exports
276
- // Filter false positive export matches
277
- . filter (
278
- ( exp ) => ! exportLocations || _isExportStatement ( exportLocations , exp ) ,
279
- )
278
+ // Filter false positive export matches
279
+ _filterStatement ( exportLocations , exports )
280
280
// Prevent multiple exports of same function, only keep latest iteration of signatures
281
281
. filter ( ( exp , index , exports ) => {
282
282
const nextExport = exports [ index + 1 ] ;
@@ -359,23 +359,31 @@ interface TokenLocation {
359
359
end : number ;
360
360
}
361
361
362
- function _isExportStatement ( exportsLocation : TokenLocation [ ] , exp : ESMExport ) {
363
- return exportsLocation . some ( ( location ) => {
364
- // AST token inside the regex match
365
- return exp . start <= location . start && exp . end >= location . end ;
366
- // AST Token start or end is within the regex match
367
- // return (exp.start <= location.start && location.start <= exp.end) ||
368
- // (exp.start <= location.end && location.end <= exp.end)
362
+ function _filterStatement < T extends TokenLocation > (
363
+ locations : TokenLocation [ ] | undefined ,
364
+ statements : T [ ] ,
365
+ ) : T [ ] {
366
+ return statements . filter ( ( exp ) => {
367
+ return (
368
+ ! locations ||
369
+ locations . some ( ( location ) => {
370
+ // AST token inside the regex match
371
+ return exp . start <= location . start && exp . end >= location . end ;
372
+ // AST Token start or end is within the regex match
373
+ // return (exp.start <= location.start && location.start <= exp.end) ||
374
+ // (exp.start <= location.end && location.end <= exp.end)
375
+ } )
376
+ ) ;
369
377
} ) ;
370
378
}
371
379
372
- function _tryGetExportLocations ( code : string ) {
380
+ function _tryGetLocations ( code : string , label : string ) {
373
381
try {
374
- return _getExportLocations ( code ) ;
382
+ return _getLocations ( code , label ) ;
375
383
} catch { }
376
384
}
377
385
378
- function _getExportLocations ( code : string ) {
386
+ function _getLocations ( code : string , label : string ) {
379
387
const tokens = tokenizer ( code , {
380
388
ecmaVersion : "latest" ,
381
389
sourceType : "module" ,
@@ -385,7 +393,7 @@ function _getExportLocations(code: string) {
385
393
} ) ;
386
394
const locations : TokenLocation [ ] = [ ] ;
387
395
for ( const token of tokens ) {
388
- if ( token . type . label === "export" ) {
396
+ if ( token . type . label === label ) {
389
397
locations . push ( {
390
398
start : token . start ,
391
399
end : token . end ,
0 commit comments