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

Commit 49ba17c

Browse files
authored
Merge pull request #482 from ingdir/stateless-comp
Generate named type instead of function
2 parents 92fd683 + e3a9414 commit 49ba17c

21 files changed

+57
-86
lines changed

src/typings.ts

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@ export function createTypings(moduleName: string|null, programAst: any, options:
4747
const tripleSlashDirectives: dom.TripleSlashDirective[] = [];
4848
const m = dom.create.module(moduleName || 'moduleName');
4949

50-
if (hasReactClass(ast, reactComponentName)) {
51-
m.members.push(dom.create.importNamed(reactComponentName || 'Component', reactImport));
52-
} else {
53-
tripleSlashDirectives.push(dom.create.tripleSlashReferenceTypesDirective('react'));
54-
}
50+
m.members.push(dom.create.importAll('React', reactImport));
5551

5652
if (importStatements.length > 0) {
5753
importStatements.forEach(importStatement => {
@@ -104,18 +100,20 @@ function createExportedTypes(m: dom.ModuleDeclaration, ast: AstQuery, componentN
104100
function createExportedClassComponent(m: dom.ModuleDeclaration, componentName: string,
105101
reactComponentName: string|undefined, exportType: dom.DeclarationFlags, interf: dom.InterfaceDeclaration): void {
106102
const classDecl = dom.create.class(componentName);
107-
classDecl.baseType = dom.create.interface(`${reactComponentName || 'Component'}<${interf.name}, any>`);
103+
classDecl.baseType = dom.create.interface(`React.${reactComponentName || 'Component'}<${interf.name}, any>`);
108104
classDecl.flags = exportType;
109105
classDecl.members.push(dom.create.method('render', [], dom.create.namedTypeReference('JSX.Element')));
110106
m.members.push(classDecl);
111107
}
112108

113109
function createExportedFunctionalComponent(m: dom.ModuleDeclaration, componentName: string, propTypes: any,
114110
exportType: dom.DeclarationFlags, interf: dom.InterfaceDeclaration): void {
115-
const funcDelc = dom.create.function(componentName, propTypes ? [dom.create.parameter('props', interf)] : [],
116-
dom.create.namedTypeReference('JSX.Element'));
117-
funcDelc.flags = exportType;
118-
m.members.push(funcDelc);
111+
112+
const typeDecl = dom.create.alias(
113+
componentName,
114+
dom.create.namedTypeReference(`React.SFC${ propTypes ? `<${interf.name}>` : '' }`));
115+
typeDecl.flags = exportType;
116+
m.members.push(typeDecl);
119117
}
120118

121119
function createPropTypeTypings(interf: dom.InterfaceDeclaration, ast: AstQuery, propTypes: any,
@@ -269,36 +267,6 @@ function getImportedPropTypes(ast: AstQuery): ImportedPropType[] {
269267
}));
270268
}
271269

272-
function hasReactClass(ast: AstQuery, reactComponentName: string|undefined): boolean {
273-
const res = ast.query(`
274-
// ClassDeclaration[
275-
'${reactComponentName}' == 'undefined'
276-
?
277-
/:superClass MemberExpression[
278-
/:object Identifier[@name == 'React'] &&
279-
/:property Identifier[@name == 'Component']
280-
]
281-
:
282-
/:superClass Identifier[@name == '${reactComponentName}']
283-
]
284-
,
285-
// VariableDeclaration
286-
/ VariableDeclarator[
287-
/:init CallExpression[
288-
'${reactComponentName}' == 'undefined'
289-
?
290-
/:arguments MemberExpression[
291-
/:object Identifier[@name == 'React'] &&
292-
/:property Identifier[@name == 'Component']
293-
]
294-
:
295-
/:arguments Identifier[@name == '${reactComponentName}']
296-
]
297-
]
298-
`);
299-
return res.length > 0;
300-
}
301-
302270
function getInstanceOfPropTypes(ast: AstQuery, importedPropTypes: ImportedPropTypes): string[] {
303271
const {propTypesName, propTypes} = importedPropTypes;
304272
const instanceOfPropType = propTypes.find(({importedName}) => importedName === 'instanceOf');

tests/component-without-proptyes.d.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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 type test = React.SFC;
12+
}

tests/es6-class.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
declare module 'component' {
2-
import {Component} from 'react';
2+
import * as React from 'react';
33

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

@@ -52,7 +52,7 @@ declare module 'component' {
5252
requiredSymbol: Symbol;
5353
}
5454

55-
export class Component extends Component<ComponentProps, any> {
55+
export class Component extends React.Component<ComponentProps, any> {
5656
render(): JSX.Element;
5757
}
5858
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
declare module 'component' {
2-
import {Component} from 'react';
2+
import * as React from 'react';
33

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

@@ -52,7 +52,7 @@ declare module 'component' {
5252
requiredSymbol: Symbol;
5353
}
5454

55-
export class MyComponent extends Component<MyComponentProps, any> {
55+
export class MyComponent extends React.Component<MyComponentProps, any> {
5656
render(): JSX.Element;
5757
}
5858
}

tests/es7-class-separate-export.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
declare module 'component' {
2-
import {Component} from 'react';
2+
import * as React from 'react';
33

44
export interface ComponentProps {
55
optionalAny?: any;
66
}
77

8-
export default class Component extends Component<ComponentProps, any> {
8+
export default class Component extends React.Component<ComponentProps, any> {
99
render(): JSX.Element;
1010
}
1111
}

tests/es7-class-top-level-module.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component} from 'react';
1+
import * as React from 'react';
22
import Message from './path/to/Message';
33

44
export type ComponentOptionalUnion = string | number;
@@ -27,6 +27,6 @@ export interface ComponentProps {
2727
requiredArrayOf: string[];
2828
}
2929

30-
export default class Component extends Component<ComponentProps, any> {
30+
export default class Component extends React.Component<ComponentProps, any> {
3131
render(): JSX.Element;
3232
}

tests/es7-class.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
declare module 'component' {
2-
import {Component} from 'react';
2+
import * as React from 'react';
33
import Message from './path/to/Message';
44

55
export type ComponentOptionalUnion = string | number;
@@ -28,7 +28,7 @@ declare module 'component' {
2828
requiredArrayOf: string[];
2929
}
3030

31-
export default class Component extends Component<ComponentProps, any> {
31+
export default class Component extends React.Component<ComponentProps, any> {
3232
render(): JSX.Element;
3333
}
3434
}

tests/import-react-component.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
declare module 'component' {
2-
import {Component} from 'react';
2+
import * as React from 'react';
33
}

0 commit comments

Comments
 (0)