Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JS.Binary to support TS/JS specific binary type like in #84

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading