Skip to content

Commit ad9afef

Browse files
ZauberNerdfkling
authored andcommitted
Allow namespaced imports to be resolved correctly (#109)
Previously react-docgen did not resolve namespaced imports correctly to their importing modules. Example: ```javascript import * as React from 'react'; export default function Button({ text }) { return (<button>{text}</button>); } Button.propTypes = { text: React.PropTypes.string.isRequired, }; ``` The prop `text` would always show up as "custom" with a raw value of "React.PropTypes.string.isRequired" instead of correctly detecting `React.PropTypes.string` to be a part of the imported `React` namespace and correctly detecting the propType as a required string. This commit adds support for `ImportNamespaceSpecifier` to the `resolveToValue` and `resolveToModule` utility functions.
1 parent d1f0b9d commit ad9afef

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

src/utils/__tests__/resolveToModule-test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,13 @@ describe('resolveToModule', () => {
9090
expect(resolveToModule(path)).toBe('Foo');
9191
});
9292

93+
it('resolves ImportNamespaceSpecifier', () => {
94+
var path = parse(`
95+
import * as foo from "Foo";
96+
foo;
97+
`);
98+
expect(resolveToModule(path)).toBe('Foo');
99+
});
100+
93101
});
94102
});

src/utils/__tests__/resolveToValue-test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,15 @@ describe('resolveToValue', () => {
126126
expect(resolveToValue(path).node.type).toBe('ImportDeclaration');
127127
});
128128

129+
it('resolves namespace import references to the import declaration', () => {
130+
var path = parse([
131+
'import * as bar from "Foo"',
132+
'bar;',
133+
].join('\n'));
134+
135+
expect(resolveToValue(path).node.type).toBe('ImportDeclaration');
136+
});
137+
129138
});
130139

131140
});

src/utils/resolveToValue.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ function findScopePath(paths: Array<NodePath>, path: NodePath): ?NodePath {
5151

5252
if (types.ImportDefaultSpecifier.check(parentPath.node) ||
5353
types.ImportSpecifier.check(parentPath.node) ||
54+
types.ImportNamespaceSpecifier.check(parentPath.node) ||
5455
types.VariableDeclarator.check(parentPath.node) ||
5556
types.TypeAlias.check(parentPath.node)
5657
) {
@@ -84,6 +85,7 @@ export default function resolveToValue(path: NodePath): NodePath {
8485
}
8586
} else if (
8687
types.ImportDefaultSpecifier.check(node) ||
88+
types.ImportNamespaceSpecifier.check(node) ||
8789
types.ImportSpecifier.check(node)
8890
) {
8991
return path.parentPath;

0 commit comments

Comments
 (0)