Skip to content

Commit a0c151b

Browse files
Groovy: support GStrings as identifiers
1 parent 3964f44 commit a0c151b

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
@@ -2388,11 +2388,14 @@ public void visitAttributeExpression(AttributeExpression attr) {
23882388
queue.add(insideParentheses(attr, fmt -> {
23892389
Expression target = doVisit(attr.getObjectExpression());
23902390
Space beforeDot = attr.isSafe() ? sourceBefore("?.") : sourceBefore(attr.isSpreadSafe() ? "*." : ".");
2391+
int saveCursor = cursor;
23912392
J name = doVisit(attr.getProperty());
23922393
if (name instanceof J.Literal) {
23932394
String nameStr = ((J.Literal) name).getValueSource();
23942395
assert nameStr != null;
23952396
name = new J.Identifier(randomId(), name.getPrefix(), Markers.EMPTY, emptyList(), nameStr, null, null);
2397+
} else if (name instanceof G.GString) {
2398+
name = gStringAsIdentifier((G.GString) name, saveCursor);
23962399
}
23972400
if (attr.isSpreadSafe()) {
23982401
name = name.withMarkers(name.getMarkers().add(new StarDot(randomId())));
@@ -2409,10 +2412,13 @@ public void visitPropertyExpression(PropertyExpression prop) {
24092412
queue.add(insideParentheses(prop, fmt -> {
24102413
Expression target = doVisit(prop.getObjectExpression());
24112414
Space beforeDot = prop.isSpreadSafe() ? sourceBefore("*.") : sourceBefore(prop.isSafe() ? "?." : ".");
2415+
int saveCursor = cursor;
24122416
J name = doVisit(prop.getProperty());
24132417
if (name instanceof J.Literal) {
24142418
J.Literal nameLiteral = ((J.Literal) name);
24152419
name = new J.Identifier(randomId(), name.getPrefix(), Markers.EMPTY, emptyList(), nameLiteral.getValueSource(), nameLiteral.getType(), null);
2420+
} else if (name instanceof G.GString) {
2421+
name = gStringAsIdentifier((G.GString) name, saveCursor);
24162422
}
24172423
if (prop.isSpreadSafe()) {
24182424
name = name.withMarkers(name.getMarkers().add(new StarDot(randomId())));
@@ -2423,6 +2429,12 @@ public void visitPropertyExpression(PropertyExpression prop) {
24232429
}));
24242430
}
24252431

2432+
private J.Identifier gStringAsIdentifier(G.GString gString, int saveCursor) {
2433+
int gStringStart = indexOfNextNonWhitespace(saveCursor, source);
2434+
String text = source.substring(gStringStart, cursor);
2435+
return new J.Identifier(randomId(), gString.getPrefix(), Markers.EMPTY, emptyList(), text, gString.getType(), null);
2436+
}
2437+
24262438
@Override
24272439
public void visitRangeExpression(RangeExpression range) {
24282440
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)