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

Commit 150a65f

Browse files
authored
Merge branch 'master' into named-imports
2 parents 66f47ce + b525016 commit 150a65f

12 files changed

+73
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"babel-generator": "6.24.0",
6666
"babylon": "6.16.1",
6767
"chalk": "1.1.3",
68-
"dts-dom": "0.1.17",
68+
"dts-dom": "0.1.18",
6969
"get-stdin": "5.0.1",
7070
"meow": "3.7.0",
7171
"pascal-case": "2.0.1",

src/typings.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ function createExportedTypes(m: dom.ModuleDeclaration, ast: AstQuery, componentN
8282
const classDecl = dom.create.class(componentName);
8383
classDecl.baseType = dom.create.interface(`Component<${interf.name}, any>`);
8484
classDecl.flags = exportType;
85+
classDecl.members.push(dom.create.method('render', [], dom.create.namedTypeReference('JSX.Element')));
8586
m.members.push(classDecl);
8687
} else {
8788
const funcDelc = dom.create.function(componentName, propTypes ? [dom.create.parameter('props', interf)] : [],
@@ -318,7 +319,7 @@ function getComponentNamesByStaticPropTypeAttribute(ast: AstQuery): string[] {
318319
]
319320
`);
320321
if (res.length > 0) {
321-
return res.map(match => match.id.name);
322+
return res.map(match => match.id ? match.id.name : '');
322323
}
323324
return [];
324325
}
@@ -337,7 +338,7 @@ function getComponentNamesByJsxInBody(ast: AstQuery): string[] {
337338
]
338339
`);
339340
if (res.length > 0) {
340-
return res.map(match => match.id.name);
341+
return res.map(match => match.id ? match.id.name : '');
341342
}
342343
return [];
343344
}
@@ -358,6 +359,19 @@ function getPropTypesFromAssignment(ast: AstQuery, componentName: string): any|u
358359
}
359360

360361
function getPropTypesFromStaticAttribute(ast: AstQuery, componentName: string): any|undefined {
362+
if (componentName === '') {
363+
const res = ast.query(`
364+
//ClassDeclaration
365+
/:body *
366+
//ClassProperty[
367+
/:key Identifier[@name == 'propTypes']
368+
]
369+
/:value*
370+
`);
371+
if (res.length > 0 && !res[0].id) {
372+
return res[0];
373+
}
374+
}
361375
const res = ast.query(`
362376
//ClassDeclaration[
363377
/:id Identifier[@name == '${componentName}']
@@ -375,6 +389,19 @@ function getPropTypesFromStaticAttribute(ast: AstQuery, componentName: string):
375389
}
376390

377391
function getComponentExportType(ast: AstQuery, componentName: string): dom.DeclarationFlags|undefined {
392+
if (componentName === '') {
393+
// case: unnamed default export
394+
const res = ast.query(`
395+
// ExportDefaultDeclaration[
396+
// ClassDeclaration
397+
||
398+
// FunctionDeclaration
399+
]
400+
`);
401+
if (res.length > 0 && !res[0].id) {
402+
return dom.DeclarationFlags.ExportDefault;
403+
}
404+
}
378405
let res = ast.query(`
379406
// ExportDefaultDeclaration[
380407
// ClassDeclaration
@@ -424,6 +451,14 @@ function getComponentExportType(ast: AstQuery, componentName: string): dom.Decla
424451

425452
function isClassComponent(ast: AstQuery, componentName: string,
426453
reactComponentName: string|undefined): boolean {
454+
if (componentName === '') {
455+
const res = ast.query(`
456+
// ClassDeclaration
457+
`);
458+
if (res.length > 0 && !res[0].id) {
459+
return true;
460+
}
461+
}
427462
const res = ast.query(`
428463
// ClassDeclaration
429464
/:id Identifier[@name == '${componentName}']

tests/component-without-proptyes.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ declare module 'component' {
55
}
66

77
export default class Test extends Component<TestProps, any> {
8+
render(): JSX.Element;
89
}
910

1011
export function test(): JSX.Element;

tests/es6-class.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@ declare module 'component' {
5353
}
5454

5555
export class Component extends Component<ComponentProps, any> {
56+
render(): JSX.Element;
5657
}
5758
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ declare module 'component' {
66
}
77

88
export default class Component extends Component<ComponentProps, any> {
9+
render(): JSX.Element;
910
}
1011
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ export interface ComponentProps {
2828
}
2929

3030
export default class Component extends Component<ComponentProps, any> {
31+
render(): JSX.Element;
3132
}

tests/es7-class.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ declare module 'component' {
2929
}
3030

3131
export default class Component extends Component<ComponentProps, any> {
32+
render(): JSX.Element;
3233
}
3334
}

tests/instance-of-proptype-names.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ declare module 'component' {
77
}
88

99
export class Test extends Component<TestProps, any> {
10+
render(): JSX.Element;
1011
}
1112
}

tests/parsing-test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,6 @@ test('Parsing should create definition from file without propTypes', t => {
8282
test('Parsing should create definition from file with references in propTypes', t => {
8383
compare(t, 'component', 'references-in-proptypes.jsx', 'references-in-proptypes.d.ts');
8484
});
85+
test('Parsing should create definition from file with unnamed default export', t => {
86+
compare(t, 'path', 'unnamed-default-export.jsx', 'unnamed-default-export.d.ts');
87+
});

tests/references-in-proptypes.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ declare module 'component' {
1313
}
1414

1515
export default class SomeComponent extends Component<SomeComponentProps, any> {
16+
render(): JSX.Element;
1617
}
1718
}

0 commit comments

Comments
 (0)