Skip to content

Commit 55c1511

Browse files
Morlayfkling
authored andcommitted
add ES6 export declarations support
1 parent 5db49a5 commit 55c1511

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/dist
2+
node_modules/

src/resolver/__tests__/findExportedReactCreateClassCall-test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,31 @@ describe('React documentation parser', () => {
103103

104104
expect(parse(source)).toBeDefined();
105105
});
106+
107+
it('finds React.createClass in default exported', () => {
108+
var source = [
109+
'var React = require("React");',
110+
'var Component = React.createClass({});',
111+
'export default Component'
112+
].join('\n');
113+
114+
expect(parse(source)).toBeDefined();
115+
116+
source = [
117+
'var React = require("React");',
118+
'export default React.createClass({});'
119+
].join('\n');
120+
121+
expect(parse(source)).toBeDefined();
122+
});
123+
124+
it('finds React.createClass in variable exported', () => {
125+
var source = [
126+
'var React = require("React");',
127+
'export var Component = React.createClass({});'
128+
].join('\n');
129+
130+
expect(parse(source)).toBeDefined();
131+
});
132+
106133
});

src/resolver/findExportedReactCreateClassCall.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ function findExportedReactCreateClass(
4747
visitDoWhileStatement: ignore,
4848
visitForStatement: ignore,
4949
visitForInStatement: ignore,
50+
visitExportDeclaration: function(path) {
51+
path = resolveToValue(path.get('declaration'));
52+
if (!isReactCreateClassCall(path)) {
53+
return false
54+
}
55+
var resolvedPath = resolveToValue(path.get('arguments', 0));
56+
if (types.ObjectExpression.check(resolvedPath.node)) {
57+
definition = resolvedPath;
58+
}
59+
this.abort();
60+
},
5061
visitAssignmentExpression: function(path) {
5162
// Ignore anything that is not `exports.X = ...;` or
5263
// `module.exports = ...;`

src/utils/__tests__/resolveToValue-test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,26 @@ describe('resolveToValue', () => {
8686
path.node
8787
)).toBe(true);
8888
});
89+
90+
it('resolves export variable declarator', () => {
91+
var path = parse([
92+
'export var Component = foo()'
93+
].join('\n'));
94+
95+
expect(
96+
resolveToValue(path.parentPath.get('declaration')).node.callee.name
97+
).toBe('foo');
98+
});
99+
100+
it('resolves export default declarator', () => {
101+
var path = parse([
102+
'var Component = foo();',
103+
'export default Component;'
104+
].join('\n'));
105+
106+
expect(
107+
resolveToValue(path.parentPath.get('declaration')).node.callee.name
108+
).toBe('foo');
109+
});
110+
89111
});

src/utils/resolveToValue.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ function buildMemberExpressionFromPattern(path: NodePath): ?Node {
4949
*/
5050
function resolveToValue(path: NodePath): NodePath {
5151
var node = path.node;
52-
if (types.AssignmentExpression.check(node)) {
52+
if (types.VariableDeclaration.check(node)) {
53+
return resolveToValue(path.get('declarations', 0))
54+
} else if (types.VariableDeclarator.check(node)) {
55+
if (node.init) {
56+
return resolveToValue(path.get('init'));
57+
}
58+
} else if (types.AssignmentExpression.check(node)) {
5359
if (node.operator === '=') {
5460
return resolveToValue(node.get('right'));
5561
}

0 commit comments

Comments
 (0)