@@ -238,44 +238,67 @@ async function analyzeHbsFile(content) {
238238 // parse the HBS file
239239 let ast = Glimmer . preprocess ( content ) ;
240240
241+ function findKeysInIfExpression ( node ) {
242+ let keysInFirstParam = findKeysInNode ( node . params [ 1 ] ) ;
243+ let keysInSecondParam = node . params . length > 2 ? findKeysInNode ( node . params [ 2 ] ) : [ '' ] ;
244+
245+ return [ ...keysInFirstParam , ...keysInSecondParam ] ;
246+ }
247+
248+ function findKeysInConcatExpression ( node ) {
249+ let potentialKeys = [ '' ] ;
250+
251+ for ( let param of node . params ) {
252+ let keysInParam = findKeysInNode ( param ) ;
253+
254+ if ( keysInParam . length === 0 ) return [ ] ;
255+
256+ potentialKeys = potentialKeys . reduce ( ( newPotentialKeys , potentialKey ) => {
257+ for ( let key of keysInParam ) {
258+ newPotentialKeys . push ( potentialKey + key ) ;
259+ }
260+
261+ return newPotentialKeys ;
262+ } , [ ] ) ;
263+ }
264+
265+ return potentialKeys ;
266+ }
267+
268+ function findKeysInNode ( node ) {
269+ if ( ! node ) return [ ] ;
270+
271+ if ( node . type === 'StringLiteral' ) {
272+ return [ node . value ] ;
273+ } else if ( node . type === 'SubExpression' && node . path . original === 'if' ) {
274+ return findKeysInIfExpression ( node ) ;
275+ } else if ( node . type === 'SubExpression' && node . path . original === 'concat' ) {
276+ return findKeysInConcatExpression ( node ) ;
277+ }
278+
279+ return [ ] ;
280+ }
281+
282+ function processNode ( node ) {
283+ if ( node . path . type !== 'PathExpression' ) return ;
284+ if ( node . path . original !== 't' ) return ;
285+ if ( node . params . length === 0 ) return ;
286+
287+ for ( let key of findKeysInNode ( node . params [ 0 ] ) ) {
288+ translationKeys . add ( key ) ;
289+ }
290+ }
291+
241292 // find translation keys in the syntax tree
242293 Glimmer . traverse ( ast , {
243294 // handle {{t "foo"}} case
244295 MustacheStatement ( node ) {
245- if ( node . path . type !== 'PathExpression' ) return ;
246- if ( node . path . original !== 't' ) return ;
247- if ( node . params . length === 0 ) return ;
248-
249- let firstParam = node . params [ 0 ] ;
250- if ( firstParam . type === 'StringLiteral' ) {
251- translationKeys . add ( firstParam . value ) ;
252- } else if ( firstParam . type === 'SubExpression' && firstParam . path . original === 'if' ) {
253- if ( firstParam . params [ 1 ] . type === 'StringLiteral' ) {
254- translationKeys . add ( firstParam . params [ 1 ] . value ) ;
255- }
256- if ( firstParam . params [ 2 ] . type === 'StringLiteral' ) {
257- translationKeys . add ( firstParam . params [ 2 ] . value ) ;
258- }
259- }
296+ processNode ( node ) ;
260297 } ,
261298
262299 // handle {{some-component foo=(t "bar")}} case
263300 SubExpression ( node ) {
264- if ( node . path . type !== 'PathExpression' ) return ;
265- if ( node . path . original !== 't' ) return ;
266- if ( node . params . length === 0 ) return ;
267-
268- let firstParam = node . params [ 0 ] ;
269- if ( firstParam . type === 'StringLiteral' ) {
270- translationKeys . add ( firstParam . value ) ;
271- } else if ( firstParam . type === 'SubExpression' && firstParam . path . original === 'if' ) {
272- if ( firstParam . params [ 1 ] . type === 'StringLiteral' ) {
273- translationKeys . add ( firstParam . params [ 1 ] . value ) ;
274- }
275- if ( firstParam . params [ 2 ] . type === 'StringLiteral' ) {
276- translationKeys . add ( firstParam . params [ 2 ] . value ) ;
277- }
278- }
301+ processNode ( node ) ;
279302 } ,
280303 } ) ;
281304
0 commit comments