From f05c4e3e51507a2e696b7ddc269596eb5584bb14 Mon Sep 17 00:00:00 2001 From: Jack Tzu-Chieh Huang Date: Tue, 27 Mar 2018 10:04:38 +0800 Subject: [PATCH] fix bug where if sourceLocale file has no trailing comma, import will end up with malformed object (#26) --- src/lib/getRawData/index.js | 19 +++++-------------- src/lib/importLocale/index.js | 5 ++--- src/lib/importLocale/index.test.js | 25 +++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/lib/getRawData/index.js b/src/lib/getRawData/index.js index 40c932b..9bc9e08 100644 --- a/src/lib/getRawData/index.js +++ b/src/lib/getRawData/index.js @@ -15,11 +15,11 @@ export function parseLine(tokens, startIdx) { token = tokens[idx]; } while (token.type !== tokTypes.colon); const valueArray = []; - idx += 1; - token = tokens[idx]; - const valueStart = idx; + const valueStart = idx + 1; let valueEnd; do { + idx += 1; + token = tokens[idx]; if ( token.type === tokTypes.backQuote ) { @@ -28,9 +28,7 @@ export function parseLine(tokens, startIdx) { valueArray.push(typeof token.value !== 'undefined' ? token.value : token.type.label); } valueEnd = idx; - idx += 1; - token = tokens[idx]; - } while (token.type !== tokTypes.comma && token.type !== tokTypes.braceR); + } while (tokens[idx + 1].type !== tokTypes.comma && tokens[idx + 1].type !== tokTypes.braceR); const value = valueArray.join(''); return { key: keyArray.join(''), @@ -38,7 +36,7 @@ export function parseLine(tokens, startIdx) { startIdx, valueStart, valueEnd, - endIdx: idx, + endIdx: tokens[idx + 1].type === tokTypes.comma ? idx + 1 : idx, }; } @@ -66,8 +64,6 @@ export async function extractData(localeFile) { const len = ast.tokens.length; let capturing = false; const data = {}; - // let dataStartIndex = null; - // let dataEndIndex = null; while (idx < len) { const token = ast.tokens[idx]; if ( @@ -75,12 +71,10 @@ export async function extractData(localeFile) { ast.tokens[idx + 1].type === tokTypes._default && ast.tokens[idx + 2].type === tokTypes.braceL ) { - // dataStartIndex = ast.tokens[idx + 2].end; capturing = true; idx += 3; } else if (capturing) { if (token.type === tokTypes.braceR) { - // dataEndIndex = token.start; break; } else { const { @@ -103,7 +97,6 @@ export async function extractData(localeFile) { if (ast.tokens[endIdx].type !== tokTypes.braceR) { idx = endIdx + 1; } else { - // dataEndIndex = ast.tokens[endIdx].start; break; } } @@ -115,8 +108,6 @@ export async function extractData(localeFile) { content, data, ast, - // dataStartIndex, - // dataEndIndex, }; } diff --git a/src/lib/importLocale/index.js b/src/lib/importLocale/index.js index ba89d4b..b27210f 100644 --- a/src/lib/importLocale/index.js +++ b/src/lib/importLocale/index.js @@ -77,7 +77,7 @@ function generateMergedContent({ annotations, }) { const entries = Object.keys(mergedData) - .map(key => mergedData[key]).sort((a, b) => a.valueStart - b.valueStart); + .map(key => mergedData[key]).sort((a, b) => a.valueStart - b.valueStart); let offset = 0; let output = content; entries.forEach((item) => { @@ -142,7 +142,7 @@ async function mergeToFiles({ console.log(`[import-locale] ${chalk.red('{Delete}')} Key: '[${key}]', Reason: Source no longer exist.`); return; } - if (!sourceData[key].value !== original[key].source) { + if (sourceData[key].value !== original[key].source) { console.log(`[import-locale] ${chalk.red('{Delete}')} Key: '[${key}]', Reason: Source value changed.`); return; } @@ -172,7 +172,6 @@ async function mergeToFiles({ mergedData, annotations, }); - console.log(mergedContent); await fs.writeFile(path.resolve(sourceFolder, fileName), mergedContent); })); })); diff --git a/src/lib/importLocale/index.test.js b/src/lib/importLocale/index.test.js index d8f5673..863ad6d 100644 --- a/src/lib/importLocale/index.test.js +++ b/src/lib/importLocale/index.test.js @@ -138,4 +138,29 @@ describe('importLocale', () => { expect(json.whisky).to.equal(undefined); expect(json.testKey).to.equal(undefined); }); + it('it should work for files without trailing comma', async () => { + await fs.writeFile(path.resolve(testFolder, 'en-US.js'), ` + const obj = { + key: 'testKey', + }; + + export default { + modern: 'rogue', + whisky: 'Vault', + [obj.key]: 'testValue', + newline: 'containes\\nnewline', + 'single-quote': 'Single Quote', + "double-'quote'": "Double Quote", + newKey: 'newKey' + }; + `); + await exportLocale(config); + await importLocale(config); + const filePath = path.resolve(testFolder, 'en-GB.js'); + expect(await fs.exists(filePath)).to.equal(true); + const content = await fs.readFile(filePath, 'utf8'); + expect(() => { + const json = eval(transform(content, babelOptions).code); + }).to.not.throw(); + }); });