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 1 commit
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
19 changes: 18 additions & 1 deletion src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.List;
import java.util.Objects;

@SuppressWarnings("unused")
@SuppressWarnings({"unused", "DuplicatedCode"})
public class JavaScriptVisitor<P> extends JavaVisitor<P> {

@Override
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import java.util.List;
import java.util.function.UnaryOperator;

@SuppressWarnings("SameParameterValue")
@SuppressWarnings({"SameParameterValue", "SwitchStatementWithTooFewBranches"})
public class JavaScriptPrinter<P> extends JavaScriptVisitor<PrintOutputCapture<P>> {

private static final UnaryOperator<String> JAVA_SCRIPT_MARKER_WRAPPER =
Expand Down Expand Up @@ -101,6 +101,24 @@ public J visitArrowFunction(JS.ArrowFunction arrowFunction, PrintOutputCapture<P
return arrowFunction;
}

@Override
public J visitBinary(JS.Binary binary, PrintOutputCapture<P> 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> p) {
beforeSyntax(binding, JsSpace.Location.BINDING_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.Binary.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.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) {
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
99 changes: 99 additions & 0 deletions src/main/java/org/openrewrite/javascript/tree/JS.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think JsBinary should be renamed instead of adding a Binary class. The change will require an update in moderne-ast-write to handle existing JsBinary in old LSTs.

Alternatively, we can add a new issue and handle that later, while adding to the existing class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as discussed, this PR will update JsBinary instead of adding Binary and we will rename it later in a separate PR


@Nullable
@NonFinal
transient WeakReference<JS.Binary.Padding> padding;

@With
@EqualsAndHashCode.Include
UUID id;

@With
Space prefix;

@With
Markers markers;

@With
Expression left;

JLeftPadded<JS.Binary.Type> operator;

public Binary(UUID id, Space prefix, Markers markers, Expression left, JLeftPadded<Type> 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 <P> J acceptJavaScript(JavaScriptVisitor<P> 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<JS.Binary.Type> getOperator() {
return t.operator;
}

public JS.Binary withOperator(JLeftPadded<JS.Binary.Type> 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
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