Skip to content

Commit ec41a75

Browse files
committed
Add support for concat expressions in HBS files
1 parent 4393d1e commit ec41a75

File tree

4 files changed

+98
-30
lines changed

4 files changed

+98
-30
lines changed

__snapshots__/test.js.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`Test Fixtures concat-expression 1`] = `
4+
"[1/4] 🔍 Finding JS and HBS files...
5+
[2/4] 🔍 Searching for translations keys in JS and HBS files...
6+
[3/4] ⚙️ Checking for unused translations...
7+
[4/4] ⚙️ Checking for missing translations...
8+
9+
👏 No unused translations were found!
10+
11+
👏 No missing translations were found!
12+
"
13+
`;
14+
15+
exports[`Test Fixtures concat-expression 2`] = `Map {}`;
16+
317
exports[`Test Fixtures decorators 1`] = `
418
"[1/4] 🔍 Finding JS and HBS files...
519
[2/4] 🔍 Searching for translations keys in JS and HBS files...
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{{t (concat "prefix" "." "simple")}}
2+
{{t (concat "prefix." (if true "with-if-first" "with-if-second"))}}
3+
{{t (concat "prefix.some-action" (if this.isCompact "-short"))}}
4+
{{t (concat "prefix." (if this.isEditing "edit" "new") ".label")}}
5+
{{t (if true "foo" (concat "prefix." (if this.isEditing "edit" "new") ".nested"))}}
6+
{{t (concat "prefix." this.dynamicKey ".not-missing")}}
7+
{{t (concat "prefix." (if true "a1" (if false "b1" (concat "c" (if false "1" "2")))) ".value")}}
8+
{{t (concat "prefix." (if true this.dynamicKey "key-that-should-exist") ".value")}}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
prefix:
2+
simple: Simple concatenation
3+
with-if-first: First condition
4+
with-if-second: Second condition
5+
some-action: Action that can be long
6+
some-action-short: Action
7+
edit:
8+
label: Label on edit branch
9+
nested: Nested concat on edit branch
10+
new:
11+
label: Label on new branch
12+
nested: Nested concat on new branch
13+
a1:
14+
value: Value
15+
b1:
16+
value: Value
17+
c1:
18+
value: Value
19+
c2:
20+
value: Value
21+
key-that-should-exist:
22+
value: value
23+
foo: Foo

index.js

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)