Skip to content

Commit 8ed4109

Browse files
Advance cursor when unknown code during Enum parsing (#6061)
1 parent 0de4681 commit 8ed4109

2 files changed

Lines changed: 26 additions & 6 deletions

File tree

rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -515,13 +515,12 @@ private J.EnumValue visitEnumField(FieldNode field) {
515515
depth--;
516516
cursor++;
517517
}
518-
argCount++;
519-
// Safety check: if cursor didn't advance, throw an exception to avoid infinite loop
520518
if (cursor == cursorBeforeIteration) {
521-
throw new IllegalStateException(
522-
"Parser error: unable to parse enum constructor arguments at position " + cursor +
523-
" near '" + source.substring(Math.max(0, cursor - 20), Math.min(source.length(), cursor + 20)) + "'"
524-
);
519+
// We are not facing an opening of a new parens/delimiter, no whitespace, just a usual piece of code - e.g. an operator like `+`.
520+
// As we are not parsing it, just grabing the code into a String, it's safe to advance.
521+
cursor++;
522+
} else {
523+
argCount++;
525524
}
526525
}
527526
String argsAsString = source.substring(start, cursor);

rewrite-groovy/src/test/java/org/openrewrite/groovy/tree/EnumTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,25 @@ enum Mapped {
316316
);
317317
}
318318

319+
@Test
320+
void expressionsInArguments() {
321+
rewriteRun(
322+
groovy(
323+
"""
324+
enum Status {
325+
GOOD(PREFIX + " one. ", PREFIX + " two. ")
326+
327+
public static final String PREFIX = "{object}"
328+
String a
329+
String b
330+
331+
Status(String a, String b) {
332+
this.a = a
333+
this.b = b
334+
}
335+
}
336+
"""
337+
)
338+
);
339+
}
319340
}

0 commit comments

Comments
 (0)