Skip to content

Commit e0e7e06

Browse files
Groovy: support GStrings as identifiers (#7525)
1 parent 979d63b commit e0e7e06

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2390,11 +2390,14 @@ public void visitAttributeExpression(AttributeExpression attr) {
23902390
queue.add(insideParentheses(attr, fmt -> {
23912391
Expression target = doVisit(attr.getObjectExpression());
23922392
Space beforeDot = attr.isSafe() ? sourceBefore("?.") : sourceBefore(attr.isSpreadSafe() ? "*." : ".");
2393+
int saveCursor = cursor;
23932394
J name = doVisit(attr.getProperty());
23942395
if (name instanceof J.Literal) {
23952396
String nameStr = ((J.Literal) name).getValueSource();
23962397
assert nameStr != null;
23972398
name = new J.Identifier(randomId(), name.getPrefix(), Markers.EMPTY, emptyList(), nameStr, null, null);
2399+
} else if (name instanceof G.GString) {
2400+
name = gStringAsIdentifier((G.GString) name, saveCursor);
23982401
}
23992402
if (attr.isSpreadSafe()) {
24002403
name = name.withMarkers(name.getMarkers().add(new StarDot(randomId())));
@@ -2411,10 +2414,13 @@ public void visitPropertyExpression(PropertyExpression prop) {
24112414
queue.add(insideParentheses(prop, fmt -> {
24122415
Expression target = doVisit(prop.getObjectExpression());
24132416
Space beforeDot = prop.isSpreadSafe() ? sourceBefore("*.") : sourceBefore(prop.isSafe() ? "?." : ".");
2417+
int saveCursor = cursor;
24142418
J name = doVisit(prop.getProperty());
24152419
if (name instanceof J.Literal) {
24162420
J.Literal nameLiteral = ((J.Literal) name);
24172421
name = new J.Identifier(randomId(), name.getPrefix(), Markers.EMPTY, emptyList(), nameLiteral.getValueSource(), nameLiteral.getType(), null);
2422+
} else if (name instanceof G.GString) {
2423+
name = gStringAsIdentifier((G.GString) name, saveCursor);
24182424
}
24192425
if (prop.isSpreadSafe()) {
24202426
name = name.withMarkers(name.getMarkers().add(new StarDot(randomId())));
@@ -2425,6 +2431,12 @@ public void visitPropertyExpression(PropertyExpression prop) {
24252431
}));
24262432
}
24272433

2434+
private J.Identifier gStringAsIdentifier(G.GString gString, int saveCursor) {
2435+
int gStringStart = indexOfNextNonWhitespace(saveCursor, source);
2436+
String text = source.substring(gStringStart, cursor);
2437+
return new J.Identifier(randomId(), gString.getPrefix(), Markers.EMPTY, emptyList(), text, gString.getType(), null);
2438+
}
2439+
24282440
@Override
24292441
public void visitRangeExpression(RangeExpression range) {
24302442
queue.add(insideParentheses(range, fmt -> new G.Range(randomId(), fmt, Markers.EMPTY,

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,17 @@ class Test {
7272
)
7373
);
7474
}
75+
76+
@Test
77+
void dynamicPropertyNameWithGString() {
78+
rewriteRun(
79+
groovy(
80+
"""
81+
def f(node, key) {
82+
node."@$key"
83+
}
84+
"""
85+
)
86+
);
87+
}
7588
}

0 commit comments

Comments
 (0)