Skip to content
This repository was archived by the owner on May 12, 2025. It is now read-only.

Commit e025613

Browse files
arodionovAndrii Rodionov
andauthored
Use OmitParentheses marker for new operation without parentheses (#182)
Co-authored-by: Andrii Rodionov <[email protected]>
1 parent 54b0dca commit e025613

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

openrewrite/src/java/markers.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,21 @@ export class TrailingComma implements Marker {
4646
return suffix == this._suffix ? this : new TrailingComma(this._id, suffix);
4747
}
4848
}
49+
50+
@LstType("org.openrewrite.java.marker.OmitParentheses")
51+
export class OmitParentheses implements Marker {
52+
[MarkerSymbol] = true;
53+
private readonly _id: UUID;
54+
55+
constructor(id: UUID) {
56+
this._id = id;
57+
}
58+
59+
get id() {
60+
return this._id;
61+
}
62+
63+
withId(id: UUID): OmitParentheses {
64+
return id == this._id ? this : new OmitParentheses(id);
65+
}
66+
}

openrewrite/src/javascript/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1945,7 +1945,7 @@ export class JavaScriptParserVisitor {
19451945
this.mapTypeArguments(this.prefix(this.findChildNode(node, ts.SyntaxKind.LessThanToken)!), node.typeArguments),
19461946
null
19471947
): new TypeTreeExpression(randomId(), Space.EMPTY, Markers.EMPTY, this.visit(node.expression)),
1948-
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
1948+
node.arguments ? this.mapCommaSeparatedList(this.getParameterListNodes(node)) : JContainer.empty<J.Expression>().withMarkers(Markers.build([(new J.OmitParentheses(randomId()))])),
19491949
null,
19501950
this.mapMethodType(node)
19511951
);

openrewrite/test/javascript/parser/call.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,12 @@ describe('call mapping', () => {
157157
});
158158

159159
// need a way to distinguish new class calls with empty braces and without braces
160-
test.skip('call new expression without braces', () => {
160+
test('call new expression without braces', () => {
161161
rewriteRun(
162162
//language=typescript
163163
typeScript(`
164164
var d = (new Date).getTime()
165-
// use marker omit
165+
const intType = new arrow.Uint32
166166
`)
167167
);
168168
});

rewrite-javascript/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.openrewrite.PrintOutputCapture;
2121
import org.openrewrite.Tree;
2222
import org.openrewrite.java.JavaPrinter;
23+
import org.openrewrite.java.marker.OmitParentheses;
2324
import org.openrewrite.java.marker.Semicolon;
2425
import org.openrewrite.java.marker.TrailingComma;
2526
import org.openrewrite.java.tree.*;
@@ -1275,7 +1276,9 @@ public J visitNewClass(J.NewClass newClass, PrintOutputCapture<P> p) {
12751276
if (newClass.getClazz() != null) {
12761277
p.append("new");
12771278
visit(newClass.getClazz(), p);
1278-
visitContainer("(", newClass.getPadding().getArguments(), JContainer.Location.NEW_CLASS_ARGUMENTS, ",", ")", p);
1279+
if (!newClass.getPadding().getArguments().getMarkers().findFirst(OmitParentheses.class).isPresent()) {
1280+
visitContainer("(", newClass.getPadding().getArguments(), JContainer.Location.NEW_CLASS_ARGUMENTS, ",", ")", p);
1281+
}
12791282
}
12801283
visit(newClass.getBody(), p);
12811284
afterSyntax(newClass, p);

0 commit comments

Comments
 (0)