@@ -147,7 +147,6 @@ interface State<EnvSpecificOptions> {
147
147
lastInsertedPath : NodePath < t . Statement > | undefined ;
148
148
filename : string ;
149
149
recursionGuard : Set < unknown > ;
150
- originalImportedNames : Map < string , [ string , string ] > ;
151
150
}
152
151
153
152
export function makePlugin < EnvSpecificOptions > ( loadOptions : ( opts : EnvSpecificOptions ) => Options ) {
@@ -156,26 +155,7 @@ export function makePlugin<EnvSpecificOptions>(loadOptions: (opts: EnvSpecificOp
156
155
) : Babel . PluginObj < State < EnvSpecificOptions > > {
157
156
let t = babel . types ;
158
157
159
- return {
160
- pre ( this : State < EnvSpecificOptions > , file ) {
161
- // Remember the available set of imported names very early here in <pre>
162
- // so that when other plugins (particularly
163
- // @babel /plugin-transform-typescript) drop "unused" imports in their
164
- // own Program.enter we still know about them. If we want to use them
165
- // from inside a template, they weren't really unused and we can ensure
166
- // they continue to exist.
167
- this . originalImportedNames = new Map ( ) ;
168
- for ( let statement of file . ast . program . body ) {
169
- if ( statement . type === 'ImportDeclaration' ) {
170
- for ( let specifier of statement . specifiers ) {
171
- this . originalImportedNames . set ( specifier . local . name , [
172
- statement . source . value ,
173
- importedName ( specifier ) ,
174
- ] ) ;
175
- }
176
- }
177
- }
178
- } ,
158
+ const plugin = {
179
159
visitor : {
180
160
Program : {
181
161
enter ( path : NodePath < t . Program > , state : State < EnvSpecificOptions > ) {
@@ -335,6 +315,15 @@ export function makePlugin<EnvSpecificOptions>(loadOptions: (opts: EnvSpecificOp
335
315
} ,
336
316
} ,
337
317
} ;
318
+
319
+ return {
320
+ pre ( this : State < EnvSpecificOptions > , file ) {
321
+ // run our processing in pre so that imports for gts
322
+ // are kept for other plugins.
323
+ babel . traverse ( file . ast , plugin . visitor , file . scope , this ) ;
324
+ } ,
325
+ visitor : { } ,
326
+ } ;
338
327
} as ( babel : typeof Babel ) => Babel . PluginObj < unknown > ;
339
328
}
340
329
@@ -513,7 +502,6 @@ function insertCompiledTemplate<EnvSpecificOptions>(
513
502
configFile : false ,
514
503
} ) as t . File ;
515
504
516
- ensureImportedNames ( target , scopeLocals , state . util , state . originalImportedNames ) ;
517
505
remapIdentifiers ( precompileResultAST , babel , scopeLocals ) ;
518
506
519
507
let templateExpression = ( precompileResultAST . program . body [ 0 ] as t . VariableDeclaration )
@@ -548,6 +536,7 @@ function insertCompiledTemplate<EnvSpecificOptions>(
548
536
) ;
549
537
}
550
538
target . replaceWith ( expression ) ;
539
+ target . scope . crawl ( ) ;
551
540
}
552
541
553
542
function insertTransformedTemplate < EnvSpecificOptions > (
@@ -572,15 +561,16 @@ function insertTransformedTemplate<EnvSpecificOptions>(
572
561
) ;
573
562
let ast = preprocess ( template , { ...options , mode : 'codemod' } ) ;
574
563
let transformed = print ( ast , { entityEncoding : 'raw' } ) ;
564
+ let needsScopeCrawl = false ;
575
565
if ( target . isCallExpression ( ) ) {
576
566
( target . get ( 'arguments.0' ) as NodePath < t . Node > ) . replaceWith ( t . stringLiteral ( transformed ) ) ;
577
567
if ( ! scopeLocals . isEmpty ( ) ) {
578
568
if ( ! formatOptions . enableScope ) {
579
569
maybePruneImport ( state . util , target . get ( 'callee' ) ) ;
580
570
target . set ( 'callee' , precompileTemplate ( state . util , target ) ) ;
581
571
}
582
- ensureImportedNames ( target , scopeLocals , state . util , state . originalImportedNames ) ;
583
572
updateScope ( babel , target , scopeLocals ) ;
573
+ needsScopeCrawl = true ;
584
574
}
585
575
586
576
if ( formatOptions . rfc931Support === 'polyfilled' ) {
@@ -590,7 +580,7 @@ function insertTransformedTemplate<EnvSpecificOptions>(
590
580
removeEvalAndScope ( target ) ;
591
581
target . node . arguments = target . node . arguments . slice ( 0 , 2 ) ;
592
582
state . recursionGuard . add ( target . node ) ;
593
- target . replaceWith (
583
+ target = target . replaceWith (
594
584
t . callExpression ( state . util . import ( target , '@ember/component' , 'setComponentTemplate' ) , [
595
585
target . node ,
596
586
backingClass ?. node ??
@@ -604,7 +594,11 @@ function insertTransformedTemplate<EnvSpecificOptions>(
604
594
[ ]
605
595
) ,
606
596
] )
607
- ) ;
597
+ ) [ 0 ] ;
598
+ needsScopeCrawl = true ;
599
+ }
600
+ if ( needsScopeCrawl ) {
601
+ target . scope . crawl ( ) ;
608
602
}
609
603
} else {
610
604
if ( ! scopeLocals . isEmpty ( ) ) {
@@ -614,8 +608,8 @@ function insertTransformedTemplate<EnvSpecificOptions>(
614
608
let newCall = target . replaceWith (
615
609
t . callExpression ( precompileTemplate ( state . util , target ) , [ t . stringLiteral ( transformed ) ] )
616
610
) [ 0 ] ;
617
- ensureImportedNames ( newCall , scopeLocals , state . util , state . originalImportedNames ) ;
618
611
updateScope ( babel , newCall , scopeLocals ) ;
612
+ newCall . scope . crawl ( ) ;
619
613
} else {
620
614
( target . get ( 'quasi' ) . get ( 'quasis.0' ) as NodePath < t . TemplateElement > ) . replaceWith (
621
615
t . templateElement ( { raw : transformed } )
@@ -752,31 +746,4 @@ function name(node: t.StringLiteral | t.Identifier) {
752
746
}
753
747
}
754
748
755
- function ensureImportedNames (
756
- target : NodePath < t . Node > ,
757
- scopeLocals : ScopeLocals ,
758
- util : ImportUtil ,
759
- originalImportedNames : Map < string , [ string , string ] >
760
- ) {
761
- for ( let [ nameInTemplate , identifier ] of scopeLocals . entries ( ) ) {
762
- if ( ! target . scope . getBinding ( identifier ) ) {
763
- let available = originalImportedNames . get ( identifier ) ;
764
- if ( available ) {
765
- let newIdent = util . import ( target , available [ 0 ] , available [ 1 ] , identifier ) ;
766
- scopeLocals . add ( nameInTemplate , newIdent . name ) ;
767
- }
768
- }
769
- }
770
- }
771
-
772
- function importedName ( node : t . ImportDeclaration [ 'specifiers' ] [ number ] ) : string {
773
- if ( node . type === 'ImportDefaultSpecifier' ) {
774
- return 'default' ;
775
- } else if ( node . type === 'ImportNamespaceSpecifier' ) {
776
- return '*' ;
777
- } else {
778
- return name ( node . imported ) ;
779
- }
780
- }
781
-
782
749
export default makePlugin < Options > ( ( options ) => options ) ;
0 commit comments