Skip to content

Commit

Permalink
Ability to parse formatMessage method from intl prop (elastic#21277)
Browse files Browse the repository at this point in the history
add support for extracting default messages from code strings like:

formatMessage();
intl.formatMessage();
props.intl.formatMessage();
this.props.intl.formatMessage();
  • Loading branch information
pavel06081991 authored and cjcenizal committed Aug 21, 2018
1 parent 9fd78e8 commit 7dbfe69
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
23 changes: 18 additions & 5 deletions src/dev/i18n/extract_code_messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,28 @@ import { extractIntlMessages, extractFormattedMessages } from './extract_react_m

/**
* Detect Intl.formatMessage() function call (React).
*
* Example: `intl.formatMessage({ id: 'message-id', defaultMessage: 'Message text' });`
* @param {Object} node
* @returns {boolean}
* @example
* formatMessage({ id: 'message-id', defaultMessage: 'Message text' });
* intl.formatMessage({ id: 'message-id', defaultMessage: 'Message text' });
* props.intl.formatMessage({ id: 'message-id', defaultMessage: 'Message text' });
* this.props.intl.formatMessage({ id: 'message-id', defaultMessage: 'Message text' });
*/
export function isIntlFormatMessageFunction(node) {
return (
isCallExpression(node) &&
isMemberExpression(node.callee) &&
isIdentifier(node.callee.object, { name: 'intl' }) &&
isIdentifier(node.callee.property, { name: 'formatMessage' })
(
isIdentifier(node.callee, { name: 'formatMessage' }) ||
(
isMemberExpression(node.callee) &&
(
isIdentifier(node.callee.object, { name: 'intl' }) ||
isIdentifier(node.callee.object.property, { name: 'intl' })
) &&
isIdentifier(node.callee.property, { name: 'formatMessage' })
)
)
);
}

Expand Down
10 changes: 7 additions & 3 deletions src/dev/i18n/extract_code_messages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ class Component extends PureComponent {
`);

const intlFormatMessageSource = `
intl.formatMessage({ id: 'kbn.mgmt.id-1', defaultMessage: 'Message text 1', context: 'Message context' });
formatMessage({ id: 'kbn.mgmt.id-1', defaultMessage: 'Message text 1', context: 'Message context' });
intl.formatMessage({ id: 'kbn.mgmt.id-2', defaultMessage: 'Message text 2', context: 'Message context' });
props.intl.formatMessage({ id: 'kbn.mgmt.id-5', defaultMessage: 'Message text 5', context: 'Message context' });
this.props.intl.formatMessage({ id: 'kbn.mgmt.id-6', defaultMessage: 'Message text 6', context: 'Message context' });
`;

const formattedMessageSource = `
Expand Down Expand Up @@ -81,11 +84,12 @@ describe('extractCodeMessages', () => {

describe('isIntlFormatMessageFunction', () => {
test('detects intl.formatMessage call expression', () => {
const callExpressioNode = [...traverseNodes(parse(intlFormatMessageSource).program.body)].find(
const callExpressionNodes = [...traverseNodes(parse(intlFormatMessageSource).program.body)].filter(
node => isCallExpression(node)
);

expect(isIntlFormatMessageFunction(callExpressioNode)).toBe(true);
expect(callExpressionNodes).toHaveLength(4);
expect(callExpressionNodes.every(callExpressionNode => isIntlFormatMessageFunction(callExpressionNode))).toBe(true);
});
});

Expand Down

0 comments on commit 7dbfe69

Please sign in to comment.