@@ -72,25 +72,16 @@ export function makeFirstTransform(opts: FirstTransformParams) {
72
72
name : '@embroider/macros/first' ,
73
73
74
74
visitor : {
75
- Program : {
76
- enter ( node : any ) {
77
- if ( node . blockParams . length > 0 ) {
78
- scopeStack . push ( node . blockParams ) ;
79
- }
80
- } ,
81
- exit ( node : any ) {
82
- if ( node . blockParams . length > 0 ) {
83
- scopeStack . pop ( ) ;
84
- }
85
- } ,
86
- } ,
75
+ ...scopeVisitors ( env , scopeStack ) ,
87
76
SubExpression ( node : any , walker : { parent : { node : any } } ) {
88
77
if ( node . path . type !== 'PathExpression' ) {
89
78
return ;
90
79
}
91
- if ( inScope ( scopeStack , node . path . parts [ 0 ] ) ) {
80
+
81
+ if ( inScope ( scopeStack , headOf ( node . path ) ) ) {
92
82
return ;
93
83
}
84
+
94
85
if ( node . path . original === 'macroGetOwnConfig' ) {
95
86
return literal (
96
87
getConfig ( node , opts . configs , opts . packageRoot , moduleName , true , packageCache ) ,
@@ -120,7 +111,8 @@ export function makeFirstTransform(opts: FirstTransformParams) {
120
111
if ( node . path . type !== 'PathExpression' ) {
121
112
return ;
122
113
}
123
- if ( inScope ( scopeStack , node . path . parts [ 0 ] ) ) {
114
+
115
+ if ( inScope ( scopeStack , headOf ( node . path ) ) ) {
124
116
return ;
125
117
}
126
118
if ( node . path . original === 'macroGetOwnConfig' ) {
@@ -166,23 +158,13 @@ export function makeSecondTransform() {
166
158
name : '@embroider/macros/second' ,
167
159
168
160
visitor : {
169
- Program : {
170
- enter ( node : any ) {
171
- if ( node . blockParams . length > 0 ) {
172
- scopeStack . push ( node . blockParams ) ;
173
- }
174
- } ,
175
- exit ( node : any ) {
176
- if ( node . blockParams . length > 0 ) {
177
- scopeStack . pop ( ) ;
178
- }
179
- } ,
180
- } ,
161
+ ...scopeVisitors ( env , scopeStack ) ,
181
162
BlockStatement ( node : any ) {
182
163
if ( node . path . type !== 'PathExpression' ) {
183
164
return ;
184
165
}
185
- if ( inScope ( scopeStack , node . path . parts [ 0 ] ) ) {
166
+
167
+ if ( inScope ( scopeStack , headOf ( node . path ) ) ) {
186
168
return ;
187
169
}
188
170
if ( node . path . original === 'if' ) {
@@ -196,7 +178,8 @@ export function makeSecondTransform() {
196
178
if ( node . path . type !== 'PathExpression' ) {
197
179
return ;
198
180
}
199
- if ( inScope ( scopeStack , node . path . parts [ 0 ] ) ) {
181
+
182
+ if ( inScope ( scopeStack , headOf ( node . path ) ) ) {
200
183
return ;
201
184
}
202
185
if ( node . path . original === 'if' ) {
@@ -228,13 +211,14 @@ export function makeSecondTransform() {
228
211
) {
229
212
modifier . path = macroUnlessExpression ( modifier . path , env . syntax . builders ) ;
230
213
if ( modifier . path . type === 'UndefinedLiteral' ) {
231
- return true ;
214
+ return false ;
232
215
}
233
216
}
234
217
if ( modifier . path . type !== 'PathExpression' ) {
235
218
return true ;
236
219
}
237
- if ( inScope ( scopeStack , modifier . path . parts [ 0 ] ) ) {
220
+
221
+ if ( inScope ( scopeStack , headOf ( node . path ) ) ) {
238
222
return true ;
239
223
}
240
224
if ( modifier . path . original === 'macroMaybeAttrs' ) {
@@ -248,7 +232,8 @@ export function makeSecondTransform() {
248
232
if ( node . path . type !== 'PathExpression' ) {
249
233
return ;
250
234
}
251
- if ( inScope ( scopeStack , node . path . parts [ 0 ] ) ) {
235
+
236
+ if ( inScope ( scopeStack , headOf ( node . path ) ) ) {
252
237
return ;
253
238
}
254
239
if ( node . path . original === 'if' ) {
@@ -281,3 +266,34 @@ function inScope(scopeStack: string[][], name: string) {
281
266
}
282
267
return false ;
283
268
}
269
+
270
+ function headOf ( path : any ) {
271
+ if ( ! path ) return ;
272
+
273
+ return 'head' in path ? path . head . name : path . parts [ 0 ] ;
274
+ }
275
+
276
+ function scopeVisitors ( env : any , scopeStack : string [ ] [ ] ) {
277
+ function enter ( node : any ) {
278
+ if ( node . blockParams . length > 0 ) {
279
+ scopeStack . push ( node . blockParams ) ;
280
+ }
281
+ }
282
+ function exit ( node : any ) {
283
+ if ( node . blockParams . length > 0 ) {
284
+ scopeStack . pop ( ) ;
285
+ }
286
+ }
287
+
288
+ let hasTemplate = 'template' in env . syntax . builders ;
289
+ if ( hasTemplate ) {
290
+ return {
291
+ Template : { enter, exit } ,
292
+ Block : { enter, exit } ,
293
+ } ;
294
+ } else {
295
+ return {
296
+ Program : { enter, exit } ,
297
+ } ;
298
+ }
299
+ }
0 commit comments