Skip to content

Commit 4f43d93

Browse files
phateddanez
authored andcommitted
Thread importer through resolveToModule
1 parent 5aa0e3d commit 4f43d93

8 files changed

+24
-23
lines changed

src/handlers/propTypeCompositionHandler.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import type { Importer } from '../types';
1818
* It resolves the path to its module name and adds it to the "composes" entry
1919
* in the documentation.
2020
*/
21-
function amendComposes(documentation, path) {
22-
const moduleName = resolveToModule(path);
21+
function amendComposes(documentation, path, importer) {
22+
const moduleName = resolveToModule(path, importer);
2323
if (moduleName) {
2424
documentation.addComposes(moduleName);
2525
}
@@ -32,6 +32,7 @@ function processObjectExpression(documentation, path, importer) {
3232
amendComposes(
3333
documentation,
3434
resolveToValue(propertyPath.get('argument'), importer),
35+
importer
3536
);
3637
break;
3738
}
@@ -57,7 +58,7 @@ export default function propTypeCompositionHandler(
5758
processObjectExpression(documentation, propTypesPath, importer);
5859
break;
5960
default:
60-
amendComposes(documentation, propTypesPath);
61+
amendComposes(documentation, propTypesPath, importer);
6162
break;
6263
}
6364
}

src/handlers/propTypeHandler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import resolveToValue from '../utils/resolveToValue';
1919
import type Documentation from '../Documentation';
2020
import type { Importer } from '../types';
2121

22-
function isPropTypesExpression(path) {
23-
const moduleName = resolveToModule(path);
22+
function isPropTypesExpression(path, importer) {
23+
const moduleName = resolveToModule(path, importer);
2424
if (moduleName) {
2525
return isReactModuleName(moduleName) || moduleName === 'ReactPropTypes';
2626
}
@@ -40,7 +40,7 @@ function amendPropTypes(getDescriptor, path, importer) {
4040

4141
const propDescriptor = getDescriptor(propName);
4242
const valuePath = propertyPath.get('value');
43-
const type = isPropTypesExpression(valuePath)
43+
const type = isPropTypesExpression(valuePath, importer)
4444
? getPropType(valuePath, importer)
4545
: { name: 'custom', raw: printValue(valuePath) };
4646

src/utils/isReactBuiltinCall.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default function isReactBuiltinCall(
2828
}
2929

3030
if (match(path.node, { callee: { property: { name } } })) {
31-
const module = resolveToModule(path.get('callee', 'object'));
31+
const module = resolveToModule(path.get('callee', 'object'), importer);
3232
return Boolean(module && isReactModuleName(module));
3333
}
3434

@@ -47,7 +47,7 @@ export default function isReactBuiltinCall(
4747
specifier => specifier.imported && specifier.imported.name === name,
4848
))
4949
) {
50-
const module = resolveToModule(value);
50+
const module = resolveToModule(value, importer);
5151
return Boolean(module && isReactModuleName(module));
5252
}
5353
}

src/utils/isReactChildrenElementCall.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import { namedTypes as t } from 'ast-types';
1111
import isReactModuleName from './isReactModuleName';
1212
import match from './match';
1313
import resolveToModule from './resolveToModule';
14+
import type { Importer } from '../types';
1415

1516
/**
1617
* Returns true if the expression is a function call of the form
1718
* `React.Children.only(...)`.
1819
*/
19-
export default function isReactChildrenElementCall(path: NodePath): boolean {
20+
export default function isReactChildrenElementCall(path: NodePath, importer: Importer): boolean {
2021
if (t.ExpressionStatement.check(path.node)) {
2122
path = path.get('expression');
2223
}
@@ -26,7 +27,7 @@ export default function isReactChildrenElementCall(path: NodePath): boolean {
2627
}
2728

2829
const calleeObj = path.get('callee', 'object');
29-
const module = resolveToModule(calleeObj);
30+
const module = resolveToModule(calleeObj, importer);
3031

3132
if (!match(calleeObj, { value: { property: { name: 'Children' } } })) {
3233
return false;

src/utils/isReactComponentClass.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default function isReactComponentClass(
4949
match(superClass.node, { property: { name: 'Component' } }) ||
5050
match(superClass.node, { property: { name: 'PureComponent' } })
5151
) {
52-
const module = resolveToModule(superClass);
52+
const module = resolveToModule(superClass, importer);
5353
if (module && isReactModuleName(module)) {
5454
return true;
5555
}

src/utils/isReactCreateClassCall.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ import type { Importer } from '../types';
2020
* createReactClass(...);
2121
* ```
2222
*/
23-
function isReactCreateClassCallModular(path: NodePath): boolean {
23+
function isReactCreateClassCallModular(path: NodePath, importer: Importer): boolean {
2424
if (t.ExpressionStatement.check(path.node)) {
2525
path = path.get('expression');
2626
}
2727

2828
if (!match(path.node, { type: 'CallExpression' })) {
2929
return false;
3030
}
31-
const module = resolveToModule(path);
31+
const module = resolveToModule(path, importer);
3232
return Boolean(module && module === 'create-react-class');
3333
}
3434

@@ -46,6 +46,6 @@ export default function isReactCreateClassCall(
4646
): boolean {
4747
return (
4848
isReactBuiltinCall(path, 'createClass', importer) ||
49-
isReactCreateClassCallModular(path)
49+
isReactCreateClassCallModular(path, importer)
5050
);
5151
}

src/utils/isStatelessComponent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function isJSXElementOrReactCall(path, importer: Importer) {
3232
isReactCreateElementCall(path, importer)) ||
3333
(path.node.type === 'CallExpression' &&
3434
isReactCloneElementCall(path, importer)) ||
35-
(path.node.type === 'CallExpression' && isReactChildrenElementCall(path))
35+
(path.node.type === 'CallExpression' && isReactChildrenElementCall(path, importer))
3636
);
3737
}
3838

src/utils/resolveToModule.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,31 @@
1010
import { namedTypes as t } from 'ast-types';
1111
import match from './match';
1212
import resolveToValue from './resolveToValue';
13-
import ignoreImports from '../importer/ignoreImports';
13+
import type { Importer } from '../types';
1414

1515
/**
1616
* Given a path (e.g. call expression, member expression or identifier),
1717
* this function tries to find the name of module from which the "root value"
1818
* was imported.
1919
*/
20-
export default function resolveToModule(path: NodePath): ?string {
20+
export default function resolveToModule(path: NodePath, importer: Importer): ?string {
2121
const node = path.node;
2222
switch (node.type) {
2323
case t.VariableDeclarator.name:
2424
if (node.init) {
25-
return resolveToModule(path.get('init'));
25+
return resolveToModule(path.get('init'), importer);
2626
}
2727
break;
2828
case t.CallExpression.name:
2929
if (match(node.callee, { type: t.Identifier.name, name: 'require' })) {
3030
return node.arguments[0].value;
3131
}
32-
return resolveToModule(path.get('callee'));
32+
return resolveToModule(path.get('callee'), importer);
3333
case t.Identifier.name:
3434
case t.JSXIdentifier.name: {
35-
// TODO: Do we want to resolve imports here?
36-
const valuePath = resolveToValue(path, ignoreImports);
35+
const valuePath = resolveToValue(path, importer);
3736
if (valuePath !== path) {
38-
return resolveToModule(valuePath);
37+
return resolveToModule(valuePath, importer);
3938
}
4039
break;
4140
}
@@ -46,7 +45,7 @@ export default function resolveToModule(path: NodePath): ?string {
4645
path = path.get('object');
4746
}
4847
if (path) {
49-
return resolveToModule(path);
48+
return resolveToModule(path, importer);
5049
}
5150
}
5251

0 commit comments

Comments
 (0)