Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly handle Function and TSMethodSignature TS interfaces and type literals #2048

Merged
merged 3 commits into from
Jun 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ No public interface changes since `12.0.0`.
- Fixed `EuiCallOut` header icon alignment ([#2006](https://github.com/elastic/eui/pull/2006))
- Fixed `EuiInMemoryTable` sort value persistence through lifecycle updates ([#2035](https://github.com/elastic/eui/pull/2035))
- Fixed `EuiColorPicker` positioning and keyboard navigation in certain portal contexts ([#2038](https://github.com/elastic/eui/pull/2038))
- Fixed proptype for `EuiCopy`'s `children` ([#2048](https://github.com/elastic/eui/pull/2048))

**Breaking changes**

Expand Down
13 changes: 11 additions & 2 deletions scripts/babel/proptypes-from-ts-props/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ function resolveIdentifierToPropTypes(node, state) {

if (identifier.name === 'Array') return resolveArrayToPropTypes(node, state);
if (identifier.name === 'MouseEventHandler') return buildPropTypePrimitiveExpression(types, 'func');
if (identifier.name === 'Function') return buildPropTypePrimitiveExpression(types, 'func');
if (identifier.name === 'ExclusiveUnion') {
// We use ExclusiveUnion at the top level to exclusively discriminate between types
// propTypes itself must be an object so merge the union sets together as an intersection
Expand Down Expand Up @@ -453,9 +454,13 @@ function getPropTypesForNode(node, optional, state) {
// which don't translate to prop types.
.filter(property => property.key != null)
.map(property => {
const propertyPropType = property.type === 'TSMethodSignature'
? getPropTypesForNode({ type: 'TSFunctionType' }, property.optional, state)
: getPropTypesForNode(property.typeAnnotation, property.optional, state);

const objectProperty = types.objectProperty(
types.identifier(property.key.name || `"${property.key.value}"`),
getPropTypesForNode(property.typeAnnotation, property.optional, state)
propertyPropType
);
if (property.leadingComments != null) {
objectProperty.leadingComments = property.leadingComments.map(({ type, value }) => ({ type, value }));
Expand Down Expand Up @@ -507,9 +512,13 @@ function getPropTypesForNode(node, optional, state) {
// skip TS index signatures
if (types.isTSIndexSignature(property)) return null;

const propertyPropType = property.type === 'TSMethodSignature'
? getPropTypesForNode({ type: 'TSFunctionType' }, property.optional, state)
: getPropTypesForNode(property.typeAnnotation, property.optional, state);

const objectProperty = types.objectProperty(
types.identifier(property.key.name || `"${property.key.value}"`),
getPropTypesForNode(property.typeAnnotation, property.optional, state)
propertyPropType
);
if (property.leadingComments != null) {
objectProperty.leadingComments = property.leadingComments.map(({ type, value }) => ({ type, value }));
Expand Down
66 changes: 66 additions & 0 deletions scripts/babel/proptypes-from-ts-props/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,72 @@ FooComponent.propTypes = {

});

describe('function propTypes', () => {

it('understands function props on interfaces', () => {
const result = transform(
`
import React from 'react';
interface IFooProps {
foo(): ReactElement;
bar?(arg: number): string;
fizz: Function;
buzz?: (arg: boolean) => string;
}
const FooComponent: React.SFC<IFooProps> = () => {
return (<div>Hello World</div>);
}`,
babelOptions
);

expect(result.code).toBe(`import React from 'react';
import PropTypes from "prop-types";

const FooComponent = () => {
return <div>Hello World</div>;
};

FooComponent.propTypes = {
foo: PropTypes.func.isRequired,
bar: PropTypes.func,
fizz: PropTypes.func.isRequired,
buzz: PropTypes.func
};`);
});

it('understands function props on types', () => {
const result = transform(
`
import React from 'react';
type FooProps = {
foo(): ReactElement;
bar?(arg: number): string;
fizz: Function;
buzz?: (arg: boolean) => string;
}
const FooComponent: React.SFC<FooProps> = () => {
return (<div>Hello World</div>);
}`,
babelOptions
);

expect(result.code).toBe(`import React from 'react';
import PropTypes from "prop-types";

const FooComponent = () => {
return <div>Hello World</div>;
};

FooComponent.propTypes = {
foo: PropTypes.func.isRequired,
bar: PropTypes.func,
fizz: PropTypes.func.isRequired,
buzz: PropTypes.func
};`);
});

});

describe('enum / oneOf propTypes', () => {

describe('union type', () => {
Expand Down