Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple components in the same file issues #17

Merged
merged 3 commits into from
Nov 1, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 33 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,48 @@ export default function ({types: t}) {
return {
visitor: {
Class(path, state) {
if(isReactComponentClass(path)) {
const className = path.node.id.name;
injectReactDocgenInfo(className, path, state, this.file.code, t);
if(!isReactComponentClass(path)) {
return;
}
const className = path.node.id.name;

if(!isExported(path, className, t)){
return;
}
injectReactDocgenInfo(className, path, state, this.file.code, t);
},
'FunctionDeclaration|FunctionExpression|ArrowFunctionExpression'(path, state) {
if(isStatelessComponent(path)) {
let className = '';
if(path.parentPath.node.id) {
className = path.parentPath.node.id.name;
} else {
return;
}
injectReactDocgenInfo(className, path, state, this.file.code, t);
if(!isStatelessComponent(path)) {
return;
}
if(!path.parentPath.node.id) {
return;
}
const className = path.parentPath.node.id.name;

if(!isExported(path, className, t)) {
return;
}
injectReactDocgenInfo(className, path, state, this.file.code, t);
},
}
};
}

function isExported(path, className, t){
const types = [
'ExportDeclaration',
'ExportNamedDeclaration',
'ExportDefaultDeclaration'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume we only need to check for the default declaration. Isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

React docgen will process the file if there is only one named declaration(multiple named declarations fail). So we have to check for that too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it.

];
const program = path.scope.getProgramParent().path;
return program.get('body').some(path=>{
if(types.some(type=> type == path.node.type)) {
return className === path.node.declaration.name;
}
});
}

function alreadyVisited(program, t) {
return program.node.body.some(node => {
if(t.isExpressionStatement(node) &&
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/case5/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React, { PropTypes } from 'react'

const Second = () => (
<div>Sample</div>
)

const First = ({ children }) => (
<div>
{ children }
<Second />
</div>
)

First.propTypes = {
children: PropTypes.node
}

export default First
Empty file added test/fixtures/case5/expected.js
Empty file.