Skip to content

Commit 1ed4c30

Browse files
authored
Merge pull request Spittal#146 from JamesBoon/feat/brackets-and-parentheses-matching
Match parentheses and brackets
2 parents 19e2c49 + 8ccb237 commit 1ed4c30

File tree

4 files changed

+133
-6
lines changed

4 files changed

+133
-6
lines changed

src/create-report/vue-files.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ function* getMatches (file: SimpleFile, regExp: RegExp, captureGroup = 1): Itera
4646
/**
4747
* Extracts translation keys from methods such as `$t` and `$tc`.
4848
*
49-
* - **regexp pattern**: (?:[$ .]tc?)\(
49+
* - **regexp pattern**: (?:[$\s.:"'`+\(\[\{]t[cm]?)\(
5050
*
51-
* **description**: Matches the sequence t( or tc(, optionally with either “$”, “.” or “ ” in front of it.
51+
* **description**: Matches the sequence t(, tc( or tm(, optionally with either “$”, SPACE, “.”, “:”, “"”, “'”,
52+
* “`”, "+", "(", "[" or "{" in front of it.
5253
*
5354
* - **regexp pattern**: (["'`])
5455
*
@@ -67,7 +68,7 @@ function* getMatches (file: SimpleFile, regExp: RegExp, captureGroup = 1): Itera
6768
* @returns a list of translation keys found in `file`.
6869
*/
6970
function extractMethodMatches (file: SimpleFile): I18NItemWithBounding[] {
70-
const methodRegExp = /(?:[$ ."'`]t[cm]?)\(\s*?(["'`])((?:[^\\]|\\.)*?)\1/g;
71+
const methodRegExp = /(?:[$\s.:"'`+\(\[\{]t[cm]?)\(\s*?(["'`])((?:[^\\]|\\.)*?)\1/g;
7172
return [ ...getMatches(file, methodRegExp, 2) ];
7273
}
7374

tests/fixtures/expected-values.ts

Lines changed: 116 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,54 @@ export const expectedFromParsedVueFiles = [
118118
file: './tests/fixtures/vue-files/edge-cases.js',
119119
line: 5
120120
},
121+
{
122+
path: 'parentheses',
123+
previousCharacter: "'",
124+
nextCharacter: "'",
125+
file: './tests/fixtures/vue-files/edge-cases.js',
126+
line: 7
127+
},
128+
{
129+
path: 'square brackets',
130+
previousCharacter: "'",
131+
nextCharacter: "'",
132+
file: './tests/fixtures/vue-files/edge-cases.js',
133+
line: 8
134+
},
135+
{
136+
path: 'curly brackets',
137+
previousCharacter: "'",
138+
nextCharacter: "'",
139+
file: './tests/fixtures/vue-files/edge-cases.js',
140+
line: 9
141+
},
142+
{
143+
path: 'colon',
144+
previousCharacter: "'",
145+
nextCharacter: "'",
146+
file: './tests/fixtures/vue-files/edge-cases.js',
147+
line: 10
148+
},
149+
{
150+
path: 'concatenation',
151+
previousCharacter: "'",
152+
nextCharacter: "'",
153+
file: './tests/fixtures/vue-files/edge-cases.js',
154+
line: 12
155+
},
156+
{
157+
path: 'leading tab',
158+
previousCharacter: "'",
159+
nextCharacter: "'",
160+
file: './tests/fixtures/vue-files/edge-cases.js',
161+
line: 13
162+
},
121163
{
122164
path: '${dynamicKey}',
123165
previousCharacter: '`',
124166
nextCharacter: '`',
125167
file: './tests/fixtures/vue-files/edge-cases.js',
126-
line: 8
168+
line: 16
127169
},
128170
{
129171
path: 'header.title',
@@ -325,6 +367,42 @@ export const expectedI18NReport = {
325367
line: 5,
326368
language: 'de_DE'
327369
},
370+
{
371+
path: 'parentheses',
372+
file: './tests/fixtures/vue-files/edge-cases.js',
373+
line: 7,
374+
language: 'de_DE'
375+
},
376+
{
377+
path: 'square brackets',
378+
file: './tests/fixtures/vue-files/edge-cases.js',
379+
line: 8,
380+
language: 'de_DE'
381+
},
382+
{
383+
path: 'curly brackets',
384+
file: './tests/fixtures/vue-files/edge-cases.js',
385+
line: 9,
386+
language: 'de_DE'
387+
},
388+
{
389+
path: 'colon',
390+
file: './tests/fixtures/vue-files/edge-cases.js',
391+
line: 10,
392+
language: 'de_DE'
393+
},
394+
{
395+
path: 'concatenation',
396+
file: './tests/fixtures/vue-files/edge-cases.js',
397+
line: 12,
398+
language: 'de_DE'
399+
},
400+
{
401+
path: 'leading tab',
402+
file: './tests/fixtures/vue-files/edge-cases.js',
403+
line: 13,
404+
language: 'de_DE'
405+
},
328406
{
329407
path: 'header.paragraphs.p_1',
330408
file: './tests/fixtures/vue-files/js-component.js',
@@ -361,6 +439,42 @@ export const expectedI18NReport = {
361439
line: 5,
362440
language: 'en_EN'
363441
},
442+
{
443+
path: 'parentheses',
444+
file: './tests/fixtures/vue-files/edge-cases.js',
445+
line: 7,
446+
language: 'en_EN'
447+
},
448+
{
449+
path: 'square brackets',
450+
file: './tests/fixtures/vue-files/edge-cases.js',
451+
line: 8,
452+
language: 'en_EN'
453+
},
454+
{
455+
path: 'curly brackets',
456+
file: './tests/fixtures/vue-files/edge-cases.js',
457+
line: 9,
458+
language: 'en_EN'
459+
},
460+
{
461+
path: 'colon',
462+
file: './tests/fixtures/vue-files/edge-cases.js',
463+
line: 10,
464+
language: 'en_EN'
465+
},
466+
{
467+
path: 'concatenation',
468+
file: './tests/fixtures/vue-files/edge-cases.js',
469+
line: 12,
470+
language: 'en_EN'
471+
},
472+
{
473+
path: 'leading tab',
474+
file: './tests/fixtures/vue-files/edge-cases.js',
475+
line: 13,
476+
language: 'en_EN'
477+
},
364478
{
365479
path: 'header.paragraphs.p_1',
366480
file: './tests/fixtures/vue-files/js-component.js',
@@ -398,7 +512,7 @@ export const expectedI18NReport = {
398512
{
399513
path: '${dynamicKey}',
400514
file: './tests/fixtures/vue-files/edge-cases.js',
401-
line: 8
515+
line: 16
402516
}
403517
]
404518
}

tests/fixtures/vue-files/edge-cases.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,22 @@ $t("single \" quote");
44
$t(`back \` tick`);
55
$t('Early ' string termination');
66

7+
someFn(t('parentheses'));
8+
const arr = [t('square brackets')];
9+
const obj1 = {t('curly brackets')};
10+
const obj2 = {v:t('colon')};
11+
12+
'start'+t('concatenation')+'end'
13+
t('leading tab') // make sure autoformat does not remove the leading tab!
14+
715
// Should be suspected dynamic
816
<p>{{ t(`${dynamicKey}`) }}</p>
917

1018
// Should not match
1119
gt("FALSE POSITIVE 1");
1220
;t("FALSE POSITIVE 2");
21+
-t("FALSE POSITIVE 3");
22+
_t("FALSE POSITIVE 4");
23+
w0t("FALSE POSITIVE 5");
24+
ಠ_ಠt("FALSE POSITIVE 6");
1325
t(dynamicKey)

tests/unit/create-report/language-files.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('file: create-report/language-files', () => {
2929
writeFileSyncSpy.mockImplementation(() => jest.fn());
3030
const dotStrSpy = jest.spyOn(dot, 'str');
3131
writeMissingToLanguageFiles(readLanguageFiles(languageFiles), expectedI18NReport.missingKeys);
32-
expect(dotStrSpy).toHaveBeenCalledTimes(18);
32+
expect(dotStrSpy).toHaveBeenCalledTimes(36);
3333
expect(writeFileSyncSpy).toHaveBeenCalledTimes(3);
3434
expect(writeFileSyncSpy.mock.calls[0][1]).toContain('missing');
3535
});

0 commit comments

Comments
 (0)