Skip to content

Commit

Permalink
Add support for $ReadOnlyArray closes #202 (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mayank1791989 authored and brigand committed Jul 30, 2018
1 parent 46a57f9 commit 30b738e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
37 changes: 37 additions & 0 deletions src/__tests__/__snapshots__/readonly-array.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`generic-array 1`] = `
"'use strict';
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
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 ReadOnlyArrayTest = function (_React$Component) {
_inherits(ReadOnlyArrayTest, _React$Component);
function ReadOnlyArrayTest() {
_classCallCheck(this, ReadOnlyArrayTest);
return _possibleConstructorReturn(this, (ReadOnlyArrayTest.__proto__ || Object.getPrototypeOf(ReadOnlyArrayTest)).apply(this, arguments));
}
return ReadOnlyArrayTest;
}(_react2.default.Component);
ReadOnlyArrayTest.propTypes = {
foo: _propTypes2.default.arrayOf(_propTypes2.default.number.isRequired).isRequired
};"
`;
19 changes: 19 additions & 0 deletions src/__tests__/readonly-array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const babel = require('babel-core');
const content = `
import React from 'react';
type Props = {
foo: $ReadOnlyArray<number>,
};
class ReadOnlyArrayTest extends React.Component<Props> {}
`;

it('generic-array', () => {
const res = babel.transform(content, {
babelrc: false,
presets: ['es2015', 'stage-1', 'react'],
plugins: ['syntax-flow', require('../')],
}).code;
expect(res).toMatchSnapshot();
});
2 changes: 1 addition & 1 deletion src/convertToPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export default function convertToPropTypes(node, importedTypes, internalTypes) {
};
}
else if (node.type === 'GenericTypeAnnotation' || node.type === 'ArrayTypeAnnotation') {
if (node.type === 'ArrayTypeAnnotation' || node.id.name === 'Array') {
if (node.type === 'ArrayTypeAnnotation' || node.id.name === 'Array' || node.id.name === '$ReadOnlyArray') {
let arrayType;
if (node.type === 'ArrayTypeAnnotation') {
arrayType = node.elementType;
Expand Down

0 comments on commit 30b738e

Please sign in to comment.