Skip to content

Commit

Permalink
Add backtick support (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheton committed Nov 22, 2017
1 parent a7d0519 commit 7cc8ce7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/parser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint no-console: 0 */
/* eslint no-continue: 0 */
/* eslint no-eval: 0 */
import fs from 'fs';
import chalk from 'chalk';
Expand Down Expand Up @@ -248,8 +247,22 @@ class Parser {
.map(func => ('(?:' + func + ')'))
.join('|')
.replace(/\./g, '\\.');
const matchStrippedCharacters = '[\\r\\n\\s]*';
const pattern = '(?:(?:^\\s*)|[^a-zA-Z0-9_])(?:' + matchFuncs + ')\\((' + matchStrippedCharacters + '"(?:[^"\\\\]|\\\\(?:.|$))*"|' + matchStrippedCharacters + '\'(?:[^\'\\\\]|\\\\(?:.|$))*\')' + matchStrippedCharacters + '[,)]';
// `\s` matches a single whitespace character, which includes spaces, tabs, form feeds, line feeds and other unicode spaces.
const matchSpecialCharacters = '[\\r\\n\\s]*';
const pattern = '(?:(?:^\\s*)|[^a-zA-Z0-9_])' +
'(?:' + matchFuncs + ')' +
'\\(' +
'(' +
// backtick (``)
matchSpecialCharacters + '`(?:[^`\\\\]|\\\\(?:.|$))*`' +
'|' +
// double quotes ("")
matchSpecialCharacters + '"(?:[^"\\\\]|\\\\(?:.|$))*"' +
'|' +
// single quote ('')
matchSpecialCharacters + '\'(?:[^\'\\\\]|\\\\(?:.|$))*\'' +
')' +
matchSpecialCharacters + '[\\,\\)]';
const re = new RegExp(pattern, 'gim');

let r;
Expand All @@ -260,7 +273,7 @@ class Parser {

let key = _.trim(r[1]); // Remove leading and trailing whitespace
const firstChar = key[0];
if (_.includes(['\'', '"'], firstChar)) {
if (_.includes(['\'', '"', '`'], firstChar)) {
// Remove first and last character
key = key.slice(1, -1);
}
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/template-literals.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
i18n.t('property in template literals', {
defaultValue: `property in template literals`
});
i18n.t(`added {{foo}}
and {{bar}}`);
1 change: 1 addition & 0 deletions test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ test('Extract properties from template literals', (t) => {
"en": {
"translation": {
"property in template literals": "property in template literals",
"added {{foo}}\n and {{bar}}": "added {{foo}}\n and {{bar}}"
}
}
};
Expand Down

0 comments on commit 7cc8ce7

Please sign in to comment.