Skip to content

Commit f847bd7

Browse files
Jmeyeringfkling
authored andcommitted
Allow getMemberValuePath to get data from a TaggedTemplateExpression (#198)
1 parent c9b36f3 commit f847bd7

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

src/utils/__tests__/getMemberExpressionValuePath-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,15 @@ describe('getMemberExpressionValuePath', () => {
6262
expect(getMemberExpressionValuePath(def, 'imComputed')).not.toBeDefined();
6363
});
6464
});
65+
describe('TaggedTemplateLiteral', () => {
66+
it('finds "normal" property definitions', () => {
67+
var def = statement(`
68+
var Foo = foo\`bar\`
69+
Foo.propTypes = {};
70+
`);
71+
72+
expect(getMemberExpressionValuePath(def, 'propTypes'))
73+
.toBe(def.parent.get('body', 1, 'expression', 'right'));
74+
});
75+
});
6576
});

src/utils/__tests__/getMemberValuePath-test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212

1313
jest.mock('../getPropertyValuePath');
1414
jest.mock('../getClassMemberValuePath');
15+
jest.mock('../getMemberExpressionValuePath');
1516

1617
import {expression, statement} from '../../../tests/utils';
1718

1819
import getPropertyValuePath from '../getPropertyValuePath';
1920
import getClassMemberValuePath from '../getClassMemberValuePath';
2021
import getMemberValuePath from '../getMemberValuePath';
22+
import getMemberExpressionValuePath from '../getMemberExpressionValuePath';
2123

2224
describe('getMemberValuePath', () => {
2325

@@ -35,6 +37,13 @@ describe('getMemberValuePath', () => {
3537
expect(getClassMemberValuePath).toBeCalledWith(path, 'foo');
3638
});
3739

40+
it('handles TaggedTemplateLiterals', () => {
41+
var path = expression('foo``');
42+
43+
getMemberValuePath(path, 'foo');
44+
expect(getMemberExpressionValuePath).toBeCalledWith(path, 'foo');
45+
});
46+
3847
it('handles ClassExpressions', () => {
3948
var path = expression('class {}');
4049

src/utils/getMemberExpressionValuePath.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ function resolveName(path) {
3636

3737
if (
3838
types.FunctionExpression.check(path.node) ||
39-
types.ArrowFunctionExpression.check(path.node)
39+
types.ArrowFunctionExpression.check(path.node) ||
40+
types.TaggedTemplateExpression.check(path.node)
4041
) {
4142
if (!types.VariableDeclarator.check(path.parent.node)) {
4243
return; // eslint-disable-line consistent-return

src/utils/getMemberValuePath.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ function isSupportedDefinitionType({node}) {
3737
types.ClassDeclaration.check(node) ||
3838
types.ClassExpression.check(node) ||
3939

40+
/**
41+
* Adds support for libraries such as
42+
* [styled components]{@link https://github.com/styled-components} that use
43+
* TaggedTemplateExpression's to generate components.
44+
*
45+
* While react-docgen's built-in resolvers do not support resolving
46+
* TaggedTemplateExpression definitiona, third-party resolvers (such as
47+
* https://github.com/Jmeyering/react-docgen-annotation-resolver) could be
48+
* used to add these definitions.
49+
*/
50+
types.TaggedTemplateExpression.check(node) ||
51+
4052
// potential stateless function component
4153
types.VariableDeclaration.check(node) ||
4254
types.ArrowFunctionExpression.check(node) ||
@@ -65,13 +77,14 @@ export default function getMemberValuePath(
6577
throw new TypeError(
6678
'Got unsupported definition type. Definition must be one of ' +
6779
'ObjectExpression, ClassDeclaration, ClassExpression,' +
68-
'VariableDeclaration, ArrowFunctionExpression, FunctionExpression, or ' +
69-
'FunctionDeclaration. Got "' + componentDefinition.node.type + '"' +
70-
'instead.'
80+
'VariableDeclaration, ArrowFunctionExpression, FunctionExpression, ' +
81+
'TaggedTemplateExpression or FunctionDeclaration. Got "' +
82+
componentDefinition.node.type + '"' + 'instead.'
7183
);
7284
}
7385

74-
var lookupMethod = LOOKUP_METHOD[componentDefinition.node.type];
86+
var lookupMethod = LOOKUP_METHOD[componentDefinition.node.type]
87+
|| getMemberExpressionValuePath;
7588
var result = lookupMethod(componentDefinition, memberName);
7689
if (!result && SYNONYMS[memberName]) {
7790
return lookupMethod(componentDefinition, SYNONYMS[memberName]);

0 commit comments

Comments
 (0)