Skip to content
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

Fixed detection of optional chains containing a reference #59144

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Andarist
Copy link
Contributor

@Andarist Andarist commented Jul 5, 2024

fixes #56998

@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Jul 5, 2024
@@ -27143,7 +27143,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
function optionalChainContainsReference(source: Node, target: Node) {
while (isOptionalChain(source)) {
source = source.expression;
if (isMatchingReference(source, target)) {
if (isMatchingReference(target, source)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think an extra refactor should be made here.

isMatchingReference accepts reference as the first parameter but optionalChainContainsReference accepts it as the second one (!). That caused the issue here as both of them refer to both of the parameters as source and target - so it wasn't apparent that the reference (called a target here) is passed to isMatchingReference at the position that is used for candidate expressions (also called a target by that function).

I think it would make sense to:

  1. rename those parameters to something like expression and reference or maybe candidate and reference. isMatchingReference sometimes calls recursively itself with adjusted source (what I propose to call a reference here) but I think that doesn't impact the proposed naming
  2. use consistent order of parameters. I think what optionalChainContainsReference uses is better (reference passed as the second argument to it) but isMatchingReference is likely an older function so maybe the team would prefer that.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
3 participants