Skip to content

Commit

Permalink
Fix for issue #177 and improve Angular parser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pablotransifex committed Feb 19, 2023
1 parent 2207a98 commit 76c3a62
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 25 deletions.
24 changes: 20 additions & 4 deletions packages/cli/src/api/parsers/angularHTML.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,32 @@ const pipeBindingRegexp = /'([\s\S]+?)'\s*?\|\s*?translate\s*?:?\s*?({[\s\S]*?})
* @returns {*}
*/
function looseJsonParse(obj) {
let parsed;
let params;

try {
// eslint-disable-next-line no-new-func
parsed = Function(`"use strict";return (${obj})`)();
params = Function(`"use strict";return (${obj})`)();
} catch (err) {
parsed = {};
// When JSON string evaluation fails
// we try to generate a dictionary with the key/value pairs
// in order to obtaint the valid parameters and discard the
// parameters that are not valid (e.g. ReferenceError)
const keyValuePairRegex = /(\w+):\s*(?:"([^"]*)"|(\S+))/g;
const paramsDict = {};
let match;

// eslint-disable-next-line no-cond-assign
while (match = keyValuePairRegex.exec(obj)) {
// eslint-disable-next-line prefer-destructuring
if (match[2]) paramsDict[match[1]] = match[2];
}
const paramsDictStr = JSON.stringify(paramsDict);

// eslint-disable-next-line no-new-func
params = Function(`"use strict";return (${paramsDictStr})`)();
}

return parsed;
return params;
}

/**
Expand Down
58 changes: 52 additions & 6 deletions packages/cli/test/api/extract.hashedkeys.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ describe('extractPhrases with hashed keys', () => {
meta: { context: [], tags: [], occurrences: ['angular-template.html'] },
},
'text.main_title': {
meta: { context: [], occurrences: ['angular-template.html'], tags: [] },
string: 'This is a test',
meta: { context: [], tags: [], occurrences: ['angular-template.html'] },
},
'text.pipe_text': {
string: 'This is a pipe text',
Expand All @@ -328,8 +328,8 @@ describe('extractPhrases with hashed keys', () => {
string: 'This is a second pipe text',
meta: { context: [], tags: [], occurrences: ['angular-template.html'] },
},
'9eae72bb80f1d30adc39a97c56eb2f6b': {
string: '\n This is a\n third pipe text\n ',
'867b7cc4643da9b4c97ababa43c50c23': {
string: 'Used in a {binding}',
meta: { context: [], tags: [], occurrences: ['angular-template.html'] },
},
'text.pipe_text_fourth': {
Expand All @@ -344,12 +344,22 @@ describe('extractPhrases with hashed keys', () => {
string: 'This is a sixth pipe text, no one should do this',
meta: { context: [], tags: [], occurrences: ['angular-template.html'] },
},
'text.pipe_text_third': {
meta: {
context: [],
occurrences: [
'angular-template.html',
],
tags: [],
},
string: 'This is a third pipe text',
},
'text.pipe_text_seventh': {
string: 'This is a seventh pipe test for additional white spaces',
meta: { context: [], tags: [], occurrences: ['angular-template.html'] },
meta: { context: [], tags: ['tagA', 'tagB'], occurrences: ['angular-template.html'] },
},
'867b7cc4643da9b4c97ababa43c50c23': {
string: 'Used in a {binding}',
'30846bf58089a2c546dd18b0aa3005c1': {
string: '\n This is a\n second pipe text\n ',
meta: { context: [], tags: [], occurrences: ['angular-template.html'] },
},
'text.pipe_binding': {
Expand All @@ -360,6 +370,42 @@ describe('extractPhrases with hashed keys', () => {
string: 'This is a text with a context, and it should be recognized as one',
meta: { context: ['is-text'], tags: [], occurrences: ['angular-template.html'] },
},
'some-key': {
meta: {
context: [],
occurrences: [
'angular-template.html',
],
tags: [
't1',
't2',
],
},
string: '{var1}',
},
'some-key-two': {
meta: {
context: [],
occurrences: [
'angular-template.html',
],
tags: [
't3',
't4',
],
},
string: '{var2}',
},
'some-key-three': {
meta: {
context: [],
occurrences: [
'angular-template.html',
],
tags: [],
},
string: '{var3}',
},
});
});

Expand Down
52 changes: 49 additions & 3 deletions packages/cli/test/api/extract.sourcekeys.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ describe('extractPhrases with source keys', () => {
string: 'This is a second pipe text',
meta: { context: [], tags: [], occurrences: ['angular-template.html'] },
},
'\n This is a\n third pipe text\n ': {
string: '\n This is a\n third pipe text\n ',
'\n This is a\n second pipe text\n ': {
string: '\n This is a\n second pipe text\n ',
meta: { context: [], tags: [], occurrences: ['angular-template.html'] },
},
'text.pipe_text_fourth': {
Expand All @@ -325,9 +325,19 @@ describe('extractPhrases with source keys', () => {
string: 'This is a sixth pipe text, no one should do this',
meta: { context: [], tags: [], occurrences: ['angular-template.html'] },
},
'text.pipe_text_third': {
meta: {
context: [],
occurrences: [
'angular-template.html',
],
tags: [],
},
string: 'This is a third pipe text',
},
'text.pipe_text_seventh': {
string: 'This is a seventh pipe test for additional white spaces',
meta: { context: [], tags: [], occurrences: ['angular-template.html'] },
meta: { context: [], tags: ['tagA', 'tagB'], occurrences: ['angular-template.html'] },
},
'Used in a {binding}': {
string: 'Used in a {binding}',
Expand All @@ -341,6 +351,42 @@ describe('extractPhrases with source keys', () => {
string: 'This is a text with a context, and it should be recognized as one',
meta: { context: ['is-text'], tags: [], occurrences: ['angular-template.html'] },
},
'some-key': {
meta: {
context: [],
occurrences: [
'angular-template.html',
],
tags: [
't1',
't2',
],
},
string: '{var1}',
},
'some-key-two': {
meta: {
context: [],
occurrences: [
'angular-template.html',
],
tags: [
't3',
't4',
],
},
string: '{var2}',
},
'some-key-three': {
meta: {
context: [],
occurrences: [
'angular-template.html',
],
tags: [],
},
string: '{var3}',
},
});
});

Expand Down
32 changes: 20 additions & 12 deletions packages/cli/test/fixtures/angular-template.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,46 +26,54 @@ <h1 class="marginBottom-2x">

<p class="using-pipe">
<!-- Note: Object is in JSON to make it parse, Angular will interpolate correctly -->
{{ 'This is a pipe text' | translate:{ key: "text.pipe_text" } }}
{{ 'This is a pipe text' | translate:{ _key: "text.pipe_text" } }}
</p>

<p class="using-pipe-second">
{{ 'This is a second pipe text' | translate }}
{{
'
This is a
third pipe text
second pipe text
' | translate:{}
}}
</p>

<p class="using-pipe-third">
{{ "This is a fourth pipe text" | translate:{ key: "text.pipe_text_fourth" } }}
{{ "This is a third pipe text" | translate:{ key: "text.pipe_text_third" } }}
</p>

<p class="using-pipe-third">
<p class="using-pipe-fourth">
{{ "This is a fourth pipe text" | translate:{ _key: "text.pipe_text_fourth" } }}
</p>

<p class="using-pipe-fifth">
{{ "It’s {weekday} today, and it is a fine day to try out Native!" | translate:{
key: "text.fifth"
_key: "text.fifth"
}}}
</p>

<p class="using-pipe-third">
<p class="using-pipe-sixth">
{{'This is a sixth pipe text, no one should do this'|translate:{
key
:
"text.pipe_text_sixth"
_key: "text.pipe_text_sixth"
}}}
</p>

<p class="using-pipe-third">
{{ "This is a seventh pipe test for additional white spaces" | translate: { key: "text.pipe_text_seventh" } }}
<p class="using-pipe-seventh">
{{ "This is a seventh pipe test for additional white spaces" | translate: { _key: "text.pipe_text_seventh", _tags: 'tagA,tagB' } }}
</p>

<p [matTooltip]="'Used in a {binding}' | translate">
Inside a p
</p>

<p [matTooltip]="'Used in a second binding'|translate:{key: 'text.pipe_binding', _secondParam: true, _thirdParams:false }">
<p class="using-pipe-with-tags-valid">{{ "{var1}" | translate: { _key: "some-key", _tags: "t1,t2", var1: "test 1" } }}</p>

<p class="using-pipe-with-tags-valid">{{ "{var2}" | translate: { _key: "some-key-two", _tags: "t3,t4", var2: _myvariable } }}</p>

<p class="using-pipe-with-tags-invalid">{{ "{var3}" | translate: { _key: "some-key-three", _tags: variableWithTags, var3: "valid value" } }}</p>

<p [matTooltip]="'Used in a second binding'|translate:{_key: 'text.pipe_binding', _secondParam: true, _thirdParams:false }">
Inside a p
</p>

Expand Down

0 comments on commit 76c3a62

Please sign in to comment.