Skip to content

Commit

Permalink
Use OmitParentheses marker for new operation without parentheses (#182)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrii Rodionov <[email protected]>
  • Loading branch information
arodionov and Andrii Rodionov authored Dec 30, 2024
1 parent 54b0dca commit e025613
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
18 changes: 18 additions & 0 deletions openrewrite/src/java/markers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,21 @@ export class TrailingComma implements Marker {
return suffix == this._suffix ? this : new TrailingComma(this._id, suffix);
}
}

@LstType("org.openrewrite.java.marker.OmitParentheses")
export class OmitParentheses implements Marker {
[MarkerSymbol] = true;
private readonly _id: UUID;

constructor(id: UUID) {
this._id = id;
}

get id() {
return this._id;
}

withId(id: UUID): OmitParentheses {
return id == this._id ? this : new OmitParentheses(id);
}
}
2 changes: 1 addition & 1 deletion openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1945,7 +1945,7 @@ export class JavaScriptParserVisitor {
this.mapTypeArguments(this.prefix(this.findChildNode(node, ts.SyntaxKind.LessThanToken)!), node.typeArguments),
null
): new TypeTreeExpression(randomId(), Space.EMPTY, Markers.EMPTY, this.visit(node.expression)),
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
node.arguments ? this.mapCommaSeparatedList(this.getParameterListNodes(node)) : JContainer.empty<J.Expression>().withMarkers(Markers.build([(new J.OmitParentheses(randomId()))])),
null,
this.mapMethodType(node)
);
Expand Down
4 changes: 2 additions & 2 deletions openrewrite/test/javascript/parser/call.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ describe('call mapping', () => {
});

// need a way to distinguish new class calls with empty braces and without braces
test.skip('call new expression without braces', () => {
test('call new expression without braces', () => {
rewriteRun(
//language=typescript
typeScript(`
var d = (new Date).getTime()
// use marker omit
const intType = new arrow.Uint32
`)
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.openrewrite.PrintOutputCapture;
import org.openrewrite.Tree;
import org.openrewrite.java.JavaPrinter;
import org.openrewrite.java.marker.OmitParentheses;
import org.openrewrite.java.marker.Semicolon;
import org.openrewrite.java.marker.TrailingComma;
import org.openrewrite.java.tree.*;
Expand Down Expand Up @@ -1275,7 +1276,9 @@ public J visitNewClass(J.NewClass newClass, PrintOutputCapture<P> p) {
if (newClass.getClazz() != null) {
p.append("new");
visit(newClass.getClazz(), p);
visitContainer("(", newClass.getPadding().getArguments(), JContainer.Location.NEW_CLASS_ARGUMENTS, ",", ")", p);
if (!newClass.getPadding().getArguments().getMarkers().findFirst(OmitParentheses.class).isPresent()) {
visitContainer("(", newClass.getPadding().getArguments(), JContainer.Location.NEW_CLASS_ARGUMENTS, ",", ")", p);
}
}
visit(newClass.getBody(), p);
afterSyntax(newClass, p);
Expand Down

0 comments on commit e025613

Please sign in to comment.