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

Conversation

motiz88
Copy link
Contributor

@motiz88 motiz88 commented Feb 13, 2020

When calling resolveToValue, currently it may resolve to an expression containing the very reference we were trying to resolve, even though the correct resolution in terms of program flow is an earlier assignment. In particular, this arises in some handlers when we reassign a variable holding a component, e.g when wrapping it with a HOC:

import React from 'react';
let Component = (props, ref) => {};
//              ^
//              expected resolution
Component = React.forwardRef(     Component     );
//          ^                     ^
//          actual resolution     reference

Here, we minimally tweak resolveToValue to (1) take an Identifier path instead of a name; and (2) not resolve to an assignment expression's RHS if the assignment contains the reference we're trying to resolve. Note that this isn't a complete solution for tracking reassignments (e.g. it doesn't account for ordering or more complex ASTs) but it should be a satisfactory fix for this particular pattern.

This minimal tweak to `resolveToValue` allows us, in particular, to follow some reassignments of the variable holding a component, e.g. when wrapping it with a HOC.
@motiz88 motiz88 force-pushed the resolve-no-circular branch from 9b0d8d0 to 505e1f8 Compare February 13, 2020 01:48
@danez danez merged commit 04f573f into reactjs:master Feb 13, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants