@@ -228,44 +228,67 @@ async function analyzeHbsFile(content) {
228228 // parse the HBS file
229229 let ast = Glimmer . preprocess ( content ) ;
230230
231+ function findKeysInIfExpression ( node ) {
232+ let keysInFirstParam = findKeysInNode ( node . params [ 1 ] ) ;
233+ let keysInSecondParam = node . params . length > 2 ? findKeysInNode ( node . params [ 2 ] ) : [ '' ] ;
234+
235+ return [ ...keysInFirstParam , ...keysInSecondParam ] ;
236+ }
237+
238+ function findKeysInConcatExpression ( node ) {
239+ let potentialKeys = [ '' ] ;
240+
241+ for ( let param of node . params ) {
242+ let keysInParam = findKeysInNode ( param ) ;
243+
244+ if ( keysInParam . length === 0 ) return [ ] ;
245+
246+ potentialKeys = potentialKeys . reduce ( ( newPotentialKeys , potentialKey ) => {
247+ for ( let key of keysInParam ) {
248+ newPotentialKeys . push ( potentialKey + key ) ;
249+ }
250+
251+ return newPotentialKeys ;
252+ } , [ ] ) ;
253+ }
254+
255+ return potentialKeys ;
256+ }
257+
258+ function findKeysInNode ( node ) {
259+ if ( ! node ) return [ ] ;
260+
261+ if ( node . type === 'StringLiteral' ) {
262+ return [ node . value ] ;
263+ } else if ( node . type === 'SubExpression' && node . path . original === 'if' ) {
264+ return findKeysInIfExpression ( node ) ;
265+ } else if ( node . type === 'SubExpression' && node . path . original === 'concat' ) {
266+ return findKeysInConcatExpression ( node ) ;
267+ }
268+
269+ return [ ] ;
270+ }
271+
272+ function processNode ( node ) {
273+ if ( node . path . type !== 'PathExpression' ) return ;
274+ if ( node . path . original !== 't' ) return ;
275+ if ( node . params . length === 0 ) return ;
276+
277+ for ( let key of findKeysInNode ( node . params [ 0 ] ) ) {
278+ translationKeys . add ( key ) ;
279+ }
280+ }
281+
231282 // find translation keys in the syntax tree
232283 Glimmer . traverse ( ast , {
233284 // handle {{t "foo"}} case
234285 MustacheStatement ( node ) {
235- if ( node . path . type !== 'PathExpression' ) return ;
236- if ( node . path . original !== 't' ) return ;
237- if ( node . params . length === 0 ) return ;
238-
239- let firstParam = node . params [ 0 ] ;
240- if ( firstParam . type === 'StringLiteral' ) {
241- translationKeys . add ( firstParam . value ) ;
242- } else if ( firstParam . type === 'SubExpression' && firstParam . path . original === 'if' ) {
243- if ( firstParam . params [ 1 ] . type === 'StringLiteral' ) {
244- translationKeys . add ( firstParam . params [ 1 ] . value ) ;
245- }
246- if ( firstParam . params [ 2 ] . type === 'StringLiteral' ) {
247- translationKeys . add ( firstParam . params [ 2 ] . value ) ;
248- }
249- }
286+ processNode ( node ) ;
250287 } ,
251288
252289 // handle {{some-component foo=(t "bar")}} case
253290 SubExpression ( node ) {
254- if ( node . path . type !== 'PathExpression' ) return ;
255- if ( node . path . original !== 't' ) return ;
256- if ( node . params . length === 0 ) return ;
257-
258- let firstParam = node . params [ 0 ] ;
259- if ( firstParam . type === 'StringLiteral' ) {
260- translationKeys . add ( firstParam . value ) ;
261- } else if ( firstParam . type === 'SubExpression' && firstParam . path . original === 'if' ) {
262- if ( firstParam . params [ 1 ] . type === 'StringLiteral' ) {
263- translationKeys . add ( firstParam . params [ 1 ] . value ) ;
264- }
265- if ( firstParam . params [ 2 ] . type === 'StringLiteral' ) {
266- translationKeys . add ( firstParam . params [ 2 ] . value ) ;
267- }
268- }
291+ processNode ( node ) ;
269292 } ,
270293 } ) ;
271294
0 commit comments