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

Commit 097518e

Browse files
dyatkoKnisterPeter
authored andcommitted
Support const reference
1 parent a7ba12f commit 097518e

9 files changed

+86
-42
lines changed

src/typings.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ function createExportedClassComponent(m: dom.ModuleDeclaration, componentName: s
112112

113113
function createExportedFunctionalComponent(m: dom.ModuleDeclaration, componentName: string, propTypes: any,
114114
exportType: dom.DeclarationFlags, interf: dom.InterfaceDeclaration): void {
115-
const typeDecl = dom.create.namedTypeReference(`React.SFC${ propTypes ? `<${interf.name}>` : '' }`);
115+
const typeDecl = dom.create.namedTypeReference(`React.FC${ propTypes ? `<${interf.name}>` : '' }`);
116116
const constDecl = dom.create.const(componentName, typeDecl);
117117
m.members.push(constDecl);
118118

@@ -383,6 +383,19 @@ function getPropTypes(ast: AstQuery, componentName: string): any|undefined {
383383
return getPropTypes(ast, referencedComponentName);
384384
}
385385

386+
if (propTypes) {
387+
const referencedVariable = ast.query(`
388+
//VariableDeclarator[
389+
/:id Identifier[@name == '${propTypes.name}']
390+
]
391+
/:init *
392+
`);
393+
394+
if (referencedVariable && referencedVariable.length) {
395+
return referencedVariable[0];
396+
}
397+
}
398+
386399
return propTypes;
387400
}
388401

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
declare module 'component' {
2-
import * as React from 'react';
3-
4-
export interface TestProps {
5-
}
6-
7-
export default class Test extends React.Component<TestProps, any> {
8-
render(): JSX.Element;
9-
}
10-
11-
export const test: React.SFC;
12-
}
1+
declare module 'component' {
2+
import * as React from 'react';
3+
4+
export interface TestProps {
5+
}
6+
7+
export default class Test extends React.Component<TestProps, any> {
8+
render(): JSX.Element;
9+
}
10+
11+
export const test: React.FC;
12+
}

tests/const-as-proptypes.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
declare module 'component' {
2+
import * as React from 'react';
3+
4+
export type ButtonButtonSize = "sm" | "md" | "lg";
5+
6+
export interface ButtonProps {
7+
buttonSize?: ButtonButtonSize;
8+
}
9+
10+
const Button: React.FC<ButtonProps>;
11+
12+
export default Button;
13+
14+
}

tests/const-as-proptypes.jsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
const buttonPropTypes = {
5+
buttonSize: PropTypes.oneOf(['sm', 'md', 'lg'])
6+
};
7+
8+
export default function Button({ buttonSize }) {
9+
return (
10+
<button>Test {buttonSize}</button>
11+
);
12+
}
13+
14+
Button.propTypes = buttonPropTypes;

tests/named-export-specifiers.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
declare module 'component' {
22
import * as React from 'react';
3-
3+
44
export interface ComponentProps {
55
optionalAny?: any;
66
}
77

8-
export const Component: React.SFC<ComponentProps>;
8+
export const Component: React.FC<ComponentProps>;
99

10-
export const Component2: React.SFC;
10+
export const Component2: React.FC;
1111
}

tests/parsing-test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,12 @@ test('Parsing should support an SFC with default export', t => {
113113
test('Parsing should support an SFC with default export babeled to es6', t => {
114114
compare(t, 'component', 'stateless-export-as-default.js', 'stateless-export-as-default.d.ts');
115115
});
116-
test('Parsing should suppport components that extend PureComponent', t => {
116+
test('Parsing should support components that extend PureComponent', t => {
117117
compare(t, 'component', 'pure-component.jsx', 'pure-component.d.ts');
118118
});
119+
test('Parsing should support prop-types as reference to constant', t => {
120+
compare(t, 'component', 'const-as-proptypes.jsx', 'const-as-proptypes.d.ts');
121+
});
119122
test('Parsing should suppport custom eol style', t => {
120123
textDiff(
121124
t,

tests/stateless-default-export.d.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
declare module 'component' {
2-
import * as React from 'react';
3-
4-
export interface ComponentProps {
5-
optionalString?: string;
6-
}
7-
8-
const Component: React.SFC<ComponentProps>;
9-
export default Component;
10-
11-
export const Component2: React.SFC;
12-
}
1+
declare module 'component' {
2+
import * as React from 'react';
3+
4+
export interface ComponentProps {
5+
optionalString?: string;
6+
}
7+
8+
const Component: React.FC<ComponentProps>;
9+
export default Component;
10+
11+
export const Component2: React.FC;
12+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
declare module 'component' {
22
import * as React from 'react';
3-
3+
44
export interface ComponentProps {
55
text: string;
66
className?: string;
77
}
88

9-
const Component: React.SFC<ComponentProps>;
9+
const Component: React.FC<ComponentProps>;
1010
export default Component;
1111
}

tests/stateless.d.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
declare module 'component' {
2-
import * as React from 'react';
3-
4-
export interface ComponentProps {
5-
optionalAny?: any;
6-
}
7-
8-
export const Component: React.SFC<ComponentProps>;
9-
10-
export const Component2: React.SFC;
11-
}
1+
declare module 'component' {
2+
import * as React from 'react';
3+
4+
export interface ComponentProps {
5+
optionalAny?: any;
6+
}
7+
8+
export const Component: React.FC<ComponentProps>;
9+
10+
export const Component2: React.FC;
11+
}

0 commit comments

Comments
 (0)