Skip to content

Commit

Permalink
Added support for ArrayBindingPattern.
Browse files Browse the repository at this point in the history
  • Loading branch information
traceyyoshima committed Nov 27, 2023
1 parent 017690f commit 438d5ac
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ public J visitAlias(JS.Alias alias, P p) {
return a;
}

public J visitArrayBindingPattern(JS.ArrayBindingPattern arrayBindingPattern, P p) {
JS.ArrayBindingPattern a = arrayBindingPattern;
a = a.withPrefix(visitSpace(a.getPrefix(), JsSpace.Location.ARRAY_BINDING_PATTERN_PREFIX, p));
a = a.withMarkers(visitMarkers(a.getMarkers(), p));
a = a.getPadding().withElements(visitContainer(a.getPadding().getElements(), JsContainer.Location.EXPORT_ELEMENT, p));
return a;
}

public J visitArrowFunction(JS.ArrowFunction arrowFunction, P p) {
JS.ArrowFunction a = arrowFunction;
a = a.withPrefix(visitSpace(a.getPrefix(), JsSpace.Location.ARROW_FUNCTION_PREFIX, p));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.openrewrite.marker.Markers;

import java.util.List;
import java.util.Optional;
import java.util.function.UnaryOperator;

@SuppressWarnings("SameParameterValue")
Expand Down Expand Up @@ -71,6 +72,14 @@ public J visitAlias(JS.Alias alias, PrintOutputCapture<P> p) {
return alias;
}

@Override
public J visitArrayBindingPattern(JS.ArrayBindingPattern arrayBindingPattern, PrintOutputCapture<P> p) {
beforeSyntax(arrayBindingPattern, JsSpace.Location.ARROW_FUNCTION_PREFIX, p);
visitContainer("[", arrayBindingPattern.getPadding().getElements(), JsContainer.Location.ARRAY_BINDING_ELEMENT, ",", "]", p);
afterSyntax(arrayBindingPattern, p);
return arrayBindingPattern;
}

@Override
public J visitArrowFunction(JS.ArrowFunction arrowFunction, PrintOutputCapture<P> p) {
beforeSyntax(arrowFunction, JsSpace.Location.ARROW_FUNCTION_PREFIX, p);
Expand Down Expand Up @@ -667,8 +676,11 @@ public J visitVariableDeclarations(J.VariableDeclarations multiVariable, PrintOu
}

if (multiVariable.getTypeExpression() != null) {
multiVariable.getMarkers().findFirst(TypeReferencePrefix.class).ifPresent(typeReferencePrefix -> visitSpace(typeReferencePrefix.getPrefix(), Space.Location.LANGUAGE_EXTENSION, p));
p.append(":");
Optional<TypeReferencePrefix> typeReferencePrefix = multiVariable.getMarkers().findFirst(TypeReferencePrefix.class);
if (typeReferencePrefix.isPresent()) {
visitSpace(typeReferencePrefix.get().getPrefix(), Space.Location.LANGUAGE_EXTENSION, p);
p.append(":");
}
visit(multiVariable.getTypeExpression(), p);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,20 @@ private J.AssignmentOperation visitAssignmentOperation(TSCNode node) {
}

private J visitArrayBindingPattern(TSCNode node) {
return unknown(node);
return new JS.ArrayBindingPattern(
randomId(),
whitespace(),
Markers.EMPTY,
mapContainer(
TSCSyntaxKind.OpenBracketToken,
node.getNodeListProperty("elements"),
TSCSyntaxKind.CommaToken,
TSCSyntaxKind.CloseBracketToken,
this::visitNode,
true
),
typeMapping.type(node)
);
}

private J visitArrowFunction(TSCNode node) {
Expand Down Expand Up @@ -2631,6 +2644,9 @@ private J.VariableDeclarations visitVariableDeclarationList(TSCNode node) {
J.Identifier name = null;
if (j instanceof J.Identifier) {
name = (J.Identifier) j;
} else if (j instanceof TypeTree) {
name = convertToIdentifier(EMPTY, "");
typeTree = (TypeTree) j;
} else {
implementMe(declaration);
}
Expand Down Expand Up @@ -2730,6 +2746,9 @@ private J.VariableDeclarations visitVariableStatement(TSCNode node) {
J.Identifier name = null;
if (j instanceof J.Identifier) {
name = (J.Identifier) j;
} else if (j instanceof TypeTree) {
name = convertToIdentifier(EMPTY, "");
typeTree = (TypeTree) j;
} else {
implementMe(declaration);
}
Expand Down Expand Up @@ -3265,7 +3284,12 @@ private <T extends J> JContainer<T> mapContainer(TSCSyntaxKind open, List<TSCNod
int saveCursor = getCursor();
T visited;
try {
visited = visitFn.apply(node);
if (node.syntaxKind() == TSCSyntaxKind.OmittedExpression) {
//noinspection unchecked
visited = (T) new J.Empty(randomId(), EMPTY, Markers.EMPTY);
} else {
visited = visitFn.apply(node);
}
} catch (Exception e) {
if (withUnknown) {
cursor(saveCursor);
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/org/openrewrite/javascript/tree/JS.java
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,79 @@ public JS.Alias withPropertyName(JRightPadded<J.Identifier> propertyName) {
}
}

@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
@RequiredArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
class ArrayBindingPattern implements JS, TypeTree {

@Nullable
@NonFinal
transient WeakReference<ArrayBindingPattern.Padding> padding;

@Getter
@With
@EqualsAndHashCode.Include
UUID id;

@Getter
@With
Space prefix;

@Getter
@With
Markers markers;

JContainer<J> elements;

@Getter
@With
@Nullable
JavaType type;

@Nullable
public List<J> getElements() {
return elements.getElements();
}

public ArrayBindingPattern withElements(List<J> elements) {
return getPadding().withElements(JContainer.withElements(this.elements, elements));
}

@Override
public <P> J acceptJavaScript(JavaScriptVisitor<P> v, P p) {
return v.visitArrayBindingPattern(this, p);
}

public ArrayBindingPattern.Padding getPadding() {
ArrayBindingPattern.Padding p;
if (this.padding == null) {
p = new ArrayBindingPattern.Padding(this);
this.padding = new WeakReference<>(p);
} else {
p = this.padding.get();
if (p == null || p.t != this) {
p = new ArrayBindingPattern.Padding(this);
this.padding = new WeakReference<>(p);
}
}
return p;
}

@RequiredArgsConstructor
public static class Padding {
private final ArrayBindingPattern t;

public JContainer<J> getElements() {
return t.elements;
}

public ArrayBindingPattern withElements(JContainer<J> elements) {
return t.elements == elements ? t : new ArrayBindingPattern(t.id, t.prefix, t.markers, elements, t.type);
}
}
}

/**
* A JavaScript `=>` is similar to a Java lambda, but additionally contains annotations, modifiers, type arguments.
* The ArrowFunction prevents J.Lambda recipes from transforming the LST because an ArrowFunction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class JsContainer {
@Getter
public enum Location {
ARRAY_LITERAL_EXPRESSION(JsSpace.Location.ARRAY_LITERAL_ELEMENTS, JsRightPadded.Location.ARRAY_LITERAL_ELEMENT_SUFFIX),
ARRAY_BINDING_ELEMENT(JsSpace.Location.ARRAY_BINDING_ELEMENTS, JsRightPadded.Location.ARRAY_BINDING_ELEMENT_SUFFIX),
BINDING_ELEMENT(JsSpace.Location.BINDING_ELEMENTS, JsRightPadded.Location.BINDING_ELEMENT_SUFFIX),
EXPORT_ELEMENT(JsSpace.Location.EXPORT_ELEMENTS, JsRightPadded.Location.EXPORT_ELEMENT_SUFFIX),
FUNCTION_TYPE_PARAMETER(JsSpace.Location.FUNCTION_TYPE_PARAMETERS, JsRightPadded.Location.FUNCTION_TYPE_PARAMETER_SUFFIX),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class JsRightPadded {
public enum Location {
ALIAS_PROPERTY_NAME(JsSpace.Location.ALIAS_PROPERTY_NAME_PREFIX),
ARRAY_LITERAL_ELEMENT_SUFFIX(JsSpace.Location.ARRAY_LITERAL_SUFFIX),
ARRAY_BINDING_ELEMENT_SUFFIX(JsSpace.Location.ARRAY_BINDING_ELEMENT_SUFFIX),
BINDING_ELEMENT_SUFFIX(JsSpace.Location.BINDING_SUFFIX),
BINDING_PROPERTY_NAME_SUFFIX(JsSpace.Location.BINDING_PROPERTY_NAME_SUFFIX),
EXPORT_ELEMENT_SUFFIX(JsSpace.Location.EXPORT_ELEMENT_SUFFIX),
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/openrewrite/javascript/tree/JsSpace.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public enum Location {
ARRAY_LITERAL_PREFIX,
ARRAY_LITERAL_ELEMENTS,
ARRAY_LITERAL_SUFFIX,
ARRAY_BINDING_PATTERN_PREFIX,
ARRAY_BINDING_ELEMENTS,
ARRAY_BINDING_ELEMENT_SUFFIX,
ARROW_FUNCTION_PREFIX,
BINARY_PREFIX,
BINDING_PREFIX,
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/org/openrewrite/javascript/tree/ForLoopTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.ExpectedToFail;
import org.junitpioneer.jupiter.Issue;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.javascript.Assertions.javaScript;
Expand Down Expand Up @@ -86,4 +87,17 @@ void destruct() {
)
);
}

@Issue("https://github.com/openrewrite/rewrite-javascript/issues/83")
@Test
void arrayBindingPattern() {
rewriteRun(
javaScript(
"""
for (const [ , ] of undefined) {
}
"""
)
);
}
}
3 changes: 2 additions & 1 deletion src/test/java/org/openrewrite/javascript/tree/TupleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.ExpectedToFail;
import org.openrewrite.Issue;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.javascript.Assertions.javaScript;
Expand Down Expand Up @@ -72,7 +73,7 @@ function concat(arr1, arr2) {
);
}

@ExpectedToFail("Requires ArrayBindingPattern.")
@Issue("https://github.com/openrewrite/rewrite-javascript/issues/83")
@Test
void arrayBindingPattern() {
rewriteRun(
Expand Down

0 comments on commit 438d5ac

Please sign in to comment.