Skip to content

Commit

Permalink
fix bug where if sourceLocale file has no trailing comma, import will…
Browse files Browse the repository at this point in the history
… end up with malformed object (#26)
  • Loading branch information
u9520107 committed Mar 27, 2018
1 parent 7bfcf94 commit f05c4e3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
19 changes: 5 additions & 14 deletions src/lib/getRawData/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
Expand All @@ -28,17 +28,15 @@ 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(''),
value,
startIdx,
valueStart,
valueEnd,
endIdx: idx,
endIdx: tokens[idx + 1].type === tokTypes.comma ? idx + 1 : idx,
};
}

Expand Down Expand Up @@ -66,21 +64,17 @@ 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 (
token.type === tokTypes._export &&
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 {
Expand All @@ -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;
}
}
Expand All @@ -115,8 +108,6 @@ export async function extractData(localeFile) {
content,
data,
ast,
// dataStartIndex,
// dataEndIndex,
};
}

Expand Down
5 changes: 2 additions & 3 deletions src/lib/importLocale/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -172,7 +172,6 @@ async function mergeToFiles({
mergedData,
annotations,
});
console.log(mergedContent);
await fs.writeFile(path.resolve(sourceFolder, fileName), mergedContent);
}));
}));
Expand Down
25 changes: 25 additions & 0 deletions src/lib/importLocale/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

0 comments on commit f05c4e3

Please sign in to comment.