@@ -81,7 +81,6 @@ export function parseScriptSetupRanges(
81
81
const definePropProposalA = vueCompilerOptions . experimentalDefinePropProposal === 'kevinEdition' || ast . text . trimStart ( ) . startsWith ( '// @experimentalDefinePropProposal=kevinEdition' ) ;
82
82
const definePropProposalB = vueCompilerOptions . experimentalDefinePropProposal === 'johnsonEdition' || ast . text . trimStart ( ) . startsWith ( '// @experimentalDefinePropProposal=johnsonEdition' ) ;
83
83
const text = ast . text ;
84
- const importComponentNames = new Set < string > ( ) ;
85
84
86
85
const leadingCommentRanges = ts . getLeadingCommentRanges ( text , 0 ) ?. reverse ( ) ?? [ ] ;
87
86
const leadingCommentEndOffset = leadingCommentRanges . find (
@@ -114,22 +113,11 @@ export function parseScriptSetupRanges(
114
113
}
115
114
foundNonImportExportNode = true ;
116
115
}
117
-
118
- if (
119
- ts . isImportDeclaration ( node )
120
- && node . importClause ?. name
121
- && ! node . importClause . isTypeOnly
122
- ) {
123
- const moduleName = _getNodeText ( node . moduleSpecifier ) . slice ( 1 , - 1 ) ;
124
- if ( vueCompilerOptions . extensions . some ( ext => moduleName . endsWith ( ext ) ) ) {
125
- importComponentNames . add ( _getNodeText ( node . importClause . name ) ) ;
126
- }
127
- }
128
116
} ) ;
129
- ts . forEachChild ( ast , child => visitNode ( child , [ ast ] ) ) ;
117
+ ts . forEachChild ( ast , node => visitNode ( node , [ ast ] ) ) ;
130
118
131
119
const templateRefNames = new Set ( useTemplateRef . map ( ref => ref . name ) ) ;
132
- bindings = bindings . filter ( range => {
120
+ bindings = bindings . filter ( ( { range } ) => {
133
121
const name = text . slice ( range . start , range . end ) ;
134
122
return ! templateRefNames . has ( name ) ;
135
123
} ) ;
@@ -138,7 +126,6 @@ export function parseScriptSetupRanges(
138
126
leadingCommentEndOffset,
139
127
importSectionEndOffset,
140
128
bindings,
141
- importComponentNames,
142
129
defineProp,
143
130
defineProps,
144
131
withDefaults,
@@ -433,72 +420,105 @@ export function parseScriptSetupRanges(
433
420
}
434
421
}
435
422
436
- export function parseBindingRanges ( ts : typeof import ( 'typescript' ) , sourceFile : ts . SourceFile ) {
437
- const bindings : TextRange [ ] = [ ] ;
438
- ts . forEachChild ( sourceFile , node => {
423
+ export function parseBindingRanges ( ts : typeof import ( 'typescript' ) , ast : ts . SourceFile ) {
424
+ const bindings : {
425
+ range : TextRange ;
426
+ moduleName ?: string ;
427
+ isDefaultImport ?: boolean ;
428
+ isNamespace ?: boolean ;
429
+ } [ ] = [ ] ;
430
+
431
+ ts . forEachChild ( ast , node => {
439
432
if ( ts . isVariableStatement ( node ) ) {
440
433
for ( const decl of node . declarationList . declarations ) {
441
434
const vars = _findBindingVars ( decl . name ) ;
442
- bindings . push ( ...vars ) ;
435
+ bindings . push ( ...vars . map ( ( range ) => ( { range } ) ) ) ;
443
436
}
444
437
}
445
438
else if ( ts . isFunctionDeclaration ( node ) ) {
446
439
if ( node . name && ts . isIdentifier ( node . name ) ) {
447
- bindings . push ( _getStartEnd ( node . name ) ) ;
440
+ bindings . push ( {
441
+ range : _getStartEnd ( node . name )
442
+ } ) ;
448
443
}
449
444
}
450
445
else if ( ts . isClassDeclaration ( node ) ) {
451
446
if ( node . name ) {
452
- bindings . push ( _getStartEnd ( node . name ) ) ;
447
+ bindings . push ( {
448
+ range : _getStartEnd ( node . name )
449
+ } ) ;
453
450
}
454
451
}
455
452
else if ( ts . isEnumDeclaration ( node ) ) {
456
- bindings . push ( _getStartEnd ( node . name ) ) ;
453
+ bindings . push ( {
454
+ range : _getStartEnd ( node . name )
455
+ } ) ;
457
456
}
458
457
459
458
if ( ts . isImportDeclaration ( node ) ) {
459
+ const moduleName = _getNodeText ( node . moduleSpecifier ) . slice ( 1 , - 1 ) ;
460
+
460
461
if ( node . importClause && ! node . importClause . isTypeOnly ) {
461
- if ( node . importClause . name ) {
462
- bindings . push ( _getStartEnd ( node . importClause . name ) ) ;
462
+ const { name, namedBindings } = node . importClause ;
463
+
464
+ if ( name ) {
465
+ bindings . push ( {
466
+ range : _getStartEnd ( name ) ,
467
+ moduleName,
468
+ isDefaultImport : true
469
+ } ) ;
463
470
}
464
- if ( node . importClause . namedBindings ) {
465
- if ( ts . isNamedImports ( node . importClause . namedBindings ) ) {
466
- for ( const element of node . importClause . namedBindings . elements ) {
471
+ if ( namedBindings ) {
472
+ if ( ts . isNamedImports ( namedBindings ) ) {
473
+ for ( const element of namedBindings . elements ) {
467
474
if ( element . isTypeOnly ) {
468
475
continue ;
469
476
}
470
- bindings . push ( _getStartEnd ( element . name ) ) ;
477
+ bindings . push ( {
478
+ range : _getStartEnd ( element . name ) ,
479
+ moduleName,
480
+ isDefaultImport : element . propertyName ?. text === 'default'
481
+ } ) ;
471
482
}
472
483
}
473
- else if ( ts . isNamespaceImport ( node . importClause . namedBindings ) ) {
474
- bindings . push ( _getStartEnd ( node . importClause . namedBindings . name ) ) ;
484
+ else {
485
+ bindings . push ( {
486
+ range : _getStartEnd ( namedBindings . name ) ,
487
+ moduleName,
488
+ isNamespace : true
489
+ } ) ;
475
490
}
476
491
}
477
492
}
478
493
}
479
494
} ) ;
495
+
480
496
return bindings ;
481
497
482
498
function _getStartEnd ( node : ts . Node ) {
483
- return getStartEnd ( ts , node , sourceFile ) ;
499
+ return getStartEnd ( ts , node , ast ) ;
500
+ }
501
+
502
+ function _getNodeText ( node : ts . Node ) {
503
+ return getNodeText ( ts , node , ast ) ;
484
504
}
485
505
486
506
function _findBindingVars ( left : ts . BindingName ) {
487
- return findBindingVars ( ts , left , sourceFile ) ;
507
+ return findBindingVars ( ts , left , ast ) ;
488
508
}
489
509
}
490
510
491
511
export function findBindingVars (
492
512
ts : typeof import ( 'typescript' ) ,
493
513
left : ts . BindingName ,
494
- sourceFile : ts . SourceFile
514
+ ast : ts . SourceFile
495
515
) {
496
516
const vars : TextRange [ ] = [ ] ;
497
517
worker ( left ) ;
498
518
return vars ;
499
519
function worker ( node : ts . Node ) {
500
520
if ( ts . isIdentifier ( node ) ) {
501
- vars . push ( getStartEnd ( ts , node , sourceFile ) ) ;
521
+ vars . push ( getStartEnd ( ts , node , ast ) ) ;
502
522
}
503
523
// { ? } = ...
504
524
// [ ? ] = ...
@@ -515,7 +535,7 @@ export function findBindingVars(
515
535
}
516
536
// { foo } = ...
517
537
else if ( ts . isShorthandPropertyAssignment ( node ) ) {
518
- vars . push ( getStartEnd ( ts , node . name , sourceFile ) ) ;
538
+ vars . push ( getStartEnd ( ts , node . name , ast ) ) ;
519
539
}
520
540
// { ...? } = ...
521
541
// [ ...? ] = ...
@@ -528,43 +548,43 @@ export function findBindingVars(
528
548
export function getStartEnd (
529
549
ts : typeof import ( 'typescript' ) ,
530
550
node : ts . Node ,
531
- sourceFile : ts . SourceFile
551
+ ast : ts . SourceFile
532
552
) : TextRange {
533
553
return {
534
- start : ( ts as any ) . getTokenPosOfNode ( node , sourceFile ) as number ,
554
+ start : ( ts as any ) . getTokenPosOfNode ( node , ast ) as number ,
535
555
end : node . end ,
536
556
} ;
537
557
}
538
558
539
559
export function getNodeText (
540
560
ts : typeof import ( 'typescript' ) ,
541
561
node : ts . Node ,
542
- sourceFile : ts . SourceFile
562
+ ast : ts . SourceFile
543
563
) {
544
- const { start, end } = getStartEnd ( ts , node , sourceFile ) ;
545
- return sourceFile . text . slice ( start , end ) ;
564
+ const { start, end } = getStartEnd ( ts , node , ast ) ;
565
+ return ast . text . slice ( start , end ) ;
546
566
}
547
567
548
568
function getStatementRange (
549
569
ts : typeof import ( 'typescript' ) ,
550
570
parents : ts . Node [ ] ,
551
571
node : ts . Node ,
552
- sourceFile : ts . SourceFile
572
+ ast : ts . SourceFile
553
573
) {
554
574
let statementRange : TextRange | undefined ;
555
575
for ( let i = parents . length - 1 ; i >= 0 ; i -- ) {
556
576
if ( ts . isStatement ( parents [ i ] ) ) {
557
577
const statement = parents [ i ] ;
558
578
ts . forEachChild ( statement , child => {
559
- const range = getStartEnd ( ts , child , sourceFile ) ;
579
+ const range = getStartEnd ( ts , child , ast ) ;
560
580
statementRange ??= range ;
561
581
statementRange . end = range . end ;
562
582
} ) ;
563
583
break ;
564
584
}
565
585
}
566
586
if ( ! statementRange ) {
567
- statementRange = getStartEnd ( ts , node , sourceFile ) ;
587
+ statementRange = getStartEnd ( ts , node , ast ) ;
568
588
}
569
589
return statementRange ;
570
590
}
0 commit comments