Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
fix(expressions): fixed indexing issues for identical expressions in …
Browse files Browse the repository at this point in the history
…a template string
  • Loading branch information
skolmer committed Jul 4, 2018
1 parent dcbbafc commit 1bb1c25
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 19 deletions.
6 changes: 3 additions & 3 deletions __tests__/data/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
"custom group": {
"type": "object",
"properties": {
"Hello ${0}, Hello ${0}.": {
"Hello ${0}, Hello ${1}.": {
"type": "string",
"minLength": 1,
"pattern": "(?=.*?\\$\\{0\\})"
"pattern": "(?=.*?\\$\\{0\\})(?=.*?\\$\\{1\\})"
},
"Hello ${0}, you have ${1} in your bank account.": {
"type": "string",
Expand All @@ -34,7 +34,7 @@
}
},
"required": [
"Hello ${0}, Hello ${0}.",
"Hello ${0}, Hello ${1}.",
"Hello ${0}, you have ${1} in your bank account."
]
},
Expand Down
2 changes: 1 addition & 1 deletion __tests__/data/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"unknown": "unknown",
"custom group": {
"Hello ${0}, you have ${1} in your bank account.": "hello${0}, you have ${1} §{0}",
"Hello ${0}, Hello ${0}.": "Hello ${0}, Hello ${0}."
"Hello ${0}, Hello ${1}.": "Hello ${0}, Hello ${1}."
},
"grouped.js": {
"Hello ${0}, you have ${1} in your bank account.": "sad"
Expand Down
2 changes: 1 addition & 1 deletion __tests__/data/translation.valid.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"${0} ${1}": "${0} ${1}",
"custom group": {
"Hello ${0}, you have ${1} in your bank account.": "Hello ${0}, you have ${1} in your bank account.",
"Hello ${0}, Hello ${0}.": "Hello ${0}, Hello ${0}."
"Hello ${0}, Hello ${1}.": "Hello ${0}, Hello ${1}."
},
"custom group 2": {
"Hello ${0}, you have ${1} in your bank account.": "Hello ${0}, you have ${1} in your bank account."
Expand Down
2 changes: 1 addition & 1 deletion __tests__/data/translations/translation.valid.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"${0} ${1}": "${0} ${1}",
"custom group": {
"Hello ${0}, you have ${1} in your bank account.": "Hello ${0}, you have ${1} in your bank account.",
"Hello ${0}, Hello ${0}.": "Hello ${0}, Hello ${0}."
"Hello ${0}, Hello ${1}.": "Hello ${0}, Hello ${1}."
},
"custom group 2": {
"Hello ${0}, you have ${1} in your bank account.": "Hello ${0}, you have in your bank account."
Expand Down
2 changes: 1 addition & 1 deletion __tests__/data/translations/translation.valid.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"${0} ${1}": "${0} ${1}",
"custom group": {
"Hello ${0}, you have ${1} in your bank account.": "Hello ${0}, you have ${1} in your bank account.",
"Hello ${0}, Hello ${0}.": "Hello ${0}, Hello ${0}."
"Hello ${0}, Hello ${1}.": "Hello ${0}, Hello ${1}."
},
"custom group 2": {
"Hello ${0}, you have ${1} in your bank account.": "Hello ${0}, you have ${1} in your bank account."
Expand Down
6 changes: 3 additions & 3 deletions __tests__/exportTranslationKeys.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('exportTranslationKeys', () => {
{
'group': 'custom group',
'items': [
'Hello ${0}, Hello ${0}.',
'Hello ${0}, Hello ${1}.',
'Hello ${0}, you have ${1} in your bank account.'
]
},
Expand Down Expand Up @@ -102,7 +102,7 @@ describe('exportTranslationKeys', () => {
{
'group': 'custom group',
'items': [
'Hello ${0}, Hello ${0}.',
'Hello ${0}, Hello ${1}.',
'Hello ${0}, you have ${1} in your bank account.'
]
},
Expand Down Expand Up @@ -190,7 +190,7 @@ msgid "\\n <users>\\n \${0}\\n </users>\\n"
msgstr ""
msgctxt "custom group"
msgid "Hello \${0}, Hello \${0}."
msgid "Hello \${0}, Hello \${1}."
msgstr ""
msgctxt "custom group"
Expand Down
6 changes: 3 additions & 3 deletions __tests__/generateTranslationSchema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ const expected = {
'custom group': {
'type': 'object',
'properties': {
'Hello ${0}, Hello ${0}.': {
'Hello ${0}, Hello ${1}.': {
'minLength': 1,
'pattern': '(?=.*?\\$\\{0\\})',
'pattern': '(?=.*?\\$\\{0\\})(?=.*?\\$\\{1\\})',
'type': 'string'
},
'Hello ${0}, you have ${1} in your bank account.': {
Expand All @@ -41,7 +41,7 @@ const expected = {
}
},
'required': [
'Hello ${0}, Hello ${0}.',
'Hello ${0}, Hello ${1}.',
'Hello ${0}, you have ${1} in your bank account.'
]
},
Expand Down
9 changes: 3 additions & 6 deletions lib/traversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ const traverseTemplateExpressions = {
node.quasi.expressions.forEach((exp) => {
const expression = source.substring(exp.start, exp.end)
const expExpression = escapeStringRegexp(expression).replace(/\r/gm, '\\r').replace(/\n/gm, '\\n').replace(/\t/gm, '\\t').replace(/\s/gm, '\\s').replace(/"/gm, '\\"')
const regExp = new RegExp(`\\\${\\s*${expExpression}\\s*}(:([a-z])(\\(([^\\)]+)\\))?)?`, 'gm')
const newMatch = match.replace(regExp, `\${${count}}`)
if(match !== newMatch) {
match = newMatch
count++
}
const regExp = new RegExp(`\\\${\\s*${expExpression}\\s*}(:([a-z])(\\(([^\\)]+)\\))?)?`, 'm')
match = match.replace(regExp, `\${${count}}`)
count++
})
let template = match.replace(/\r\n/g, '\n')
if(template) {
Expand Down

0 comments on commit 1bb1c25

Please sign in to comment.