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

Commit a0be64e

Browse files
committed
fix: handle unnamed default export
Closes #350
1 parent 587b02e commit a0be64e

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

Diff for: src/typings.ts

+36-2
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ function getComponentNamesByStaticPropTypeAttribute(ast: AstQuery): string[] {
318318
]
319319
`);
320320
if (res.length > 0) {
321-
return res.map(match => match.id.name);
321+
return res.map(match => match.id ? match.id.name : '');
322322
}
323323
return [];
324324
}
@@ -337,7 +337,7 @@ function getComponentNamesByJsxInBody(ast: AstQuery): string[] {
337337
]
338338
`);
339339
if (res.length > 0) {
340-
return res.map(match => match.id.name);
340+
return res.map(match => match.id ? match.id.name : '');
341341
}
342342
return [];
343343
}
@@ -358,6 +358,19 @@ function getPropTypesFromAssignment(ast: AstQuery, componentName: string): any|u
358358
}
359359

360360
function getPropTypesFromStaticAttribute(ast: AstQuery, componentName: string): any|undefined {
361+
if (componentName === '') {
362+
const res = ast.query(`
363+
//ClassDeclaration
364+
/:body *
365+
//ClassProperty[
366+
/:key Identifier[@name == 'propTypes']
367+
]
368+
/:value*
369+
`);
370+
if (res.length > 0 && !res[0].id) {
371+
return res[0];
372+
}
373+
}
361374
const res = ast.query(`
362375
//ClassDeclaration[
363376
/:id Identifier[@name == '${componentName}']
@@ -375,6 +388,19 @@ function getPropTypesFromStaticAttribute(ast: AstQuery, componentName: string):
375388
}
376389

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

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

Diff for: tests/parsing-test.ts

+3
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+
});

Diff for: tests/unnamed-default-export.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
declare module 'path' {
2+
import * as React from 'react';
3+
4+
export interface Props {
5+
onClick?: (...args: any[]) => any;
6+
}
7+
8+
export default class extends React.Component<Props, any> {
9+
}
10+
}

Diff for: tests/unnamed-default-export.jsx

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {Component, PropTypes} from 'react';
2+
3+
export default class extends Component {
4+
5+
static propTypes = {
6+
onClick: PropTypes.func
7+
};
8+
9+
render() {
10+
return (
11+
<div></div>
12+
);
13+
}
14+
}

0 commit comments

Comments
 (0)