Skip to content

Commit

Permalink
Merge pull request #84 from openrewrite/JsBinary
Browse files Browse the repository at this point in the history
Add JS.Binary to support TS/JS specific binary type like `in`
  • Loading branch information
kunli2 authored Nov 27, 2023
2 parents 017690f + 7ac4be6 commit 7b6c85f
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ public J visitJsBinary(JS.JsBinary binary, PrintOutputCapture<P> p) {
case IdentityNotEquals:
keyword = "!==";
break;
case In:
keyword = "in";
break;
}

visitSpace(binary.getPadding().getOperator().getBefore(), JsSpace.Location.BINARY_PREFIX, p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<J.Binary.Type> op = null;
JLeftPadded<JS.JsBinary.Type> jsOp = null;
TSCSyntaxKind opKind = node.getNodeProperty("operatorToken").syntaxKind();
switch (opKind) {
// Bitwise ops
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -428,6 +447,7 @@ private J visitBinaryExpression(TSCNode node) {
case GreaterThanEqualsToken:
case GreaterThanGreaterThanToken:
case GreaterThanGreaterThanGreaterThanToken:
case InKeyword:
case LessThanToken:
case LessThanEqualsToken:
case LessThanLessThanToken:
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/openrewrite/javascript/tree/JS.java
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,8 @@ public CoordinateBuilder.Expression getCoordinates() {

public enum Type {
IdentityEquals,
IdentityNotEquals
IdentityNotEquals,
In
}

public JS.JsBinary.Padding getPadding() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -30,9 +33,5 @@ public enum Location {
Location(JsSpace.Location beforeLocation) {
this.beforeLocation = beforeLocation;
}

public JsSpace.Location getBeforeLocation() {
return beforeLocation;
}
}
}
13 changes: 13 additions & 0 deletions src/test/java/org/openrewrite/javascript/tree/BinaryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -128,4 +129,16 @@ void identityEquals(String arg) {
)
);
}

@Test
void in() {
rewriteRun(
javaScript(
"""
let foo = { bar : 'v1' , buz : 'v2' }
var x = 'bar' in foo
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 7b6c85f

Please sign in to comment.