Skip to content

Commit

Permalink
Map ts.DeleteExpression, ts.TypeOfExpression, and `ts.PrefixUnary…
Browse files Browse the repository at this point in the history
…Expression`
  • Loading branch information
knutwannheden committed Sep 27, 2024
1 parent 92fec89 commit 3074f23
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 21 deletions.
52 changes: 49 additions & 3 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,11 +586,25 @@ export class JavaScriptParserVisitor {
}

visitDeleteExpression(node: ts.DeleteExpression) {
return this.visitUnknown(node);
return new JS.Delete(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.convert(node.expression),
this.mapType(node)
);
}

visitTypeOfExpression(node: ts.TypeOfExpression) {
return this.visitUnknown(node);
return new JS.JsOperator(
randomId(),
this.prefix(node),
Markers.EMPTY,
null,
this.leftPadded(this.prefix(node.getFirstToken()!), JS.JsOperator.Type.TypeOf),
this.convert(node.expression),
this.mapType(node)
)
}

visitVoidExpression(node: ts.VoidExpression) {
Expand All @@ -602,7 +616,39 @@ export class JavaScriptParserVisitor {
}

visitPrefixUnaryExpression(node: ts.PrefixUnaryExpression) {
return this.visitUnknown(node);
let unaryOperator: J.Unary.Type | undefined;
switch (node.operator) {
case ts.SyntaxKind.PlusToken:
unaryOperator = J.Unary.Type.Positive;
break;
case ts.SyntaxKind.MinusToken:
unaryOperator = J.Unary.Type.Negative;
break;
case ts.SyntaxKind.ExclamationToken:
unaryOperator = J.Unary.Type.Not;
break;
case ts.SyntaxKind.PlusPlusToken:
unaryOperator = J.Unary.Type.PreIncrement;
break;
case ts.SyntaxKind.MinusMinusToken:
unaryOperator = J.Unary.Type.PreDecrement;
break;
case ts.SyntaxKind.TildeToken:
unaryOperator = J.Unary.Type.Complement;
}

if (unaryOperator === undefined) {
return this.visitUnknown(node);
}

return new J.Unary(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.leftPadded(this.prefix(node.getFirstToken()!), unaryOperator),
this.convert(node.operand),
this.mapType(node)
);
}

visitPostfixUnaryExpression(node: ts.PostfixUnaryExpression) {
Expand Down
4 changes: 1 addition & 3 deletions openrewrite/src/javascript/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1075,9 +1075,7 @@ export class JsOperator extends JSMixin(Object) implements Statement, Expression
export namespace JsOperator {
export enum Type {
Await = 0,
Delete = 1,
In = 2,
TypeOf = 3,
TypeOf = 1,

}

Expand Down
13 changes: 13 additions & 0 deletions openrewrite/test/javascript/parser/delete.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {connect, disconnect, rewriteRun, typeScript} from '../testHarness';

describe('delete operator mapping', () => {
beforeAll(() => connect());
afterAll(() => disconnect());

test('delete', () => {
rewriteRun(
//language=typescript
typeScript('delete 1')
);
});
});
13 changes: 13 additions & 0 deletions openrewrite/test/javascript/parser/typeof.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {connect, disconnect, rewriteRun, typeScript} from '../testHarness';

describe('typeof operator mapping', () => {
beforeAll(() => connect());
afterAll(() => disconnect());

test('typeof', () => {
rewriteRun(
//language=typescript
typeScript('typeof 1')
);
});
});
43 changes: 43 additions & 0 deletions openrewrite/test/javascript/parser/unary.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {connect, disconnect, rewriteRun, typeScript} from '../testHarness';

describe('prefix operator mapping', () => {
beforeAll(() => connect());
afterAll(() => disconnect());

test('plus', () => {
rewriteRun(
//language=typescript
typeScript('+1')
);
});
test('minus', () => {
rewriteRun(
//language=typescript
typeScript('-1')
);
});
test('not', () => {
rewriteRun(
//language=typescript
typeScript('!1')
);
});
test('tilde', () => {
rewriteRun(
//language=typescript
typeScript('~1')
);
});
test('increment', () => {
rewriteRun(
//language=typescript
typeScript('++1')
);
});
test('decrement', () => {
rewriteRun(
//language=typescript
typeScript('--a;')
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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 @@ -240,12 +239,6 @@ public J visitJsOperator(JS.JsOperator operator, PrintOutputCapture<P> p) {
case Await:
keyword = "await";
break;
case Delete:
keyword = "delete";
break;
case In:
keyword = "in";
break;
case TypeOf:
keyword = "typeof";
break;
Expand Down Expand Up @@ -645,13 +638,12 @@ public J visitNewClass(J.NewClass newClass, PrintOutputCapture<P> p) {
beforeSyntax(newClass, Space.Location.NEW_CLASS_PREFIX, p);
visitRightPadded(newClass.getPadding().getEnclosing(), JRightPadded.Location.NEW_CLASS_ENCLOSING, ".", p);
visitSpace(newClass.getNew(), Space.Location.NEW_PREFIX, p);
boolean objectLiteral = newClass.getMarkers().findFirst(ObjectLiteral.class).isPresent();
if (!objectLiteral) {
if (newClass.getClazz() != null) {
p.append("new");
}
visit(newClass.getClazz(), p);
if (!newClass.getPadding().getArguments().getMarkers().findFirst(OmitParentheses.class).isPresent()) {
visitContainer(objectLiteral ? "{" : "(", newClass.getPadding().getArguments(), JContainer.Location.NEW_CLASS_ARGUMENTS, ",", objectLiteral ? "}" : ")", p);
visit(newClass.getClazz(), p);
visitContainer("(", newClass.getPadding().getArguments(), JContainer.Location.NEW_CLASS_ARGUMENTS, ",", ")", p);
} else {
visitContainer("{", newClass.getPadding().getArguments(), JContainer.Location.NEW_CLASS_ARGUMENTS, ",", "}", p);
}
visit(newClass.getBody(), p);
afterSyntax(newClass, p);
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/org/openrewrite/javascript/tree/JS.java
Original file line number Diff line number Diff line change
Expand Up @@ -974,8 +974,6 @@ public CoordinateBuilder.Statement getCoordinates() {

public enum Type {
Await,
Delete,
In,
TypeOf
}

Expand Down

0 comments on commit 3074f23

Please sign in to comment.