Skip to content
This repository was archived by the owner on Jun 8, 2019. It is now read-only.

Commit 7279eb1

Browse files
authored
Tweak description object support (#88)
1 parent 429956c commit 7279eb1

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

src/index.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ export default function ({types: t}) {
5454
// Always trim the Message Descriptor values.
5555
const descriptorValue = evaluatePath(path);
5656

57-
if(typeof descriptorValue === 'string'){
57+
if (typeof descriptorValue === 'string') {
5858
return descriptorValue.trim();
5959
}
60+
6061
return descriptorValue;
6162
}
6263

@@ -135,10 +136,15 @@ export default function ({types: t}) {
135136
}
136137
}
137138

138-
if (opts.enforceDescriptions && !description) {
139-
throw path.buildCodeFrameError(
140-
'[React Intl] Message must have a `description`.'
141-
);
139+
if (opts.enforceDescriptions) {
140+
if (
141+
!description ||
142+
(typeof description === 'object' && Object.keys(description).length < 1)
143+
) {
144+
throw path.buildCodeFrameError(
145+
'[React Intl] Message must have a `description`.'
146+
);
147+
}
142148
}
143149

144150
let loc;

test/fixtures/descriptionsAsObjects/actual.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ export default class Foo extends Component {
77
<FormattedMessage
88
id='foo.bar.baz'
99
defaultMessage='Hello World!'
10-
description={{ text:'The default message.', metadata:'metadata content'}}
10+
description={{
11+
text: 'Something for the translator.',
12+
metadata: 'Additional metadata content.',
13+
}}
1114
/>
1215
);
1316
}

test/fixtures/descriptionsAsObjects/expected.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
{
33
"id": "foo.bar.baz",
44
"description": {
5-
"text": "The default message.",
6-
"metadata": "metadata content"
5+
"text": "Something for the translator.",
6+
"metadata": "Additional metadata content."
77
},
88
"defaultMessage": "Hello World!"
99
}

0 commit comments

Comments
 (0)