diff --git a/scripts/build-fixtures.js b/scripts/build-fixtures.js index c9c9417..dea605b 100644 --- a/scripts/build-fixtures.js +++ b/scripts/build-fixtures.js @@ -7,6 +7,7 @@ const baseDir = p.resolve(`${__dirname}/../test/fixtures`); const fixtures = [ 'defineMessages', + 'descriptionsAsObjects', ['extractSourceLocation', { extractSourceLocation: true, }], diff --git a/src/index.js b/src/index.js index 40b8cde..4680522 100644 --- a/src/index.js +++ b/src/index.js @@ -52,7 +52,12 @@ export default function ({types: t}) { } // Always trim the Message Descriptor values. - return evaluatePath(path).trim(); + const descriptorValue = evaluatePath(path); + + if(typeof descriptorValue === 'string'){ + return descriptorValue.trim(); + } + return descriptorValue; } function getICUMessageValue(messagePath, {isJSXSource = false} = {}) { diff --git a/test/fixtures/descriptionsAsObjects/actual.js b/test/fixtures/descriptionsAsObjects/actual.js new file mode 100644 index 0000000..543c1d4 --- /dev/null +++ b/test/fixtures/descriptionsAsObjects/actual.js @@ -0,0 +1,14 @@ +import React, {Component} from 'react'; +import {FormattedMessage} from 'react-intl'; + +export default class Foo extends Component { + render() { + return ( + + ); + } +} diff --git a/test/fixtures/descriptionsAsObjects/expected.js b/test/fixtures/descriptionsAsObjects/expected.js new file mode 100644 index 0000000..294f3d2 --- /dev/null +++ b/test/fixtures/descriptionsAsObjects/expected.js @@ -0,0 +1,45 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); + +var _react = require('react'); + +var _react2 = _interopRequireDefault(_react); + +var _reactIntl = require('react-intl'); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } + +function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } + +var Foo = function (_Component) { + _inherits(Foo, _Component); + + function Foo() { + _classCallCheck(this, Foo); + + return _possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).apply(this, arguments)); + } + + _createClass(Foo, [{ + key: 'render', + value: function render() { + return _react2.default.createElement(_reactIntl.FormattedMessage, { + id: 'foo.bar.baz', + defaultMessage: 'Hello World!' + }); + } + }]); + + return Foo; +}(_react.Component); + +exports.default = Foo; diff --git a/test/fixtures/descriptionsAsObjects/expected.json b/test/fixtures/descriptionsAsObjects/expected.json new file mode 100644 index 0000000..2718d31 --- /dev/null +++ b/test/fixtures/descriptionsAsObjects/expected.json @@ -0,0 +1,10 @@ +[ + { + "id": "foo.bar.baz", + "description": { + "text": "The default message.", + "metadata": "metadata content" + }, + "defaultMessage": "Hello World!" + } +]