Skip to content

Commit

Permalink
support modifiers in for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
jevanlingen committed Dec 27, 2024
1 parent 52ff86e commit 20ded7a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1575,8 +1575,8 @@ public void visitForLoop(ForStatement forLoop) {
} else {
Parameter param = forLoop.getVariable();
Space paramFmt = whitespace();
TypeTree paramType = param.getOriginType().getColumnNumber() >= 0 ?
visitTypeTree(param.getOriginType()) : null;
List<J.Modifier> modifiers = getModifiers();
TypeTree paramType = param.getOriginType().getColumnNumber() >= 0 ? visitTypeTree(param.getOriginType()) : null;
JRightPadded<J.VariableDeclarations.NamedVariable> paramName = JRightPadded.build(
new J.VariableDeclarations.NamedVariable(randomId(), whitespace(), Markers.EMPTY,
new J.Identifier(randomId(), EMPTY, Markers.EMPTY, emptyList(), param.getName(), null, null),
Expand All @@ -1593,7 +1593,7 @@ public void visitForLoop(ForStatement forLoop) {
}

JRightPadded<J.VariableDeclarations> variable = JRightPadded.build(new J.VariableDeclarations(randomId(), paramFmt,
Markers.EMPTY, emptyList(), emptyList(), paramType, null, emptyList(),
Markers.EMPTY, emptyList(), modifiers, paramType, null, emptyList(),
singletonList(paramName))
).withAfter(rightPad);

Expand Down Expand Up @@ -2641,6 +2641,7 @@ private TypeTree visitTypeTree(ClassNode classNode, boolean inferredType) {

int saveCursor = cursor;
Space fmt = whitespace();
// TODO: remove this? At least use `getModifiers` function
if (cursor < source.length() && source.startsWith("def", cursor)) {
cursor += 3;
return new J.Identifier(randomId(), fmt, Markers.EMPTY, emptyList(), "def",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ void multiVariableInitialization() {

@Test
void forEachWithColon() {
rewriteRun(
groovy(
"""
for(def i : [1, 2, 3]) {}
"""
)
);
}

@Test
void forEachTypedWithColon() {
rewriteRun(
groovy(
"""
Expand All @@ -176,9 +187,7 @@ void forIn() {
rewriteRun(
groovy(
"""
def dependenciesType = ['implementation', 'testImplementation']
for (type in dependenciesType) {
}
for (type in ['implementation', 'testImplementation']) {}
"""
)
);
Expand Down

0 comments on commit 20ded7a

Please sign in to comment.