Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ternary operator and logical expressions #43

Merged
merged 4 commits into from
Jun 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions src/extractFromCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,37 @@ 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 (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.
return ['*']; // We can't extract anything.
}

console.warn(`Unsupported node: ${node.type}`);

return null;
return [null];
}

const commentRegExp = /i18n-extract (.+)/;
Expand Down Expand Up @@ -81,16 +98,18 @@ export default function extractFromCode(code, options = {}) {
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,
});
}
});
}
},
});
Expand Down
156 changes: 156 additions & 0 deletions src/extractFromCode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,4 +646,160 @@ describe('#extractFromCode()', () => {
);
});
});

describe('ternary operator', () => {
it('should parse ternary operator', () => {
const keys = extractFromCode(getCode('ternaryOperator.js'));

assert.deepEqual(
[
{
key: '*',
loc: {
end: {
column: 23,
line: 8,
},
start: {
column: 0,
line: 8,
},
},
},
{
key: 'bar',
loc: {
end: {
column: 23,
line: 8,
},
start: {
column: 0,
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,
},
},
},
],
keys,
'Should return the good keys.',
);
});
});

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.',
);
});
});
});
14 changes: 14 additions & 0 deletions src/extractFromCodeFixtures/logicalExpression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint-disable */

import i18n from 'i18n';

const foo = true;

// &&
i18n(foo && 'bar');

// ||
i18n(foo || 'baz');

// mixed
i18n('bar' || foo && 'baz');
11 changes: 11 additions & 0 deletions src/extractFromCodeFixtures/ternaryOperator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable */

import i18n from 'i18n';

const foo = 'bar';

// simple
i18n(foo ? foo : 'bar');

// nested
i18n(foo ? (foo.length > 1 ? foo : 'baz') : 'bar');