Skip to content
This repository was archived by the owner on May 4, 2020. It is now read-only.

Commit dafa46b

Browse files
author
Long Ho
committed
fix(babel-plugin-react-intl): extract messages without defaultMessage, fix #536
1 parent b264ab2 commit dafa46b

File tree

3 files changed

+63
-17
lines changed

3 files changed

+63
-17
lines changed

packages/babel-plugin-react-intl/src/index.ts

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ export default declare((api: any, options: OptionsSchema) => {
427427
// write `<FormattedMessage {...descriptor} />`, because it will be
428428
// skipped here and extracted elsewhere. The descriptor will
429429
// be extracted only (storeMessage) if a `defaultMessage` prop.
430-
if (descriptorPath.id && descriptorPath.defaultMessage) {
430+
if (descriptorPath.id || descriptorPath.defaultMessage) {
431431
// Evaluate the Message Descriptor values in a JSX
432432
// context, then store it.
433433
const descriptor = evaluateMessageDescriptor(
@@ -444,23 +444,43 @@ export default declare((api: any, options: OptionsSchema) => {
444444
this.ReactIntlMessages
445445
);
446446

447-
attributes.forEach(attr => {
448-
const ketPath = attr.get('name');
449-
const msgDescriptorKey = getMessageDescriptorKey(ketPath);
450-
if (
451-
// Remove description since it's not used at runtime.
452-
msgDescriptorKey === 'description' ||
453-
// Remove defaultMessage if opts says so.
454-
(removeDefaultMessage && msgDescriptorKey === 'defaultMessage')
455-
) {
456-
attr.remove();
457-
} else if (
458-
overrideIdFn &&
459-
getMessageDescriptorKey(ketPath) === 'id'
460-
) {
461-
attr.get('value').replaceWith(t.stringLiteral(descriptor.id));
447+
let idAttr: NodePath<t.JSXAttribute> | undefined;
448+
let descriptionAttr: NodePath<t.JSXAttribute> | undefined;
449+
let defaultMessageAttr: NodePath<t.JSXAttribute> | undefined;
450+
for (const attr of attributes) {
451+
switch (getMessageDescriptorKey(attr.get('name'))) {
452+
case 'description':
453+
descriptionAttr = attr;
454+
break;
455+
case 'defaultMessage':
456+
defaultMessageAttr = attr;
457+
break;
458+
case 'id':
459+
idAttr = attr;
460+
break;
462461
}
463-
});
462+
}
463+
464+
if (descriptionAttr) {
465+
descriptionAttr.remove();
466+
}
467+
468+
if (removeDefaultMessage && defaultMessageAttr) {
469+
defaultMessageAttr.remove();
470+
}
471+
472+
if (overrideIdFn) {
473+
if (idAttr) {
474+
idAttr.get('value').replaceWith(t.stringLiteral(descriptor.id));
475+
} else if (defaultMessageAttr) {
476+
defaultMessageAttr.insertBefore(
477+
t.jsxAttribute(
478+
t.jsxIdentifier('id'),
479+
t.stringLiteral(descriptor.id)
480+
)
481+
);
482+
}
483+
}
464484

465485
// Tag the AST node so we don't try to extract it twice.
466486
tagAsExtracted(path);

packages/babel-plugin-react-intl/test/__snapshots__/index.test.ts.snap

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,11 @@ Object {
278278
"description": undefined,
279279
"id": "TRANSLATION_KEY",
280280
},
281+
Object {
282+
"defaultMessage": undefined,
283+
"description": undefined,
284+
"id": "foo.bar.baz",
285+
},
281286
],
282287
}
283288
`;
@@ -324,6 +329,9 @@ Array [
324329
Object {
325330
"id": "TRANSLATION_KEY",
326331
},
332+
Object {
333+
"id": "foo.bar.baz",
334+
},
327335
]
328336
`;
329337

@@ -491,6 +499,9 @@ class Foo extends _react.Component {
491499
return _react.default.createElement(\\"div\\", null, _react.default.createElement(\\"h1\\", null, _react.default.createElement(_reactIntl.FormattedMessage, msgs.header)), _react.default.createElement(\\"p\\", null, _react.default.createElement(_reactIntl.FormattedMessage, msgs.content)), _react.default.createElement(_reactIntl.FormattedMessage, {
492500
id: \\"HELLO.foo.bar.zoo.12.object\\",
493501
defaultMessage: \\"Hello World!\\"
502+
}), _react.default.createElement(_reactIntl.FormattedMessage, {
503+
id: \\"HELLO..5.object\\",
504+
defaultMessage: \\"NO ID\\"
494505
}), _react.default.createElement(_reactIntl.FormattedHTMLMessage, {
495506
id: \\"HELLO.foo.bar.delta.21.string\\",
496507
defaultMessage: \\"<h1>Hello World!</h1>\\"
@@ -525,6 +536,14 @@ Array [
525536
},
526537
"id": "HELLO.foo.bar.zoo.12.object",
527538
},
539+
Object {
540+
"defaultMessage": "NO ID",
541+
"description": Object {
542+
"metadata": "Additional metadata content.",
543+
"text": "Something for the translator. Another description",
544+
},
545+
"id": "HELLO..5.object",
546+
},
528547
Object {
529548
"defaultMessage": "<h1>Hello World!</h1>",
530549
"description": "The default message.",

packages/babel-plugin-react-intl/test/fixtures/overrideIdFn/actual.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ export default class Foo extends Component {
3939
metadata: 'Additional metadata content.',
4040
}}
4141
/>
42+
<FormattedMessage
43+
defaultMessage="NO ID"
44+
description={{
45+
text: 'Something for the translator. Another description',
46+
metadata: 'Additional metadata content.',
47+
}}
48+
/>
4249
<FormattedHTMLMessage
4350
id="foo.bar.delta"
4451
defaultMessage="<h1>Hello World!</h1>"

0 commit comments

Comments
 (0)