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

Commit

Permalink
Tweak description object support (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericf authored Dec 29, 2016
1 parent 429956c commit 7279eb1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
16 changes: 11 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ export default function ({types: t}) {
// Always trim the Message Descriptor values.
const descriptorValue = evaluatePath(path);

if(typeof descriptorValue === 'string'){
if (typeof descriptorValue === 'string') {
return descriptorValue.trim();
}

return descriptorValue;
}

Expand Down Expand Up @@ -135,10 +136,15 @@ export default function ({types: t}) {
}
}

if (opts.enforceDescriptions && !description) {
throw path.buildCodeFrameError(
'[React Intl] Message must have a `description`.'
);
if (opts.enforceDescriptions) {
if (
!description ||
(typeof description === 'object' && Object.keys(description).length < 1)
) {
throw path.buildCodeFrameError(
'[React Intl] Message must have a `description`.'
);
}
}

let loc;
Expand Down
5 changes: 4 additions & 1 deletion test/fixtures/descriptionsAsObjects/actual.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ export default class Foo extends Component {
<FormattedMessage
id='foo.bar.baz'
defaultMessage='Hello World!'
description={{ text:'The default message.', metadata:'metadata content'}}
description={{
text: 'Something for the translator.',
metadata: 'Additional metadata content.',
}}
/>
);
}
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/descriptionsAsObjects/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
{
"id": "foo.bar.baz",
"description": {
"text": "The default message.",
"metadata": "metadata content"
"text": "Something for the translator.",
"metadata": "Additional metadata content."
},
"defaultMessage": "Hello World!"
}
Expand Down

0 comments on commit 7279eb1

Please sign in to comment.