@@ -33,6 +33,7 @@ describe('htmlbars-inline-precompile', function () {
33
33
} ) ;
34
34
35
35
afterEach ( function ( ) {
36
+ sharedStateToBabel = null ;
36
37
sinon . restore ( ) ;
37
38
} ) ;
38
39
@@ -654,13 +655,23 @@ describe('htmlbars-inline-precompile', function () {
654
655
) ;
655
656
} ) ;
656
657
658
+ let sharedStateToBabel = null as any ;
659
+
657
660
let expressionTransform : ExtendedPluginBuilder = ( env ) => {
658
661
return {
659
662
name : 'expression-transform' ,
660
663
visitor : {
661
664
PathExpression ( node , path ) {
662
665
if ( node . original === 'onePlusOne' ) {
666
+ let boundName = sharedStateToBabel ?. boundName ;
667
+ if ( boundName ) {
668
+ env . meta . jsutils . bindVariable ( boundName ) ;
669
+ return env . syntax . builders . path ( boundName ) ;
670
+ }
663
671
let name = env . meta . jsutils . bindExpression ( '1+1' , path , { nameHint : 'two' } ) ;
672
+ if ( sharedStateToBabel ) {
673
+ sharedStateToBabel . boundName = name ;
674
+ }
664
675
return env . syntax . builders . path ( name ) ;
665
676
}
666
677
return undefined ;
@@ -1314,6 +1325,47 @@ describe('htmlbars-inline-precompile', function () {
1314
1325
` ) ;
1315
1326
} ) ;
1316
1327
1328
+ it ( 'can reuse bindings' , function ( ) {
1329
+ plugins = [
1330
+ [ HTMLBarsInlinePrecompile , { targetFormat : 'hbs' , transforms : [ expressionTransform ] } ] ,
1331
+ ] ;
1332
+
1333
+ sharedStateToBabel = { } ;
1334
+
1335
+ let transformed = transform ( stripIndent `
1336
+ import { precompileTemplate } from '@ember/template-compilation';
1337
+ import Message from 'message';
1338
+ const template = precompileTemplate('<Message @text={{onePlusOne}} />', {
1339
+ scope: () => ({
1340
+ Message
1341
+ })
1342
+ });
1343
+ const template2 = precompileTemplate('<Message @text={{onePlusOne}} />', {
1344
+ scope: () => ({
1345
+ Message
1346
+ })
1347
+ });
1348
+ ` ) ;
1349
+
1350
+ expect ( transformed ) . toEqualCode ( `
1351
+ import { precompileTemplate } from '@ember/template-compilation';
1352
+ import Message from 'message';
1353
+ let two = 1 + 1;
1354
+ const template = precompileTemplate("<Message @text={{two}} />", {
1355
+ scope: () => ({
1356
+ Message,
1357
+ two
1358
+ })
1359
+ });
1360
+ const template2 = precompileTemplate("<Message @text={{two}} />", {
1361
+ scope: () => ({
1362
+ Message,
1363
+ two
1364
+ })
1365
+ });
1366
+ ` ) ;
1367
+ } ) ;
1368
+
1317
1369
it ( 'adds new locals to preexisting renamed scope' , function ( ) {
1318
1370
plugins = [
1319
1371
[ HTMLBarsInlinePrecompile , { targetFormat : 'hbs' , transforms : [ expressionTransform ] } ] ,
0 commit comments