diff --git a/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java b/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java index 9ea557e5..e2fb4515 100644 --- a/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java +++ b/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java @@ -212,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 7288174c..c6328756 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.JsBinary.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.JsBinary( + 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..cb14a7bf 100644 --- a/src/main/java/org/openrewrite/javascript/tree/JS.java +++ b/src/main/java/org/openrewrite/javascript/tree/JS.java @@ -895,7 +895,8 @@ public CoordinateBuilder.Expression getCoordinates() { public enum Type { IdentityEquals, - IdentityNotEquals + IdentityNotEquals, + In } public JS.JsBinary.Padding getPadding() { 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 398ae29d..20a5c779 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(