@@ -997,13 +997,104 @@ describe('htmlbars-inline-precompile', function () {
997
997
} ) ;
998
998
999
999
it ( 'can emit side-effectful import' , function ( ) {
1000
- let compatTransform : ExtendedPluginBuilder = ( env ) => {
1000
+ let compatTransform : ExtendedPluginBuilder = ( ) => {
1001
+ return {
1002
+ name : 'compat-transform' ,
1003
+ visitor : {
1004
+ ElementNode ( node ) {
1005
+ if ( node . tag === 'Thing' ) {
1006
+ node . tag = 'NewThing' ;
1007
+ }
1008
+ } ,
1009
+ } ,
1010
+ } ;
1011
+ } ;
1012
+
1013
+ plugins = [ [ HTMLBarsInlinePrecompile , { targetFormat : 'hbs' , transforms : [ compatTransform ] } ] ] ;
1014
+
1015
+ let transformed = transform ( stripIndent `
1016
+ import { precompileTemplate } from '@ember/template-compilation';
1017
+ let NewThing = '';
1018
+ export default function() {
1019
+ const template = precompileTemplate('<Thing />');
1020
+ }
1021
+ ` ) ;
1022
+
1023
+ expect ( transformed ) . toEqualCode ( `
1024
+ import { precompileTemplate } from '@ember/template-compilation';
1025
+ let NewThing = '';
1026
+ export default function () {
1027
+ const template = precompileTemplate("<NewThing />", {
1028
+ scope: () => ({
1029
+ NewThing
1030
+ })
1031
+ });
1032
+ }` ) ;
1033
+ } ) ;
1034
+
1035
+ it ( 'updates scope correctly when renamed' , function ( ) {
1036
+ let renameTransform : ExtendedPluginBuilder = ( ) => {
1001
1037
return {
1002
1038
name : 'compat-transform' ,
1003
1039
visitor : {
1004
1040
ElementNode ( node ) {
1041
+ if ( node . tag === 'Thing' ) {
1042
+ node . tag = 'NewThing' ;
1043
+ }
1044
+ } ,
1045
+ } ,
1046
+ } ;
1047
+ } ;
1048
+
1049
+ plugins = [ [ HTMLBarsInlinePrecompile , { targetFormat : 'hbs' , transforms : [ ] } ] ] ;
1050
+
1051
+ let transformed = transform ( stripIndent `
1052
+ import { precompileTemplate } from '@ember/template-compilation';
1053
+ let Thing = '';
1054
+ let NewThing = '';
1055
+ export default function() {
1056
+ const template = precompileTemplate('<Thing />');
1057
+ }
1058
+ ` ) ;
1059
+
1060
+ expect ( transformed ) . toEqualCode ( `
1061
+ import { precompileTemplate } from '@ember/template-compilation';
1062
+ let Thing = '';
1063
+ let NewThing = '';
1064
+ export default function () {
1065
+ const template = precompileTemplate("<Thing />", {
1066
+ scope: () => ({
1067
+ Thing
1068
+ })
1069
+ });
1070
+ }` ) ;
1071
+
1072
+ plugins = [ [ HTMLBarsInlinePrecompile , { targetFormat : 'hbs' , transforms : [ renameTransform ] } ] ] ;
1073
+
1074
+ transformed = transform ( transformed ) ;
1075
+
1076
+ expect ( transformed ) . toEqualCode ( `
1077
+ import { precompileTemplate } from '@ember/template-compilation';
1078
+ let Thing = '';
1079
+ let NewThing = '';
1080
+ export default function () {
1081
+ const template = precompileTemplate("<NewThing />", {
1082
+ scope: () => ({
1083
+ NewThing
1084
+ })
1085
+ });
1086
+ }` ) ;
1087
+ } ) ;
1088
+
1089
+ it ( 'updates scope correctly when renamed' , function ( ) {
1090
+ let compatTransform : ExtendedPluginBuilder = ( env ) => {
1091
+ return {
1092
+ name : 'compat-transform' ,
1093
+ visitor : {
1094
+ ElementNode ( node , path ) {
1005
1095
if ( node . tag === 'Thing' ) {
1006
1096
env . meta . jsutils . importForSideEffect ( 'setup-the-things' ) ;
1097
+ node . tag = env . meta . jsutils . bindExpression ( 'Thing' , path , { nameHint : 'NewThing' } ) ;
1007
1098
}
1008
1099
} ,
1009
1100
} ,
@@ -1014,12 +1105,24 @@ describe('htmlbars-inline-precompile', function () {
1014
1105
1015
1106
let transformed = transform ( stripIndent `
1016
1107
import { precompileTemplate } from '@ember/template-compilation';
1108
+ let Thing = '';
1017
1109
export default function() {
1018
1110
const template = precompileTemplate('<Thing />');
1019
1111
}
1020
1112
` ) ;
1021
1113
1022
- expect ( transformed ) . toContain ( `import "setup-the-things"` ) ;
1114
+ expect ( transformed ) . toEqualCode ( `
1115
+ import { precompileTemplate } from '@ember/template-compilation';
1116
+ let NewThing = Thing;
1117
+ import "setup-the-things";
1118
+ let Thing = '';
1119
+ export default function () {
1120
+ const template = precompileTemplate("<NewThing />", {
1121
+ scope: () => ({
1122
+ NewThing
1123
+ })
1124
+ });
1125
+ }` ) ;
1023
1126
} ) ;
1024
1127
1025
1128
describe ( 'source-to-source' , function ( ) {
@@ -1627,6 +1730,16 @@ describe('htmlbars-inline-precompile', function () {
1627
1730
/ S c o p e o b j e c t s f o r ` p r e c o m p i l e T e m p l a t e ` m a y o n l y c o n t a i n d i r e c t r e f e r e n c e s t o i n - s c o p e v a l u e s , e .g . { b a r } o r { b a r : b a r } /
1628
1731
) ;
1629
1732
} ) ;
1733
+
1734
+ it ( 'correctly removes not used scope' , function ( ) {
1735
+ let source = '<foo /><bar/>' ;
1736
+ let spy = sinon . spy ( compiler , 'precompile' ) ;
1737
+
1738
+ transform (
1739
+ `import { precompileTemplate } from '@ember/template-compilation';\nvar compiled = precompileTemplate('${ source } ', { scope: () => ({ foo, bar, baz }) });`
1740
+ ) ;
1741
+ expect ( spy . firstCall . lastArg ) . toHaveProperty ( 'locals' , [ 'foo' , 'bar' ] ) ;
1742
+ } ) ;
1630
1743
} ) ;
1631
1744
1632
1745
describe ( 'implicit-scope-form' , function ( ) {
0 commit comments