Skip to content

Commit c480dc5

Browse files
committed
Stateless Components: the final test?
1 parent 2b944a3 commit c480dc5

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

src/handlers/__tests__/propTypeHandler-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,16 @@ describe('propTypeHandler', () => {
184184
);
185185
});
186186

187+
describe('stateless component', () => {
188+
test(
189+
propTypesSrc => template(`
190+
var Component = (props) => <div />;
191+
Component.propTypes = ${propTypesSrc};
192+
`),
193+
src => statement(src)
194+
);
195+
});
196+
187197
it('does not error if propTypes cannot be found', () => {
188198
var definition = expression('{fooBar: 42}');
189199
expect(() => propTypeHandler(documentation, definition))

src/utils/getMemberExpressionValuePath.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,15 @@ function resolveName(path) {
3333
return path.get('id', 'name').value
3434
}
3535

36-
if (types.FunctionExpression.check(path.node)) {
37-
console.log('TODO: handle FunctionExpression');
38-
// return path.get('id', 'name').value
36+
if (
37+
types.FunctionExpression.check(path.node) ||
38+
types.ArrowFunctionExpression.check(path.node)
39+
) {
40+
if (!types.VariableDeclarator.check(path.parent.node)) {
41+
return;
42+
}
43+
44+
return path.parent.get('id', 'name').value;
3945
}
4046

4147
throw new TypeError(
@@ -57,13 +63,14 @@ export default function getMemberExpressionValuePath(
5763
variableDefinition: NodePath,
5864
memberName: string
5965
): ?NodePath {
60-
if (typeof memberName === 'undefined') return;
61-
62-
6366
var localName = resolveName(variableDefinition);
6467
var program = getRoot(variableDefinition);
6568

66-
if (!localName) return;
69+
if (!localName) {
70+
// likely an immediately exported and therefore nameless/anonymous node
71+
// passed in
72+
return;
73+
}
6774

6875
var result;
6976
recast.visit(program, {

src/utils/getMemberValuePath.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ var SYNONYMS = {
2323
};
2424

2525
var LOOKUP_METHOD = {
26+
[types.ArrowFunctionExpression.name]: getMemberExpressionValuePath,
27+
[types.FunctionExpression.name]: getMemberExpressionValuePath,
28+
[types.FunctionDeclaration.name]: getMemberExpressionValuePath,
2629
[types.VariableDeclaration.name]: getMemberExpressionValuePath,
2730
[types.ObjectExpression.name]: getPropertyValuePath,
2831
[types.ClassDeclaration.name]: getClassMemberValuePath,
@@ -33,7 +36,12 @@ function isSupportedDefinitionType({node}) {
3336
return types.ObjectExpression.check(node) ||
3437
types.ClassDeclaration.check(node) ||
3538
types.ClassExpression.check(node) ||
36-
types.VariableDeclaration.check(node);
39+
40+
// potential stateless function component
41+
types.VariableDeclaration.check(node) ||
42+
types.ArrowFunctionExpression.check(node) ||
43+
types.FunctionDeclaration.check(node) ||
44+
types.FunctionExpression.check(node);
3745
}
3846

3947
/**
@@ -55,10 +63,11 @@ export default function getMemberValuePath(
5563
): ?NodePath {
5664
if (!isSupportedDefinitionType(componentDefinition)) {
5765
throw new TypeError(
58-
'Got unsupported definition type. Definition must either be an ' +
59-
'ObjectExpression, ClassDeclaration, ClassExpression, or a ' +
60-
'VariableDeclaration. Got "' +
61-
componentDefinition.node.type + '" instead.'
66+
'Got unsupported definition type. Definition must be one of ' +
67+
'ObjectExpression, ClassDeclaration, ClassExpression,' +
68+
'VariableDeclaration, ArrowFunctionExpression, FunctionExpression, or ' +
69+
'FunctionDeclaration. Got "' + componentDefinition.node.type + '" +
70+
'instead.'
6271
);
6372
}
6473

0 commit comments

Comments
 (0)