Skip to content

Commit

Permalink
Fix variable highlighting in arrow function apache#4209
Browse files Browse the repository at this point in the history
- apache#4209
- Fix the scope for the variable in the arrow function
- Add unit tests
  • Loading branch information
junichi11 committed Jun 18, 2022
1 parent 624fb16 commit 9cab4ae
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -862,9 +862,11 @@ private void prepareArrowFunctionVariable(Scope scope, Variable node) {
&& ((FunctionScope)inScope).isAnonymous()) {
FunctionScope functionScope = (FunctionScope) inScope;
Collection<? extends VariableName> declaredVariables = functionScope.getDeclaredVariables();
List<? extends String> parameterNames = functionScope.getParameterNames();
for (VariableName declaredVariable : declaredVariables) {
if (declaredVariable.getName().equals(CodeUtils.extractVariableName(node))) {
if (isLexicalVariable(node)) {
if (isLexicalVariable(node)
|| (functionScope instanceof ArrowFunctionScope) && !parameterNames.contains(declaredVariable.getName())) { // GH-4209
occurencesBuilder.prepare(node, inScope.getInScope());
} else {
occurencesBuilder.prepare(node, inScope);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ function (): ArrowFunctions {
return fn($x) => $x + $y;
};

// GH-4209
$gh4209a = 0;
$gh4209b = fn() => $gh4209a > 0 ? $gh4209a + 1 : 2;
$gh4209b = fn($gh4209c) => $gh4209a > 0 ? $gh4209a + 1 : $gh4209c + 2;

function test(callable $f) {
return fn(...$args) => !$f(...$args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ function (): ArrowFunctions {
return fn($x) => $x + $y;
};

// GH-4209
$gh4209a = 0;
$gh4209b = fn() => $gh4209a > 0 ? $gh4209a + 1 : 2;
$gh4209b = fn($gh4209c) => $gh4209a > 0 ? $gh4209a + 1 : $gh4209c + 2;

function test(callable $f) {
return fn(...$args) => !$f(...$args);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$|>MARK_OCCURRENCES:gh^4209a<| = 0;
$gh4209b = fn() => $|>MARK_OCCURRENCES:gh4209a<| > 0 ? $|>MARK_OCCURRENCES:gh4209a<| + 1 : 2;
$gh4209b = fn($gh4209c) => $|>MARK_OCCURRENCES:gh4209a<| > 0 ? $|>MARK_OCCURRENCES:gh4209a<| + 1 : $gh4209c + 2;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$|>MARK_OCCURRENCES:gh4209a<| = 0;
$gh4209b = fn() => $|>MARK_OCCURRENCES:gh420^9a<| > 0 ? $|>MARK_OCCURRENCES:gh4209a<| + 1 : 2;
$gh4209b = fn($gh4209c) => $|>MARK_OCCURRENCES:gh4209a<| > 0 ? $|>MARK_OCCURRENCES:gh4209a<| + 1 : $gh4209c + 2;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$|>MARK_OCCURRENCES:gh4209a<| = 0;
$gh4209b = fn() => $|>MARK_OCCURRENCES:gh4209a<| > 0 ? $|>MARK_OCCURRENCES:gh^4209a<| + 1 : 2;
$gh4209b = fn($gh4209c) => $|>MARK_OCCURRENCES:gh4209a<| > 0 ? $|>MARK_OCCURRENCES:gh4209a<| + 1 : $gh4209c + 2;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$|>MARK_OCCURRENCES:gh4209a<| = 0;
$gh4209b = fn() => $|>MARK_OCCURRENCES:gh4209a<| > 0 ? $|>MARK_OCCURRENCES:gh4209a<| + 1 : 2;
$gh4209b = fn($gh4209c) => $|>MARK_OCCURRENCES:gh42^09a<| > 0 ? $|>MARK_OCCURRENCES:gh4209a<| + 1 : $gh4209c + 2;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$|>MARK_OCCURRENCES:gh4209a<| = 0;
$gh4209b = fn() => $|>MARK_OCCURRENCES:gh4209a<| > 0 ? $|>MARK_OCCURRENCES:gh4209a<| + 1 : 2;
$gh4209b = fn($gh4209c) => $|>MARK_OCCURRENCES:gh4209a<| > 0 ? $|>MARK_OCCURRENCES:gh4^209a<| + 1 : $gh4209c + 2;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$|>MARK_OCCURRENCES:gh4^209b<| = fn() => $gh4209a > 0 ? $gh4209a + 1 : 2;
$|>MARK_OCCURRENCES:gh4209b<| = fn($gh4209c) => $gh4209a > 0 ? $gh4209a + 1 : $gh4209c + 2;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$|>MARK_OCCURRENCES:gh4209b<| = fn() => $gh4209a > 0 ? $gh4209a + 1 : 2;
$|>MARK_OCCURRENCES:gh42^09b<| = fn($gh4209c) => $gh4209a > 0 ? $gh4209a + 1 : $gh4209c + 2;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$gh4209b = fn($|>MARK_OCCURRENCES:gh^4209c<|) => $gh4209a > 0 ? $gh4209a + 1 : $|>MARK_OCCURRENCES:gh4209c<| + 2;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$gh4209b = fn($|>MARK_OCCURRENCES:gh4209c<|) => $gh4209a > 0 ? $gh4209a + 1 : $|>MARK_OCCURRENCES:gh42^09c<| + 2;
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,28 @@ public void testArrowFunctions_21() throws Exception {
checkDeclaration(getTestPath(), " return fn(...$args) => !$f(...$ar^gs);", " return fn(...$^args) => !$f(...$args);");
}

public void testArrowFunctions_GH4209_01a() throws Exception {
checkDeclaration(getTestPath(), "$gh4209b = fn() => $g^h4209a > 0 ? $gh4209a + 1 : 2;", "$^gh4209a = 0;");
}

public void testArrowFunctions_GH4209_01b() throws Exception {
checkDeclaration(getTestPath(), "$gh4209b = fn() => $gh4209a > 0 ? $gh4^209a + 1 : 2;", "$^gh4209a = 0;");
}

public void testArrowFunctions_GH4209_01c() throws Exception {
checkDeclaration(getTestPath(), "$gh4209b = fn($gh4209c) => $gh420^9a > 0 ? $gh4209a + 1 : $gh4209c + 2;", "$^gh4209a = 0;");
}

public void testArrowFunctions_GH4209_01d() throws Exception {
checkDeclaration(getTestPath(), "$gh4209b = fn($gh4209c) => $gh4209a > 0 ? $gh4209^a + 1 : $gh4209c + 2;", "$^gh4209a = 0;");
}

public void testArrowFunctions_GH4209_02a() throws Exception {
checkDeclaration(getTestPath(), "$gh420^9b = fn($gh4209c) => $gh4209a > 0 ? $gh4209a + 1 : $gh4209c + 2;", "$^gh4209b = fn() => $gh4209a > 0 ? $gh4209a + 1 : 2;");
}

public void testArrowFunctions_GH4209_03a() throws Exception {
checkDeclaration(getTestPath(), "$gh4209b = fn($gh4209c) => $gh4209a > 0 ? $gh4209a + 1 : $gh^4209c + 2;", "$gh4209b = fn($^gh4209c) => $gh4209a > 0 ? $gh4209a + 1 : $gh4209c + 2;");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -596,4 +596,40 @@ public void testArrowFunctions_25b() throws Exception {
checkOccurrences(getTestPath(), " return fn(...$args) => !$f(...$ar^gs);", true);
}

public void testArrowFunctions_GH4209_01a() throws Exception {
checkOccurrences(getTestPath(), "$gh^4209a = 0;", true);
}

public void testArrowFunctions_GH4209_01b() throws Exception {
checkOccurrences(getTestPath(), "$gh4209b = fn() => $gh420^9a > 0 ? $gh4209a + 1 : 2;", true);
}

public void testArrowFunctions_GH4209_01c() throws Exception {
checkOccurrences(getTestPath(), "$gh4209b = fn() => $gh4209a > 0 ? $gh^4209a + 1 : 2;", true);
}

public void testArrowFunctions_GH4209_01d() throws Exception {
checkOccurrences(getTestPath(), "$gh4209b = fn($gh4209c) => $gh42^09a > 0 ? $gh4209a + 1 : $gh4209c + 2;", true);
}

public void testArrowFunctions_GH4209_01e() throws Exception {
checkOccurrences(getTestPath(), "$gh4209b = fn($gh4209c) => $gh4209a > 0 ? $gh4^209a + 1 : $gh4209c + 2;", true);
}

public void testArrowFunctions_GH4209_02a() throws Exception {
checkOccurrences(getTestPath(), "$gh4^209b = fn() => $gh4209a > 0 ? $gh4209a + 1 : 2;", true);
}

public void testArrowFunctions_GH4209_02b() throws Exception {
checkOccurrences(getTestPath(), "$gh42^09b = fn($gh4209c) => $gh4209a > 0 ? $gh4209a + 1 : $gh4209c + 2;", true);
}

public void testArrowFunctions_GH4209_03a() throws Exception {
checkOccurrences(getTestPath(), "$gh4209b = fn($gh^4209c) => $gh4209a > 0 ? $gh4209a + 1 : $gh4209c + 2;", true);
}

public void testArrowFunctions_GH4209_03b() throws Exception {
checkOccurrences(getTestPath(), "$gh4209b = fn($gh4209c) => $gh4209a > 0 ? $gh4209a + 1 : $gh42^09c + 2;", true);
}

}

0 comments on commit 9cab4ae

Please sign in to comment.