Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit cc4b6f9

Browse files
authored
fix: handle references for shapes correctly (#262)
1 parent 07bf452 commit cc4b6f9

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

Diff for: src/types.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function getComplexType(ast: AstQuery, propertyAst: any,
8686
const enumEntries = getEnumValues(ast, typeAst.arguments[0]);
8787
return getTypeDeclaration(dom.create.union(enumEntries as dom.Type[]), !required);
8888
case 'shape':
89-
const entries = typeAst.arguments[0].properties.map((entry: any) => {
89+
const entries = getShapeProperties(ast, typeAst.arguments[0]).map((entry: any) => {
9090
const typeDecl = get(ast, entry.value, propTypesName);
9191
return dom.create.property(entry.key.name, typeDecl.type,
9292
typeDecl.optional ? dom.DeclarationFlags.Optional : dom.DeclarationFlags.None);
@@ -151,3 +151,16 @@ function getEnumValues(ast: AstQuery, oneOfTypes: any): any[] {
151151
return 'any';
152152
});
153153
}
154+
155+
function getShapeProperties(ast: AstQuery, input: any): any[] {
156+
if (input.type === 'Identifier') {
157+
const res = ast.query(`
158+
//VariableDeclarator[
159+
/:id Identifier[@name == '${input.name}']
160+
]
161+
/:init *
162+
`);
163+
return res[0].properties;
164+
}
165+
return input.properties;
166+
}

Diff for: tests/references-in-proptypes.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ declare module 'component' {
22
import * as React from 'react';
33
export interface SomeComponentProps {
44
someEnum?: 'foo' | 'bar';
5+
someShape?: {
6+
string?: string;
7+
};
58
}
69
export default class SomeComponent extends React.Component<SomeComponentProps, any>{
710
}

Diff for: tests/references-in-proptypes.jsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import React from 'react';
22

33
const fooOrBar = ['foo', 'bar'];
4+
const shape = { string: React.PropTypes.string };
45

56
export default class SomeComponent extends React.Component {
67
static propTypes = {
7-
someEnum: React.PropTypes.oneOf(fooOrBar)
8+
someEnum: React.PropTypes.oneOf(fooOrBar),
9+
someShape: React.PropTypes.shape(shape)
810
};
911
}

0 commit comments

Comments
 (0)