Skip to content

fix: avoid circular reference when resolving name from within assignment #434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/handlers/__tests__/flowTypeHandler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,5 +368,25 @@ describe('flowTypeHandler', () => {
},
});
});

it('resolves when the function is rebound and not inline', () => {
const src = `
import React from 'react';
type Props = { foo: string };
let Component = (props: Props, ref) => <div ref={ref}>{props.foo}</div>;
Component = React.forwardRef(Component);
`;
flowTypeHandler(
documentation,
parse(src).get('body', 3, 'expression', 'right'),
);
expect(documentation.descriptors).toEqual({
foo: {
flowType: {},
required: true,
description: '',
},
});
});
});
});
20 changes: 20 additions & 0 deletions src/utils/__tests__/resolveToValue-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,26 @@ describe('resolveToValue', () => {

expect(resolveToValue(path)).toEqualASTNode(builders.literal(42));
});

it('resolves to other assigned value if ref is in an assignment lhs', () => {
const path = parsePath(
['var foo;', 'foo = 42;', 'foo = wrap(foo);'].join('\n'),
);

expect(resolveToValue(path.get('left'))).toEqualASTNode(
builders.literal(42),
);
});

it('resolves to other assigned value if ref is in an assignment rhs', () => {
const path = parsePath(
['var foo;', 'foo = 42;', 'foo = wrap(foo);'].join('\n'),
);

expect(resolveToValue(path.get('right', 'arguments', 0))).toEqualASTNode(
builders.literal(42),
);
});
});

describe('ImportDeclaration', () => {
Expand Down
16 changes: 13 additions & 3 deletions src/utils/resolveToValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,32 @@ function findScopePath(paths: Array<NodePath>, path: NodePath): ?NodePath {
* Tries to find the last value assigned to `name` in the scope created by
* `scope`. We are not descending into any statements (blocks).
*/
function findLastAssignedValue(scope, name) {
function findLastAssignedValue(scope, idPath) {
const results = [];
const name = idPath.node.name;

traverseShallow(scope.path.node, {
visitAssignmentExpression: function(path) {
const node = path.node;
// Skip anything that is not an assignment to a variable with the
// passed name.
// Ensure the LHS isn't the reference we're trying to resolve.
if (
!t.Identifier.check(node.left) ||
node.left === idPath.node ||
node.left.name !== name ||
node.operator !== '='
) {
return this.traverse(path);
}
results.push(path.get('right'));
// Ensure the RHS doesn't contain the reference we're trying to resolve.
const candidatePath = path.get('right');
for (let p = idPath; p && p.node != null; p = p.parent) {
if (p.node === candidatePath.node) {
return this.traverse(path);
}
}
results.push(candidatePath);
return false;
},
});
Expand Down Expand Up @@ -159,7 +169,7 @@ export default function resolveToValue(path: NodePath): NodePath {
// The variable may be assigned a different value after initialization.
// We are first trying to find all assignments to the variable in the
// block where it is defined (i.e. we are not traversing into statements)
resolvedPath = findLastAssignedValue(scope, node.name);
resolvedPath = findLastAssignedValue(scope, path);
if (!resolvedPath) {
const bindings = scope.getBindings()[node.name];
resolvedPath = findScopePath(bindings, path);
Expand Down