Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ function getComponentNamesByStaticPropTypeAttribute(ast: AstQuery): string[] {
]
`);
if (res.length > 0) {
return res.map(match => match.id.name);
return res.map(match => match.id ? match.id.name : '');
}
return [];
}
Expand All @@ -337,7 +337,7 @@ function getComponentNamesByJsxInBody(ast: AstQuery): string[] {
]
`);
if (res.length > 0) {
return res.map(match => match.id.name);
return res.map(match => match.id ? match.id.name : '');
}
return [];
}
Expand All @@ -358,6 +358,19 @@ function getPropTypesFromAssignment(ast: AstQuery, componentName: string): any|u
}

function getPropTypesFromStaticAttribute(ast: AstQuery, componentName: string): any|undefined {
if (componentName === '') {
const res = ast.query(`
//ClassDeclaration
/:body *
//ClassProperty[
/:key Identifier[@name == 'propTypes']
]
/:value*
`);
if (res.length > 0 && !res[0].id) {
return res[0];
}
}
const res = ast.query(`
//ClassDeclaration[
/:id Identifier[@name == '${componentName}']
Expand All @@ -375,6 +388,19 @@ function getPropTypesFromStaticAttribute(ast: AstQuery, componentName: string):
}

function getComponentExportType(ast: AstQuery, componentName: string): dom.DeclarationFlags|undefined {
if (componentName === '') {
// case: unnamed default export
const res = ast.query(`
// ExportDefaultDeclaration[
// ClassDeclaration
||
// FunctionDeclaration
]
`);
if (res.length > 0 && !res[0].id) {
return dom.DeclarationFlags.ExportDefault;
}
}
let res = ast.query(`
// ExportDefaultDeclaration[
// ClassDeclaration
Expand Down Expand Up @@ -424,6 +450,14 @@ function getComponentExportType(ast: AstQuery, componentName: string): dom.Decla

function isClassComponent(ast: AstQuery, componentName: string,
reactComponentName: string|undefined): boolean {
if (componentName === '') {
const res = ast.query(`
// ClassDeclaration
`);
if (res.length > 0 && !res[0].id) {
return true;
}
}
const res = ast.query(`
// ClassDeclaration
/:id Identifier[@name == '${componentName}']
Expand Down
3 changes: 3 additions & 0 deletions tests/parsing-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,6 @@ test('Parsing should create definition from file without propTypes', t => {
test('Parsing should create definition from file with references in propTypes', t => {
compare(t, 'component', 'references-in-proptypes.jsx', 'references-in-proptypes.d.ts');
});
test('Parsing should create definition from file with unnamed default export', t => {
compare(t, 'path', 'unnamed-default-export.jsx', 'unnamed-default-export.d.ts');
});
10 changes: 10 additions & 0 deletions tests/unnamed-default-export.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
declare module 'path' {
import * as React from 'react';

export interface Props {
onClick?: (...args: any[]) => any;
}

export default class extends React.Component<Props, any> {
}
}
14 changes: 14 additions & 0 deletions tests/unnamed-default-export.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {Component, PropTypes} from 'react';

export default class extends Component {

static propTypes = {
onClick: PropTypes.func
};

render() {
return (
<div></div>
);
}
}