Skip to content

Commit

Permalink
Add used enums into the list of used variable names
Browse files Browse the repository at this point in the history
Currently, Enums aren't being added to the list of used variable names which can result in collisions when naming new variable.
  • Loading branch information
lkerford committed Oct 22, 2024
1 parent 212059e commit 6409220
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,23 @@ class Test {
);
}

@Test
void enumFieldNames() {
rewriteRun(
baseTest("myType", "myType,TYPES"),
java(
"""
class Test {
TYPES myType;
}
enum TYPES {
VALUE, NUMBER, TEXT
}
"""
)
);
}

@Test
void allClassFieldsAreFound() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,15 @@ public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations
return super.visitVariable(variable, strings);
}

public J.Identifier visitIdentifier(J.Identifier identifier, Set<String> namesInScope) {
J.Identifier v = super.visitIdentifier(identifier, namesInScope);
if (v.getType() instanceof JavaType.Class &&
((JavaType.Class) v.getType()).getKind() == JavaType.FullyQualified.Kind.Enum) {
namesInScope.add(v.getSimpleName());
}
return v;
}

private void addImportedStaticFieldNames(@Nullable JavaSourceFile cu, Cursor classCursor) {
if (cu != null) {
List<J.Import> imports = cu.getImports();
Expand Down

0 comments on commit 6409220

Please sign in to comment.