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

Commit

Permalink
fix: avoid any for oneOf arrays with identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
unstubbable committed Feb 7, 2018
1 parent 7c70ecb commit f947a23
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 31 deletions.
55 changes: 27 additions & 28 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,52 +146,51 @@ function getComplexTypeName(ast: AstQuery, propertyAst: any,
}

function getEnumValues(ast: AstQuery, oneOfTypes: any): any[] {
if (oneOfTypes.type === 'Identifier') {
const res = ast.query(`
//VariableDeclarator[
/:id Identifier[@name == '${oneOfTypes.name}']
]
/:init *
`);
if (!res[0]) {
throwWithLocation('Failed to lookup enum values', oneOfTypes);
}
oneOfTypes = res[0];
}
oneOfTypes = resolveIdentifier(ast, oneOfTypes);

if (!oneOfTypes.elements) {
throwWithLocation('Failed to lookup enum values', oneOfTypes);
}

return (oneOfTypes.elements as any[]).map((element: any) => {
// fixme: This are not named references!
element = resolveIdentifier(ast, element);
if (element.type === 'StringLiteral') {
return dom.create.namedTypeReference(`'${element.value}'`);
return dom.type.stringLiteral(element.value);
}
if (element.value) {
return dom.create.namedTypeReference(element.value);
if (element.type === 'NumericLiteral') {
return dom.type.numberLiteral(element.value);
}
return 'any';
});
}

function getShapeProperties(ast: AstQuery, input: any): any[] {
if (input.type === 'Identifier') {
const res = ast.query(`
//VariableDeclarator[
/:id Identifier[@name == '${input.name}']
]
/:init *
`);
if (res[0]) {
return res[0].properties;
}
throwWithLocation('Failed to lookup shape properties', input);
}
input = resolveIdentifier(ast, input);

if (!input.properties) {
throwWithLocation('Failed to lookup shape properties', input);
}

return input.properties;
}

function resolveIdentifier(ast: AstQuery, input: any): any {
if (input.type !== 'Identifier') {
return input;
}

const res = ast.query(`
//VariableDeclarator[
/:id Identifier[@name == '${input.name}']
]
/:init *
`);
if (!res[0]) {
throwWithLocation('Failed to lookup identifier', input);
}
return res[0];
}

function throwWithLocation(message: string, ast: any): never {
const error = new Error(message);
(error as any).loc = ast.loc;
Expand Down
2 changes: 1 addition & 1 deletion tests/es6-class.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ declare module 'component' {

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

export type ComponentOptionalEnum = 'News' | 'Photos' | 1 | 2;
export type ComponentOptionalEnum = "News" | "Photos" | 1 | 2;

export type ComponentOptionalUnion = string | number;

Expand Down
2 changes: 1 addition & 1 deletion tests/es7-class-babeled-to-es6.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ declare module 'component' {

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

export type MyComponentOptionalEnum = 'News' | 'Photos' | 1 | 2;
export type MyComponentOptionalEnum = "News" | "Photos" | 1 | 2;

export type MyComponentOptionalUnion = string | number;

Expand Down
4 changes: 3 additions & 1 deletion tests/references-in-proptypes.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
declare module 'component' {
import {Component} from 'react';

export type SomeComponentSomeOneOf = 'foo' | 'bar';
export type SomeComponentSomeOneOf = "foo" | "bar";
export type SomeComponentAnotherOneOf = "foo" | "bar";

export interface SomeComponentSomeShape {
string?: string;
}

export interface SomeComponentProps {
someOneOf?: SomeComponentSomeOneOf;
anotherOneOf?: SomeComponentAnotherOneOf;
someShape?: SomeComponentSomeShape;
}

Expand Down
4 changes: 4 additions & 0 deletions tests/references-in-proptypes.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import React from 'react';

const FOO = 'foo';
const BAR = 'bar';
const fooOrBar = ['foo', 'bar'];
const fooOrBarWithConsts = [FOO, BAR];
const shape = { string: React.PropTypes.string };

export default class SomeComponent extends React.Component {
static propTypes = {
someOneOf: React.PropTypes.oneOf(fooOrBar),
anotherOneOf: React.PropTypes.oneOf(fooOrBarWithConsts),
someShape: React.PropTypes.shape(shape)
};
}

0 comments on commit f947a23

Please sign in to comment.