From 9b088fbb8d00ef797197b420050ddc548bd35b10 Mon Sep 17 00:00:00 2001 From: Soufiane Ghzal Date: Sat, 2 Jun 2018 14:57:53 +0200 Subject: [PATCH 1/4] Add support for ternary expression #39 --- src/extractFromCode.js | 39 ++++++++++++------- src/extractFromCode.spec.js | 39 +++++++++++++++++++ .../ternaryOperator.js | 7 ++++ 3 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 src/extractFromCodeFixtures/ternaryOperator.js diff --git a/src/extractFromCode.js b/src/extractFromCode.js index 42622bb..6d512d7 100644 --- a/src/extractFromCode.js +++ b/src/extractFromCode.js @@ -3,20 +3,27 @@ import traverse from 'babel-traverse'; const noInformationTypes = ['CallExpression', 'Identifier', 'MemberExpression']; -function getKey(node) { +function getKeys(node) { if (node.type === 'StringLiteral') { - return node.value; + return [node.value]; } else if (node.type === 'BinaryExpression' && node.operator === '+') { - return getKey(node.left) + getKey(node.right); + const left = getKeys(node.left); + const right = getKeys(node.right); + if (left.length > 1 || right.length > 1) { + console.warn('Unsupported multiple keys for binary expression, keys skipped.'); // TODO + } + return [left[0] + right[0]]; } else if (node.type === 'TemplateLiteral') { - return node.quasis.map(quasi => quasi.value.cooked).join('*'); + return [node.quasis.map(quasi => quasi.value.cooked).join('*')]; + } else if (node.type === 'ConditionalExpression') { + return [...getKeys(node.consequent), ...getKeys(node.alternate)]; } else if (noInformationTypes.includes(node.type)) { - return '*'; // We can't extract anything. + return ['*']; // We can't extract anything. } console.warn(`Unsupported node: ${node.type}`); - return null; + return [null]; } const commentRegExp = /i18n-extract (.+)/; @@ -78,19 +85,23 @@ export default function extractFromCode(code, options = {}) { return; } - const { callee: { name, type } } = node; + const { + callee: { name, type }, + } = node; if ((type === 'Identifier' && name === marker) || path.get('callee').matchesPattern(marker)) { - const key = getKey( + const foundKeys = getKeys( keyLoc < 0 ? node.arguments[node.arguments.length + keyLoc] : node.arguments[keyLoc], ); - if (key) { - keys.push({ - key, - loc: node.loc, - }); - } + foundKeys.forEach(key => { + if (key) { + keys.push({ + key, + loc: node.loc, + }); + } + }); } }, }); diff --git a/src/extractFromCode.spec.js b/src/extractFromCode.spec.js index 2b25d62..782bc12 100644 --- a/src/extractFromCode.spec.js +++ b/src/extractFromCode.spec.js @@ -646,4 +646,43 @@ describe('#extractFromCode()', () => { ); }); }); + + describe('ternary operator', () => { + it('should parse ternary operator', () => { + const keys = extractFromCode(getCode('ternaryOperator.js')); + + assert.deepEqual( + [ + { + key: '*', + loc: { + end: { + column: 23, + line: 7, + }, + start: { + column: 0, + line: 7, + }, + }, + }, + { + key: 'bar', + loc: { + end: { + column: 23, + line: 7, + }, + start: { + column: 0, + line: 7, + }, + }, + }, + ], + keys, + 'Should return the good keys.', + ); + }); + }); }); diff --git a/src/extractFromCodeFixtures/ternaryOperator.js b/src/extractFromCodeFixtures/ternaryOperator.js new file mode 100644 index 0000000..13c0882 --- /dev/null +++ b/src/extractFromCodeFixtures/ternaryOperator.js @@ -0,0 +1,7 @@ +/* eslint-disable */ + +import i18n from 'i18n'; + +const foo = 'bar'; + +i18n(foo ? foo : 'bar'); \ No newline at end of file From ea5d2a860045b8dfda3bd1faf345758802600580 Mon Sep 17 00:00:00 2001 From: Soufiane Ghzal Date: Sat, 2 Jun 2018 15:04:46 +0200 Subject: [PATCH 2/4] added nested test for ternary operator --- src/extractFromCode.spec.js | 47 +++++++++++++++++-- .../ternaryOperator.js | 6 ++- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/extractFromCode.spec.js b/src/extractFromCode.spec.js index 782bc12..8c80384 100644 --- a/src/extractFromCode.spec.js +++ b/src/extractFromCode.spec.js @@ -658,11 +658,11 @@ describe('#extractFromCode()', () => { loc: { end: { column: 23, - line: 7, + line: 8, }, start: { column: 0, - line: 7, + line: 8, }, }, }, @@ -671,11 +671,50 @@ describe('#extractFromCode()', () => { loc: { end: { column: 23, - line: 7, + line: 8, }, start: { column: 0, - line: 7, + line: 8, + }, + }, + }, + { + key: '*', + loc: { + end: { + column: 50, + line: 11, + }, + start: { + column: 0, + line: 11, + }, + }, + }, + { + key: 'baz', + loc: { + end: { + column: 50, + line: 11, + }, + start: { + column: 0, + line: 11, + }, + }, + }, + { + key: 'bar', + loc: { + end: { + column: 50, + line: 11, + }, + start: { + column: 0, + line: 11, }, }, }, diff --git a/src/extractFromCodeFixtures/ternaryOperator.js b/src/extractFromCodeFixtures/ternaryOperator.js index 13c0882..d48bb15 100644 --- a/src/extractFromCodeFixtures/ternaryOperator.js +++ b/src/extractFromCodeFixtures/ternaryOperator.js @@ -4,4 +4,8 @@ import i18n from 'i18n'; const foo = 'bar'; -i18n(foo ? foo : 'bar'); \ No newline at end of file +// simple +i18n(foo ? foo : 'bar'); + +// nested +i18n(foo ? (foo.length > 1 ? foo : 'baz') : 'bar'); \ No newline at end of file From 552be116aa186e8beaf1fbf88fb5833863087ad5 Mon Sep 17 00:00:00 2001 From: Soufiane Ghzal Date: Sat, 2 Jun 2018 23:28:55 +0200 Subject: [PATCH 3/4] Add support for logical expressions #39 --- src/extractFromCode.js | 10 +++ src/extractFromCode.spec.js | 78 +++++++++++++++++++ .../logicalExpression.js | 14 ++++ 3 files changed, 102 insertions(+) create mode 100644 src/extractFromCodeFixtures/logicalExpression.js diff --git a/src/extractFromCode.js b/src/extractFromCode.js index 6d512d7..5b3c4b2 100644 --- a/src/extractFromCode.js +++ b/src/extractFromCode.js @@ -17,6 +17,16 @@ function getKeys(node) { return [node.quasis.map(quasi => quasi.value.cooked).join('*')]; } else if (node.type === 'ConditionalExpression') { return [...getKeys(node.consequent), ...getKeys(node.alternate)]; + } else if (node.type === 'LogicalExpression') { + switch (node.operator) { + case '&&': + return [...getKeys(node.right)]; + case '||': + return [...getKeys(node.left), ...getKeys(node.right)]; + default: + console.warn(`unsupported logicalExpression's operator: ${node.operator}`); + return [null]; + } } else if (noInformationTypes.includes(node.type)) { return ['*']; // We can't extract anything. } diff --git a/src/extractFromCode.spec.js b/src/extractFromCode.spec.js index 8c80384..47df61a 100644 --- a/src/extractFromCode.spec.js +++ b/src/extractFromCode.spec.js @@ -724,4 +724,82 @@ describe('#extractFromCode()', () => { ); }); }); + + describe('logical expression', () => { + it('should parse logical expressions', () => { + const keys = extractFromCode(getCode('logicalExpression.js')); + + assert.deepEqual( + [ + { + key: 'bar', + loc: { + end: { + column: 18, + line: 8, + }, + start: { + column: 0, + line: 8, + }, + }, + }, + { + key: '*', + loc: { + end: { + column: 18, + line: 11, + }, + start: { + column: 0, + line: 11, + }, + }, + }, + { + key: 'baz', + loc: { + end: { + column: 18, + line: 11, + }, + start: { + column: 0, + line: 11, + }, + }, + }, + { + key: 'bar', + loc: { + end: { + column: 27, + line: 14, + }, + start: { + column: 0, + line: 14, + }, + }, + }, + { + key: 'baz', + loc: { + end: { + column: 27, + line: 14, + }, + start: { + column: 0, + line: 14, + }, + }, + }, + ], + keys, + 'Should return the good keys.', + ); + }); + }); }); diff --git a/src/extractFromCodeFixtures/logicalExpression.js b/src/extractFromCodeFixtures/logicalExpression.js new file mode 100644 index 0000000..c6e85c2 --- /dev/null +++ b/src/extractFromCodeFixtures/logicalExpression.js @@ -0,0 +1,14 @@ +/* eslint-disable */ + +import i18n from 'i18n'; + +const foo = true; + +// && +i18n(foo && 'bar'); + +// || +i18n(foo || 'baz'); + +// mixed +i18n('bar' || foo && 'baz'); \ No newline at end of file From 38c64c0203130fd5f9b115bcac0705e5af897c9b Mon Sep 17 00:00:00 2001 From: Soufiane Ghzal Date: Mon, 4 Jun 2018 12:16:03 +0200 Subject: [PATCH 4/4] fixed eslint --- src/extractFromCode.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/extractFromCode.js b/src/extractFromCode.js index 5b3c4b2..faf64c8 100644 --- a/src/extractFromCode.js +++ b/src/extractFromCode.js @@ -95,9 +95,7 @@ export default function extractFromCode(code, options = {}) { return; } - const { - callee: { name, type }, - } = node; + const { callee: { name, type } } = node; if ((type === 'Identifier' && name === marker) || path.get('callee').matchesPattern(marker)) { const foundKeys = getKeys(