Skip to content

Commit 4483f65

Browse files
authored
Add identity and power operators (#5790)
1 parent f1dd96f commit 4483f65

4 files changed

Lines changed: 72 additions & 8 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
@@ -1021,6 +1021,18 @@ public void visitBinaryExpression(BinaryExpression binary) {
10211021
case "?=":
10221022
gBinaryOp = G.Binary.Type.ElvisAssignment;
10231023
break;
1024+
case "**":
1025+
gBinaryOp = G.Binary.Type.Power;
1026+
break;
1027+
case "**=":
1028+
gBinaryOp = G.Binary.Type.PowerAssignment;
1029+
break;
1030+
case "===":
1031+
gBinaryOp = G.Binary.Type.IdentityEquals;
1032+
break;
1033+
case "!==":
1034+
gBinaryOp = G.Binary.Type.IdentityNotEquals;
1035+
break;
10241036
}
10251037

10261038
cursor += binary.getOperation().getText().length();

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,18 @@ public J visitBinary(G.Binary binary, PrintOutputCapture<P> p) {
172172
case ElvisAssignment:
173173
keyword = "?=";
174174
break;
175+
case Power:
176+
keyword = "**";
177+
break;
178+
case PowerAssignment:
179+
keyword = "**=";
180+
break;
181+
case IdentityEquals:
182+
keyword = "===";
183+
break;
184+
case IdentityNotEquals:
185+
keyword = "!==";
186+
break;
175187
}
176188
beforeSyntax(binary, GSpace.Location.BINARY_PREFIX, p);
177189
visit(binary.getLeft(), p);

rewrite-groovy/src/main/java/org/openrewrite/groovy/tree/G.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,11 @@ public enum Type {
796796
NotIn,
797797
Access,
798798
Spaceship,
799-
ElvisAssignment
799+
ElvisAssignment,
800+
Power,
801+
PowerAssignment,
802+
IdentityEquals,
803+
IdentityNotEquals
800804
}
801805

802806
public Padding getPadding() {

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

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,49 @@ void spaceShipOperator() {
281281
rewriteRun(
282282
groovy(
283283
"""
284-
def justPrint(){
285-
println(1 <=> 2)
286-
println('a' <=> 'z')
287-
def a = 'tiger'
288-
def b = 'cheetah'
289-
println(a <=> b)
290-
}
284+
println(1 <=> 2)
285+
println('a' <=> 'z')
286+
def a = 'tiger'
287+
def b = 'cheetah'
288+
println(a <=> b)
289+
"""
290+
));
291+
}
292+
293+
@Test
294+
void powerOperator() {
295+
rewriteRun(
296+
groovy(
297+
"""
298+
4 ** 3
299+
"""
300+
));
301+
}
302+
303+
@Test
304+
void powerAssigmentOperator() {
305+
rewriteRun(
306+
groovy(
307+
"""
308+
def f = 3
309+
f **= 2
310+
"""
311+
));
312+
}
313+
314+
@Test
315+
void identicalOperators() {
316+
rewriteRun(
317+
groovy(
318+
"""
319+
class Creature {}
320+
321+
def cat = new Creature()
322+
def copyCat = cat
323+
def lion = new Creature()
324+
325+
assert cat === copyCat
326+
assert cat !== lion
291327
"""
292328
));
293329
}

0 commit comments

Comments
 (0)