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

Commit

Permalink
fix(schema): fixed expression index issue in validation schema
Browse files Browse the repository at this point in the history
  • Loading branch information
skolmer committed Mar 9, 2018
1 parent e5714ec commit 6a33bee
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 12 deletions.
1 change: 1 addition & 0 deletions __tests__/data/grouped.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import i18nc, { i18nGroup as e } from 'es2015-i18n-tag'
export class Test {
log() {
console.log(this.i18n`Hello ${name}, you have ${amount}:c in your bank account.`)
console.log(this.i18n`Hello ${name}, Hello ${name}.`)
console.log(i18nc('custom inline group') `Hello!`)
console.log(this.i18n('custom inline group') `Welcome!`)

Expand Down
6 changes: 6 additions & 0 deletions __tests__/data/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@
"custom group": {
"type": "object",
"properties": {
"Hello ${0}, Hello ${0}.": {
"type": "string",
"minLength": 1,
"pattern": "(?=.*?\\$\\{0\\})"
},
"Hello ${0}, you have ${1} in your bank account.": {
"type": "string",
"minLength": 1,
"pattern": "(?=.*?\\$\\{0\\})(?=.*?\\$\\{1\\})"
}
},
"required": [
"Hello ${0}, Hello ${0}.",
"Hello ${0}, you have ${1} in your bank account."
]
},
Expand Down
3 changes: 2 additions & 1 deletion __tests__/data/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"${0} ${1}": "${0} ${1}",
"unknown": "unknown",
"custom group": {
"Hello ${0}, you have ${1} in your bank account.": "hello${0}, you have ${1} §{0}"
"Hello ${0}, you have ${1} in your bank account.": "hello${0}, you have ${1} §{0}",
"Hello ${0}, Hello ${0}.": "Hello ${0}, Hello ${0}."
},
"grouped.js": {
"Hello ${0}, you have ${1} in your bank account.": "sad"
Expand Down
3 changes: 2 additions & 1 deletion __tests__/data/translation.valid.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"\n <users>\n ${0}\n </users>\n": "\n <users>\n ${0}\n </users>\n",
"${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}, you have ${1} in your bank account.": "Hello ${0}, you have ${1} in your bank account.",
"Hello ${0}, Hello ${0}.": "Hello ${0}, Hello ${0}."
},
"custom group 2": {
"Hello ${0}, you have ${1} in your bank account.": "Hello ${0}, you have ${1} in your bank account."
Expand Down
3 changes: 2 additions & 1 deletion __tests__/data/translations/translation.valid.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"\n <users>\n ${0}\n </users>\n": "\n <users>\n ${0}\n </users>\n",
"${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}, you have ${1} in your bank account.": "Hello ${0}, you have ${1} in your bank account.",
"Hello ${0}, Hello ${0}.": "Hello ${0}, Hello ${0}."
},
"custom group 2": {
"Hello ${0}, you have ${1} in your bank account.": "Hello ${0}, you have in your bank account."
Expand Down
3 changes: 2 additions & 1 deletion __tests__/data/translations/translation.valid.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"\n <users>\n ${0}\n </users>\n": "\n <users>\n ${0}\n </users>\n",
"${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}, you have ${1} in your bank account.": "Hello ${0}, you have ${1} in your bank account.",
"Hello ${0}, Hello ${0}.": "Hello ${0}, Hello ${0}."
},
"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: 6 additions & 0 deletions __tests__/exportTranslationKeys.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe('exportTranslationKeys', () => {
{
'group': 'custom group',
'items': [
'Hello ${0}, Hello ${0}.',
'Hello ${0}, you have ${1} in your bank account.'
]
},
Expand Down Expand Up @@ -101,6 +102,7 @@ describe('exportTranslationKeys', () => {
{
'group': 'custom group',
'items': [
'Hello ${0}, Hello ${0}.',
'Hello ${0}, you have ${1} in your bank account.'
]
},
Expand Down Expand Up @@ -187,6 +189,10 @@ msgstr ""
msgid "\\n <users>\\n \${0}\\n </users>\\n"
msgstr ""
msgctxt "custom group"
msgid "Hello \${0}, Hello \${0}."
msgstr ""
msgctxt "custom group"
msgid "Hello \${0}, you have \${1} in your bank account."
msgstr ""
Expand Down
6 changes: 6 additions & 0 deletions __tests__/generateTranslationSchema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ const expected = {
'custom group': {
'type': 'object',
'properties': {
'Hello ${0}, Hello ${0}.': {
'minLength': 1,
'pattern': '(?=.*?\\$\\{0\\})',
'type': 'string'
},
'Hello ${0}, you have ${1} in your bank account.': {
'type': 'string',
'minLength': 1,
'pattern': '(?=.*?\\$\\{0\\})(?=.*?\\$\\{1\\})'
}
},
'required': [
'Hello ${0}, Hello ${0}.',
'Hello ${0}, you have ${1} in your bank account.'
]
},
Expand Down
4 changes: 2 additions & 2 deletions __tests__/validateTranslations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('validateTranslations', () => {
logger: { toConsole: true }
})
} catch(err) {
expect(err.message).toContain('translation.json has 4 missing translations and 1 invalid key; 60% translated.')
expect(err.message).toContain('translation.json has 4 missing translations and 1 invalid key; 64% translated.')
expect(err.message).toContain('translation.valid.json is valid and 100% translated!')
}
})
Expand All @@ -104,7 +104,7 @@ describe('validateTranslations', () => {
logger: { toConsole: true }
})
} catch(err) {
expect(err.message).toEqual('translation.json has 4 missing translations and 1 invalid key; 60% translated.')
expect(err.message).toEqual('translation.json has 4 missing translations and 1 invalid key; 64% translated.')
}
})

Expand Down
7 changes: 5 additions & 2 deletions lib/traversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ const traverseTemplateExpressions = {
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')
match = match.replace(regExp, `\${${count}}`)
count++
const newMatch = match.replace(regExp, `\${${count}}`)
if(match !== newMatch) {
match = newMatch
count++
}
})
let template = match.replace(/\r\n/g, '\n')
if(template) {
Expand Down
22 changes: 18 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,14 @@ ajv@^4.7.0:
co "^4.6.0"
json-stable-stringify "^1.0.1"

ajv@^5.1.5:
version "5.1.5"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-5.1.5.tgz#8734931b601f00d4feef7c65738d77d1b65d1f68"
ajv@^5.2.0:
version "5.5.2"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-5.5.2.tgz#73b5eeca3fab653e3d3f9422b341ad42205dc965"
dependencies:
co "^4.6.0"
json-stable-stringify "^1.0.1"
fast-deep-equal "^1.0.0"
fast-json-stable-stringify "^2.0.0"
json-schema-traverse "^0.3.0"

align-text@^0.1.1, align-text@^0.1.3:
version "0.1.4"
Expand Down Expand Up @@ -1579,6 +1581,14 @@ [email protected]:
version "1.0.2"
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.0.2.tgz#e1080e0658e300b06294990cc70e1502235fd550"

fast-deep-equal@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-1.1.0.tgz#c053477817c86b51daa853c81e059b733d023614"

fast-json-stable-stringify@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz#d5142c0caee6b1189f87d3a76111064f86c8bbf2"

fast-levenshtein@~2.0.4:
version "2.0.5"
resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.5.tgz#bd33145744519ab1c36c3ee9f31f08e9079b67f2"
Expand Down Expand Up @@ -2542,6 +2552,10 @@ jsesc@~0.5.0:
version "0.5.0"
resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-0.5.0.tgz#e7dee66e35d6fc16f710fe91d5cf69f70f08911d"

json-schema-traverse@^0.3.0:
version "0.3.1"
resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.3.1.tgz#349a6d44c53a51de89b40805c5d5e59b417d3340"

[email protected]:
version "0.2.3"
resolved "https://registry.yarnpkg.com/json-schema/-/json-schema-0.2.3.tgz#b480c892e59a2f05954ce727bd3f2a4e882f9e13"
Expand Down

0 comments on commit 6a33bee

Please sign in to comment.