From 1caeedd9879b7b519482948a07e424dd264048b7 Mon Sep 17 00:00:00 2001 From: Kun Li Date: Mon, 27 Nov 2023 11:10:29 -0800 Subject: [PATCH 1/3] Add JS.Binary to support TS/JS specific binary type like in --- .../javascript/JavaScriptVisitor.java | 19 +++- .../internal/JavaScriptPrinter.java | 20 +++- .../internal/TypeScriptParserVisitor.java | 40 ++++++-- .../org/openrewrite/javascript/tree/JS.java | 99 +++++++++++++++++++ .../javascript/tree/JsLeftPadded.java | 7 +- .../javascript/tree/BinaryTest.java | 13 +++ .../javascript/tree/TypeOperatorTest.java | 13 --- 7 files changed, 182 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java b/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java index 3fa40dcc..c89d8ef5 100644 --- a/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java +++ b/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java @@ -26,7 +26,7 @@ import java.util.List; import java.util.Objects; -@SuppressWarnings("unused") +@SuppressWarnings({"unused", "DuplicatedCode"}) public class JavaScriptVisitor

extends JavaVisitor

{ @Override @@ -108,6 +108,23 @@ public J visitArrowFunction(JS.ArrowFunction arrowFunction, P p) { return a; } + public J visitBinary(JS.Binary binary, P p) { + JS.Binary b = binary; + b = b.withPrefix(visitSpace(b.getPrefix(), Space.Location.BINARY_PREFIX, p)); + b = b.withMarkers(visitMarkers(b.getMarkers(), p)); + Expression temp = (Expression) visitExpression(b, p); + if (!(temp instanceof JS.Binary)) { + return temp; + } else { + b = (JS.Binary) temp; + } + b = b.withLeft(visitAndCast(b.getLeft(), p)); + b = b.getPadding().withOperator(visitLeftPadded(b.getPadding().getOperator(), JsLeftPadded.Location.BINARY_OPERATOR, p)); + b = b.withRight(visitAndCast(b.getRight(), p)); + b = b.withType(visitType(b.getType(), p)); + return b; + } + public J visitBinding(JS.ObjectBindingDeclarations.Binding binding, P p) { JS.ObjectBindingDeclarations.Binding b = binding; b = b.withPrefix(visitSpace(b.getPrefix(), JsSpace.Location.BINDING_PREFIX, p)); diff --git a/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java b/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java index 9ea557e5..63b4109b 100644 --- a/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java +++ b/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java @@ -33,7 +33,7 @@ import java.util.List; import java.util.function.UnaryOperator; -@SuppressWarnings("SameParameterValue") +@SuppressWarnings({"SameParameterValue", "SwitchStatementWithTooFewBranches"}) public class JavaScriptPrinter

extends JavaScriptVisitor> { private static final UnaryOperator JAVA_SCRIPT_MARKER_WRAPPER = @@ -101,6 +101,24 @@ public J visitArrowFunction(JS.ArrowFunction arrowFunction, PrintOutputCapture

p) { + beforeSyntax(binary, Space.Location.BINARY_PREFIX, p); + String keyword = ""; + switch (binary.getOperator()) { + case In: + keyword = "in"; + break; + } + visit(binary.getLeft(), p); + visitSpace(binary.getPadding().getOperator().getBefore(), Space.Location.BINARY_OPERATOR, p); + p.append(keyword); + + visit(binary.getRight(), p); + afterSyntax(binary, p); + return binary; + } + @Override public J visitBinding(JS.ObjectBindingDeclarations.Binding binding, PrintOutputCapture

p) { beforeSyntax(binding, JsSpace.Location.BINDING_PREFIX, p); diff --git a/src/main/java/org/openrewrite/javascript/internal/TypeScriptParserVisitor.java b/src/main/java/org/openrewrite/javascript/internal/TypeScriptParserVisitor.java index 7288174c..d0e61320 100644 --- a/src/main/java/org/openrewrite/javascript/internal/TypeScriptParserVisitor.java +++ b/src/main/java/org/openrewrite/javascript/internal/TypeScriptParserVisitor.java @@ -290,13 +290,14 @@ private J.TypeCast visitAsExpression(TSCNode node) { ); } - private J.Binary visitBinary(TSCNode node) { + private J visitBinary(TSCNode node) { Space prefix = whitespace(); Markers markers = Markers.EMPTY; Expression left = (Expression) visitNode(node.getNodeProperty("left")); Space opPrefix = whitespace(); JLeftPadded op = null; + JLeftPadded jsOp = null; TSCSyntaxKind opKind = node.getNodeProperty("operatorToken").syntaxKind(); switch (opKind) { // Bitwise ops @@ -387,20 +388,38 @@ private J.Binary visitBinary(TSCNode node) { consumeToken(TSCSyntaxKind.SlashToken); op = padLeft(opPrefix, J.Binary.Type.Division); break; + // TS/JS specific ops + case InKeyword: + consumeToken(TSCSyntaxKind.InKeyword); + jsOp = padLeft(opPrefix, JS.Binary.Type.In); + break; default: implementMe(node); } Expression right = (Expression) visitNode(node.getNodeProperty("right")); - return new J.Binary( - randomId(), - prefix, - markers, - left, - op, - right, - typeMapping.type(node) - ); + + if (jsOp != null) { + return new JS.Binary( + randomId(), + prefix, + markers, + left, + jsOp, + right, + typeMapping.type(node) + ); + } else { + return new J.Binary( + randomId(), + prefix, + markers, + left, + op, + right, + typeMapping.type(node) + ); + } } private J visitBinaryExpression(TSCNode node) { @@ -428,6 +447,7 @@ private J visitBinaryExpression(TSCNode node) { case GreaterThanEqualsToken: case GreaterThanGreaterThanToken: case GreaterThanGreaterThanGreaterThanToken: + case InKeyword: case LessThanToken: case LessThanEqualsToken: case LessThanLessThanToken: diff --git a/src/main/java/org/openrewrite/javascript/tree/JS.java b/src/main/java/org/openrewrite/javascript/tree/JS.java index 2d0208e9..49ec6cbd 100644 --- a/src/main/java/org/openrewrite/javascript/tree/JS.java +++ b/src/main/java/org/openrewrite/javascript/tree/JS.java @@ -403,6 +403,105 @@ public CoordinateBuilder.Statement getCoordinates() { } } + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @AllArgsConstructor(access = AccessLevel.PRIVATE) + @Data + final class Binary implements JS, Expression, TypedTree { + + @Nullable + @NonFinal + transient WeakReference padding; + + @With + @EqualsAndHashCode.Include + UUID id; + + @With + Space prefix; + + @With + Markers markers; + + @With + Expression left; + + JLeftPadded operator; + + public Binary(UUID id, Space prefix, Markers markers, Expression left, JLeftPadded operator, Expression right, @Nullable JavaType type) { + this.id = id; + this.prefix = prefix; + this.markers = markers; + this.left = left; + this.operator = operator; + this.right = right; + this.type = type; + } + + public JS.Binary.Type getOperator() { + return operator.getElement(); + } + + public JS.Binary withOperator(JS.Binary.Type operator) { + return getPadding().withOperator(this.operator.withElement(operator)); + } + + @With + Expression right; + + @With + @Nullable + JavaType type; + + @Override + public

J acceptJavaScript(JavaScriptVisitor

v, P p) { + return v.visitBinary(this, p); + } + + @Transient + @Override + public CoordinateBuilder.Expression getCoordinates() { + return new CoordinateBuilder.Expression(this); + } + + public enum Type { + In, + } + + public JS.Binary.Padding getPadding() { + JS.Binary.Padding p; + if (this.padding == null) { + p = new JS.Binary.Padding(this); + this.padding = new WeakReference<>(p); + } else { + p = this.padding.get(); + if (p == null || p.t != this) { + p = new JS.Binary.Padding(this); + this.padding = new WeakReference<>(p); + } + } + return p; + } + + @RequiredArgsConstructor + public static class Padding { + private final JS.Binary t; + + public JLeftPadded getOperator() { + return t.operator; + } + + public JS.Binary withOperator(JLeftPadded operator) { + return t.operator == operator ? t : new JS.Binary(t.id, t.prefix, t.markers, t.left, operator, t.right, t.type); + } + } + + @Override + public String toString() { + return withPrefix(Space.EMPTY).printTrimmed(new JavaScriptPrinter<>()); + } + } + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) @Data diff --git a/src/main/java/org/openrewrite/javascript/tree/JsLeftPadded.java b/src/main/java/org/openrewrite/javascript/tree/JsLeftPadded.java index 48352bbb..898437fc 100644 --- a/src/main/java/org/openrewrite/javascript/tree/JsLeftPadded.java +++ b/src/main/java/org/openrewrite/javascript/tree/JsLeftPadded.java @@ -15,7 +15,10 @@ */ package org.openrewrite.javascript.tree; +import lombok.Getter; + public class JsLeftPadded { + @Getter public enum Location { BINARY_OPERATOR(JsSpace.Location.BINARY_PREFIX), BINDING_INITIALIZER(JsSpace.Location.BINDING_INITIALIZER_PREFIX), @@ -30,9 +33,5 @@ public enum Location { Location(JsSpace.Location beforeLocation) { this.beforeLocation = beforeLocation; } - - public JsSpace.Location getBeforeLocation() { - return beforeLocation; - } } } diff --git a/src/test/java/org/openrewrite/javascript/tree/BinaryTest.java b/src/test/java/org/openrewrite/javascript/tree/BinaryTest.java index bf18fd74..d6f59da2 100644 --- a/src/test/java/org/openrewrite/javascript/tree/BinaryTest.java +++ b/src/test/java/org/openrewrite/javascript/tree/BinaryTest.java @@ -15,6 +15,7 @@ */ package org.openrewrite.javascript.tree; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import org.openrewrite.Issue; @@ -128,4 +129,16 @@ void identityEquals(String arg) { ) ); } + + @Test + void in() { + rewriteRun( + javaScript( + """ + let foo = { bar : 'v1' , buz : 'v2' } + var x = 'bar' in foo + """ + ) + ); + } } diff --git a/src/test/java/org/openrewrite/javascript/tree/TypeOperatorTest.java b/src/test/java/org/openrewrite/javascript/tree/TypeOperatorTest.java index b74a30ff..c3dc849f 100644 --- a/src/test/java/org/openrewrite/javascript/tree/TypeOperatorTest.java +++ b/src/test/java/org/openrewrite/javascript/tree/TypeOperatorTest.java @@ -24,19 +24,6 @@ @SuppressWarnings("JSUnusedLocalSymbols") class TypeOperatorTest implements RewriteTest { - @ExpectedToFail - @Test - void in() { - rewriteRun( - javaScript( - """ - let foo = { bar : 'v1' , buz : 'v2' } - v = 'bar' in foo - """ - ) - ); - } - @Test void delete() { rewriteRun( From f51e0dc3a0b8bdebaa4e75f31c85dcdafe50880c Mon Sep 17 00:00:00 2001 From: Kun Li Date: Mon, 27 Nov 2023 13:30:50 -0800 Subject: [PATCH 2/3] Remove Js.Binary since it already has a Js.JsBinary and we will rename that to Js.Binary --- .../javascript/JavaScriptVisitor.java | 17 --- .../internal/JavaScriptPrinter.java | 21 +--- .../internal/TypeScriptParserVisitor.java | 6 +- .../org/openrewrite/javascript/tree/JS.java | 102 +----------------- 4 files changed, 8 insertions(+), 138 deletions(-) diff --git a/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java b/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java index c89d8ef5..11ab4dd7 100644 --- a/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java +++ b/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java @@ -108,23 +108,6 @@ public J visitArrowFunction(JS.ArrowFunction arrowFunction, P p) { return a; } - public J visitBinary(JS.Binary binary, P p) { - JS.Binary b = binary; - b = b.withPrefix(visitSpace(b.getPrefix(), Space.Location.BINARY_PREFIX, p)); - b = b.withMarkers(visitMarkers(b.getMarkers(), p)); - Expression temp = (Expression) visitExpression(b, p); - if (!(temp instanceof JS.Binary)) { - return temp; - } else { - b = (JS.Binary) temp; - } - b = b.withLeft(visitAndCast(b.getLeft(), p)); - b = b.getPadding().withOperator(visitLeftPadded(b.getPadding().getOperator(), JsLeftPadded.Location.BINARY_OPERATOR, p)); - b = b.withRight(visitAndCast(b.getRight(), p)); - b = b.withType(visitType(b.getType(), p)); - return b; - } - public J visitBinding(JS.ObjectBindingDeclarations.Binding binding, P p) { JS.ObjectBindingDeclarations.Binding b = binding; b = b.withPrefix(visitSpace(b.getPrefix(), JsSpace.Location.BINDING_PREFIX, p)); diff --git a/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java b/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java index 63b4109b..3c6b1f7b 100644 --- a/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java +++ b/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java @@ -101,24 +101,6 @@ public J visitArrowFunction(JS.ArrowFunction arrowFunction, PrintOutputCapture

p) { - beforeSyntax(binary, Space.Location.BINARY_PREFIX, p); - String keyword = ""; - switch (binary.getOperator()) { - case In: - keyword = "in"; - break; - } - visit(binary.getLeft(), p); - visitSpace(binary.getPadding().getOperator().getBefore(), Space.Location.BINARY_OPERATOR, p); - p.append(keyword); - - visit(binary.getRight(), p); - afterSyntax(binary, p); - return binary; - } - @Override public J visitBinding(JS.ObjectBindingDeclarations.Binding binding, PrintOutputCapture

p) { beforeSyntax(binding, JsSpace.Location.BINDING_PREFIX, p); @@ -230,6 +212,9 @@ public J visitJsBinary(JS.JsBinary binary, PrintOutputCapture

p) { case IdentityNotEquals: keyword = "!=="; break; + case In: + keyword = "in"; + break; } visitSpace(binary.getPadding().getOperator().getBefore(), JsSpace.Location.BINARY_PREFIX, p); diff --git a/src/main/java/org/openrewrite/javascript/internal/TypeScriptParserVisitor.java b/src/main/java/org/openrewrite/javascript/internal/TypeScriptParserVisitor.java index d0e61320..c6328756 100644 --- a/src/main/java/org/openrewrite/javascript/internal/TypeScriptParserVisitor.java +++ b/src/main/java/org/openrewrite/javascript/internal/TypeScriptParserVisitor.java @@ -297,7 +297,7 @@ private J visitBinary(TSCNode node) { Space opPrefix = whitespace(); JLeftPadded op = null; - JLeftPadded jsOp = null; + JLeftPadded jsOp = null; TSCSyntaxKind opKind = node.getNodeProperty("operatorToken").syntaxKind(); switch (opKind) { // Bitwise ops @@ -391,7 +391,7 @@ private J visitBinary(TSCNode node) { // TS/JS specific ops case InKeyword: consumeToken(TSCSyntaxKind.InKeyword); - jsOp = padLeft(opPrefix, JS.Binary.Type.In); + jsOp = padLeft(opPrefix, JS.JsBinary.Type.In); break; default: implementMe(node); @@ -400,7 +400,7 @@ private J visitBinary(TSCNode node) { Expression right = (Expression) visitNode(node.getNodeProperty("right")); if (jsOp != null) { - return new JS.Binary( + return new JS.JsBinary( randomId(), prefix, markers, diff --git a/src/main/java/org/openrewrite/javascript/tree/JS.java b/src/main/java/org/openrewrite/javascript/tree/JS.java index 49ec6cbd..cb14a7bf 100644 --- a/src/main/java/org/openrewrite/javascript/tree/JS.java +++ b/src/main/java/org/openrewrite/javascript/tree/JS.java @@ -403,105 +403,6 @@ public CoordinateBuilder.Statement getCoordinates() { } } - @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) - @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) - @AllArgsConstructor(access = AccessLevel.PRIVATE) - @Data - final class Binary implements JS, Expression, TypedTree { - - @Nullable - @NonFinal - transient WeakReference padding; - - @With - @EqualsAndHashCode.Include - UUID id; - - @With - Space prefix; - - @With - Markers markers; - - @With - Expression left; - - JLeftPadded operator; - - public Binary(UUID id, Space prefix, Markers markers, Expression left, JLeftPadded operator, Expression right, @Nullable JavaType type) { - this.id = id; - this.prefix = prefix; - this.markers = markers; - this.left = left; - this.operator = operator; - this.right = right; - this.type = type; - } - - public JS.Binary.Type getOperator() { - return operator.getElement(); - } - - public JS.Binary withOperator(JS.Binary.Type operator) { - return getPadding().withOperator(this.operator.withElement(operator)); - } - - @With - Expression right; - - @With - @Nullable - JavaType type; - - @Override - public

J acceptJavaScript(JavaScriptVisitor

v, P p) { - return v.visitBinary(this, p); - } - - @Transient - @Override - public CoordinateBuilder.Expression getCoordinates() { - return new CoordinateBuilder.Expression(this); - } - - public enum Type { - In, - } - - public JS.Binary.Padding getPadding() { - JS.Binary.Padding p; - if (this.padding == null) { - p = new JS.Binary.Padding(this); - this.padding = new WeakReference<>(p); - } else { - p = this.padding.get(); - if (p == null || p.t != this) { - p = new JS.Binary.Padding(this); - this.padding = new WeakReference<>(p); - } - } - return p; - } - - @RequiredArgsConstructor - public static class Padding { - private final JS.Binary t; - - public JLeftPadded getOperator() { - return t.operator; - } - - public JS.Binary withOperator(JLeftPadded operator) { - return t.operator == operator ? t : new JS.Binary(t.id, t.prefix, t.markers, t.left, operator, t.right, t.type); - } - } - - @Override - public String toString() { - return withPrefix(Space.EMPTY).printTrimmed(new JavaScriptPrinter<>()); - } - } - @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) @Data @@ -994,7 +895,8 @@ public CoordinateBuilder.Expression getCoordinates() { public enum Type { IdentityEquals, - IdentityNotEquals + IdentityNotEquals, + In } public JS.JsBinary.Padding getPadding() { From 7ac4be668e08b760c3d20b69136aa146408e063c Mon Sep 17 00:00:00 2001 From: Kun Li Date: Mon, 27 Nov 2023 13:33:58 -0800 Subject: [PATCH 3/3] clean up --- src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java | 2 +- .../org/openrewrite/javascript/internal/JavaScriptPrinter.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java b/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java index 11ab4dd7..3fa40dcc 100644 --- a/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java +++ b/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java @@ -26,7 +26,7 @@ import java.util.List; import java.util.Objects; -@SuppressWarnings({"unused", "DuplicatedCode"}) +@SuppressWarnings("unused") public class JavaScriptVisitor

extends JavaVisitor

{ @Override diff --git a/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java b/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java index 3c6b1f7b..e2fb4515 100644 --- a/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java +++ b/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java @@ -33,7 +33,7 @@ import java.util.List; import java.util.function.UnaryOperator; -@SuppressWarnings({"SameParameterValue", "SwitchStatementWithTooFewBranches"}) +@SuppressWarnings("SameParameterValue") public class JavaScriptPrinter

extends JavaScriptVisitor> { private static final UnaryOperator JAVA_SCRIPT_MARKER_WRAPPER =