From 6a33beec57a4fc3537b0f856fa22a07d42e53002 Mon Sep 17 00:00:00 2001 From: Steffen Kolmer Date: Fri, 9 Mar 2018 15:09:07 +0100 Subject: [PATCH] fix(schema): fixed expression index issue in validation schema --- __tests__/data/grouped.js | 1 + __tests__/data/schema.json | 6 +++++ __tests__/data/translation.json | 3 ++- __tests__/data/translation.valid.json | 3 ++- .../translations/translation.valid.1.json | 3 ++- .../data/translations/translation.valid.json | 3 ++- __tests__/exportTranslationKeys.test.js | 6 +++++ __tests__/generateTranslationSchema.test.js | 6 +++++ __tests__/validateTranslations.test.js | 4 ++-- lib/traversal.js | 7 ++++-- yarn.lock | 22 +++++++++++++++---- 11 files changed, 52 insertions(+), 12 deletions(-) diff --git a/__tests__/data/grouped.js b/__tests__/data/grouped.js index 8f0c8c8..d43d90c 100644 --- a/__tests__/data/grouped.js +++ b/__tests__/data/grouped.js @@ -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!`) diff --git a/__tests__/data/schema.json b/__tests__/data/schema.json index 3e81a50..33cdb07 100644 --- a/__tests__/data/schema.json +++ b/__tests__/data/schema.json @@ -22,6 +22,11 @@ "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, @@ -29,6 +34,7 @@ } }, "required": [ + "Hello ${0}, Hello ${0}.", "Hello ${0}, you have ${1} in your bank account." ] }, diff --git a/__tests__/data/translation.json b/__tests__/data/translation.json index 09c468a..7c7c85c 100644 --- a/__tests__/data/translation.json +++ b/__tests__/data/translation.json @@ -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" diff --git a/__tests__/data/translation.valid.json b/__tests__/data/translation.valid.json index 7ea9273..5f32bf7 100644 --- a/__tests__/data/translation.valid.json +++ b/__tests__/data/translation.valid.json @@ -4,7 +4,8 @@ "\n \n ${0}\n \n": "\n \n ${0}\n \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." diff --git a/__tests__/data/translations/translation.valid.1.json b/__tests__/data/translations/translation.valid.1.json index 8b1da3d..8463ddc 100644 --- a/__tests__/data/translations/translation.valid.1.json +++ b/__tests__/data/translations/translation.valid.1.json @@ -4,7 +4,8 @@ "\n \n ${0}\n \n": "\n \n ${0}\n \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." diff --git a/__tests__/data/translations/translation.valid.json b/__tests__/data/translations/translation.valid.json index f9cb9f1..953a112 100644 --- a/__tests__/data/translations/translation.valid.json +++ b/__tests__/data/translations/translation.valid.json @@ -4,7 +4,8 @@ "\n \n ${0}\n \n": "\n \n ${0}\n \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." diff --git a/__tests__/exportTranslationKeys.test.js b/__tests__/exportTranslationKeys.test.js index e8a6cca..2f92179 100644 --- a/__tests__/exportTranslationKeys.test.js +++ b/__tests__/exportTranslationKeys.test.js @@ -43,6 +43,7 @@ describe('exportTranslationKeys', () => { { 'group': 'custom group', 'items': [ + 'Hello ${0}, Hello ${0}.', 'Hello ${0}, you have ${1} in your bank account.' ] }, @@ -101,6 +102,7 @@ describe('exportTranslationKeys', () => { { 'group': 'custom group', 'items': [ + 'Hello ${0}, Hello ${0}.', 'Hello ${0}, you have ${1} in your bank account.' ] }, @@ -187,6 +189,10 @@ msgstr "" msgid "\\n \\n \${0}\\n \\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 "" diff --git a/__tests__/generateTranslationSchema.test.js b/__tests__/generateTranslationSchema.test.js index 2cef14b..c28e1d4 100644 --- a/__tests__/generateTranslationSchema.test.js +++ b/__tests__/generateTranslationSchema.test.js @@ -29,6 +29,11 @@ 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, @@ -36,6 +41,7 @@ const expected = { } }, 'required': [ + 'Hello ${0}, Hello ${0}.', 'Hello ${0}, you have ${1} in your bank account.' ] }, diff --git a/__tests__/validateTranslations.test.js b/__tests__/validateTranslations.test.js index 0f206f7..929b35a 100644 --- a/__tests__/validateTranslations.test.js +++ b/__tests__/validateTranslations.test.js @@ -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!') } }) @@ -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.') } }) diff --git a/lib/traversal.js b/lib/traversal.js index 19f56aa..2c47730 100644 --- a/lib/traversal.js +++ b/lib/traversal.js @@ -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) { diff --git a/yarn.lock b/yarn.lock index 0463410..408dc6a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -1579,6 +1581,14 @@ extsprintf@1.0.2: 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" @@ -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" + json-schema@0.2.3: version "0.2.3" resolved "https://registry.yarnpkg.com/json-schema/-/json-schema-0.2.3.tgz#b480c892e59a2f05954ce727bd3f2a4e882f9e13"