Skip to content

Commit 840ca88

Browse files
committed
Handle namespace imports
1 parent 268b828 commit 840ca88

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

src/__tests__/__snapshots__/main-test.js.snap

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,3 +1562,34 @@ Object {
15621562
},
15631563
}
15641564
`;
1565+
1566+
exports[`main fixtures processes component "component_33.js" without errors 1`] = `
1567+
Object {
1568+
"description": "",
1569+
"displayName": "SuperDuperCustomButton",
1570+
"methods": Array [],
1571+
"props": Object {
1572+
"children": Object {
1573+
"description": "",
1574+
"required": true,
1575+
"type": Object {
1576+
"name": "string",
1577+
},
1578+
},
1579+
"onClick": Object {
1580+
"description": "",
1581+
"required": false,
1582+
"type": Object {
1583+
"name": "func",
1584+
},
1585+
},
1586+
"style": Object {
1587+
"description": "",
1588+
"required": false,
1589+
"type": Object {
1590+
"name": "object",
1591+
},
1592+
},
1593+
},
1594+
}
1595+
`;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as C31 from './component_31';
2+
import PropTypes from 'prop-types';
3+
4+
export function SuperDuperCustomButton({color, ...otherProps}) {
5+
return <C31.SuperCustomButton {...otherProps} style={{color}} />;
6+
}
7+
8+
SuperDuperCustomButton.propTypes = C31.sharedProps;

src/utils/resolveToValue.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,15 @@ function findScopePath(
5252
let resultPath = paths[0];
5353
const parentPath = resultPath.parent;
5454

55+
// Namespace imports are handled separately, at the site of a member expression access
5556
if (
5657
resolveImports &&
5758
(t.ImportDefaultSpecifier.check(parentPath.node) ||
58-
t.ImportSpecifier.check(parentPath.node) ||
59-
t.ImportNamespaceSpecifier.check(parentPath.node))
59+
t.ImportSpecifier.check(parentPath.node))
6060
) {
6161
let exportName;
6262
if (t.ImportDefaultSpecifier.check(parentPath.node)) {
6363
exportName = 'default';
64-
} else if (t.ImportNamespaceSpecifier.check(parentPath.node)) {
65-
exportName = '*';
6664
} else {
6765
exportName = parentPath.node.imported.name;
6866
}
@@ -150,8 +148,9 @@ export default function resolveToValue(
150148
return resolveToValue(path.get('init'), resolveImports);
151149
}
152150
} else if (t.MemberExpression.check(node)) {
153-
const resolved = resolveToValue(
154-
getMemberExpressionRoot(path),
151+
const root = getMemberExpressionRoot(path);
152+
let resolved = resolveToValue(
153+
root,
155154
resolveImports,
156155
);
157156
if (t.ObjectExpression.check(resolved.node)) {
@@ -169,6 +168,18 @@ export default function resolveToValue(
169168
} else if (isSupportedDefinitionType(resolved)) {
170169
const memberPath = getMemberValuePath(resolved, path.node.property.name);
171170
return memberPath || path;
171+
} else if (t.ImportDeclaration.check(resolved.node)) {
172+
// Handle references to namespace imports, e.g. import * as foo from 'bar'.
173+
// Try to find a specifier that matches the root of the member expression, and
174+
// find the export that matches the property name.
175+
for (let specifier of resolved.node.specifiers) {
176+
if (t.ImportNamespaceSpecifier.check(specifier) && specifier.local.name === root.node.name) {
177+
const resolvedPath = resolveImportedValue(resolved, root.parentPath.node.property.name);
178+
if (resolvedPath) {
179+
return resolveToValue(resolvedPath, resolveImports);
180+
}
181+
}
182+
}
172183
}
173184
} else if (
174185
t.ImportDefaultSpecifier.check(node) ||

0 commit comments

Comments
 (0)