Skip to content

Commit

Permalink
Add support for concat expressions in HBS files
Browse files Browse the repository at this point in the history
  • Loading branch information
robinborst95 committed May 26, 2022
1 parent 4393d1e commit ec41a75
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 30 deletions.
14 changes: 14 additions & 0 deletions __snapshots__/test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Test Fixtures concat-expression 1`] = `
"[1/4] 🔍 Finding JS and HBS files...
[2/4] 🔍 Searching for translations keys in JS and HBS files...
[3/4] ⚙️ Checking for unused translations...
[4/4] ⚙️ Checking for missing translations...
👏 No unused translations were found!
👏 No missing translations were found!
"
`;

exports[`Test Fixtures concat-expression 2`] = `Map {}`;

exports[`Test Fixtures decorators 1`] = `
"[1/4] 🔍 Finding JS and HBS files...
[2/4] 🔍 Searching for translations keys in JS and HBS files...
Expand Down
8 changes: 8 additions & 0 deletions fixtures/concat-expression/app/templates/application.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{t (concat "prefix" "." "simple")}}
{{t (concat "prefix." (if true "with-if-first" "with-if-second"))}}
{{t (concat "prefix.some-action" (if this.isCompact "-short"))}}
{{t (concat "prefix." (if this.isEditing "edit" "new") ".label")}}
{{t (if true "foo" (concat "prefix." (if this.isEditing "edit" "new") ".nested"))}}
{{t (concat "prefix." this.dynamicKey ".not-missing")}}
{{t (concat "prefix." (if true "a1" (if false "b1" (concat "c" (if false "1" "2")))) ".value")}}
{{t (concat "prefix." (if true this.dynamicKey "key-that-should-exist") ".value")}}
23 changes: 23 additions & 0 deletions fixtures/concat-expression/translations/en.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
prefix:
simple: Simple concatenation
with-if-first: First condition
with-if-second: Second condition
some-action: Action that can be long
some-action-short: Action
edit:
label: Label on edit branch
nested: Nested concat on edit branch
new:
label: Label on new branch
nested: Nested concat on new branch
a1:
value: Value
b1:
value: Value
c1:
value: Value
c2:
value: Value
key-that-should-exist:
value: value
foo: Foo
83 changes: 53 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,44 +238,67 @@ async function analyzeHbsFile(content) {
// parse the HBS file
let ast = Glimmer.preprocess(content);

function findKeysInIfExpression(node) {
let keysInFirstParam = findKeysInNode(node.params[1]);
let keysInSecondParam = node.params.length > 2 ? findKeysInNode(node.params[2]) : [''];

return [...keysInFirstParam, ...keysInSecondParam];
}

function findKeysInConcatExpression(node) {
let potentialKeys = [''];

for (let param of node.params) {
let keysInParam = findKeysInNode(param);

if (keysInParam.length === 0) return [];

potentialKeys = potentialKeys.reduce((newPotentialKeys, potentialKey) => {
for (let key of keysInParam) {
newPotentialKeys.push(potentialKey + key);
}

return newPotentialKeys;
}, []);
}

return potentialKeys;
}

function findKeysInNode(node) {
if (!node) return [];

if (node.type === 'StringLiteral') {
return [node.value];
} else if (node.type === 'SubExpression' && node.path.original === 'if') {
return findKeysInIfExpression(node);
} else if (node.type === 'SubExpression' && node.path.original === 'concat') {
return findKeysInConcatExpression(node);
}

return [];
}

function processNode(node) {
if (node.path.type !== 'PathExpression') return;
if (node.path.original !== 't') return;
if (node.params.length === 0) return;

for (let key of findKeysInNode(node.params[0])) {
translationKeys.add(key);
}
}

// find translation keys in the syntax tree
Glimmer.traverse(ast, {
// handle {{t "foo"}} case
MustacheStatement(node) {
if (node.path.type !== 'PathExpression') return;
if (node.path.original !== 't') return;
if (node.params.length === 0) return;

let firstParam = node.params[0];
if (firstParam.type === 'StringLiteral') {
translationKeys.add(firstParam.value);
} else if (firstParam.type === 'SubExpression' && firstParam.path.original === 'if') {
if (firstParam.params[1].type === 'StringLiteral') {
translationKeys.add(firstParam.params[1].value);
}
if (firstParam.params[2].type === 'StringLiteral') {
translationKeys.add(firstParam.params[2].value);
}
}
processNode(node);
},

// handle {{some-component foo=(t "bar")}} case
SubExpression(node) {
if (node.path.type !== 'PathExpression') return;
if (node.path.original !== 't') return;
if (node.params.length === 0) return;

let firstParam = node.params[0];
if (firstParam.type === 'StringLiteral') {
translationKeys.add(firstParam.value);
} else if (firstParam.type === 'SubExpression' && firstParam.path.original === 'if') {
if (firstParam.params[1].type === 'StringLiteral') {
translationKeys.add(firstParam.params[1].value);
}
if (firstParam.params[2].type === 'StringLiteral') {
translationKeys.add(firstParam.params[2].value);
}
}
processNode(node);
},
});

Expand Down

0 comments on commit ec41a75

Please sign in to comment.