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

Commit f947a23

Browse files
committed
fix: avoid any for oneOf arrays with identifiers
1 parent 7c70ecb commit f947a23

5 files changed

+36
-31
lines changed

src/types.ts

+27-28
Original file line numberDiff line numberDiff line change
@@ -146,52 +146,51 @@ function getComplexTypeName(ast: AstQuery, propertyAst: any,
146146
}
147147

148148
function getEnumValues(ast: AstQuery, oneOfTypes: any): any[] {
149-
if (oneOfTypes.type === 'Identifier') {
150-
const res = ast.query(`
151-
//VariableDeclarator[
152-
/:id Identifier[@name == '${oneOfTypes.name}']
153-
]
154-
/:init *
155-
`);
156-
if (!res[0]) {
157-
throwWithLocation('Failed to lookup enum values', oneOfTypes);
158-
}
159-
oneOfTypes = res[0];
160-
}
149+
oneOfTypes = resolveIdentifier(ast, oneOfTypes);
150+
161151
if (!oneOfTypes.elements) {
162152
throwWithLocation('Failed to lookup enum values', oneOfTypes);
163153
}
154+
164155
return (oneOfTypes.elements as any[]).map((element: any) => {
165-
// fixme: This are not named references!
156+
element = resolveIdentifier(ast, element);
166157
if (element.type === 'StringLiteral') {
167-
return dom.create.namedTypeReference(`'${element.value}'`);
158+
return dom.type.stringLiteral(element.value);
168159
}
169-
if (element.value) {
170-
return dom.create.namedTypeReference(element.value);
160+
if (element.type === 'NumericLiteral') {
161+
return dom.type.numberLiteral(element.value);
171162
}
172163
return 'any';
173164
});
174165
}
175166

176167
function getShapeProperties(ast: AstQuery, input: any): any[] {
177-
if (input.type === 'Identifier') {
178-
const res = ast.query(`
179-
//VariableDeclarator[
180-
/:id Identifier[@name == '${input.name}']
181-
]
182-
/:init *
183-
`);
184-
if (res[0]) {
185-
return res[0].properties;
186-
}
187-
throwWithLocation('Failed to lookup shape properties', input);
188-
}
168+
input = resolveIdentifier(ast, input);
169+
189170
if (!input.properties) {
190171
throwWithLocation('Failed to lookup shape properties', input);
191172
}
173+
192174
return input.properties;
193175
}
194176

177+
function resolveIdentifier(ast: AstQuery, input: any): any {
178+
if (input.type !== 'Identifier') {
179+
return input;
180+
}
181+
182+
const res = ast.query(`
183+
//VariableDeclarator[
184+
/:id Identifier[@name == '${input.name}']
185+
]
186+
/:init *
187+
`);
188+
if (!res[0]) {
189+
throwWithLocation('Failed to lookup identifier', input);
190+
}
191+
return res[0];
192+
}
193+
195194
function throwWithLocation(message: string, ast: any): never {
196195
const error = new Error(message);
197196
(error as any).loc = ast.loc;

tests/es6-class.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ declare module 'component' {
33

44
import Message from './path/to/Message';
55

6-
export type ComponentOptionalEnum = 'News' | 'Photos' | 1 | 2;
6+
export type ComponentOptionalEnum = "News" | "Photos" | 1 | 2;
77

88
export type ComponentOptionalUnion = string | number;
99

tests/es7-class-babeled-to-es6.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ declare module 'component' {
33

44
import Message from './path/to/Message';
55

6-
export type MyComponentOptionalEnum = 'News' | 'Photos' | 1 | 2;
6+
export type MyComponentOptionalEnum = "News" | "Photos" | 1 | 2;
77

88
export type MyComponentOptionalUnion = string | number;
99

tests/references-in-proptypes.d.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
declare module 'component' {
22
import {Component} from 'react';
33

4-
export type SomeComponentSomeOneOf = 'foo' | 'bar';
4+
export type SomeComponentSomeOneOf = "foo" | "bar";
5+
export type SomeComponentAnotherOneOf = "foo" | "bar";
56

67
export interface SomeComponentSomeShape {
78
string?: string;
89
}
910

1011
export interface SomeComponentProps {
1112
someOneOf?: SomeComponentSomeOneOf;
13+
anotherOneOf?: SomeComponentAnotherOneOf;
1214
someShape?: SomeComponentSomeShape;
1315
}
1416

tests/references-in-proptypes.jsx

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

3+
const FOO = 'foo';
4+
const BAR = 'bar';
35
const fooOrBar = ['foo', 'bar'];
6+
const fooOrBarWithConsts = [FOO, BAR];
47
const shape = { string: React.PropTypes.string };
58

69
export default class SomeComponent extends React.Component {
710
static propTypes = {
811
someOneOf: React.PropTypes.oneOf(fooOrBar),
12+
anotherOneOf: React.PropTypes.oneOf(fooOrBarWithConsts),
913
someShape: React.PropTypes.shape(shape)
1014
};
1115
}

0 commit comments

Comments
 (0)