Skip to content

Commit

Permalink
Added JS.Tuple.
Browse files Browse the repository at this point in the history
Updated mapModifiers to create J.Modifier with LanguageExtension.
Updated various JS modifiers from J.Annotation to J.Modifier.
Updated leading and trailing annotation to work with changes from J.Annotation to J.Modifier.
  • Loading branch information
traceyyoshima committed Nov 21, 2023
1 parent eb23bb0 commit 58c9086
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public JS.TemplateExpression.Value visitTemplateExpressionValue(JS.TemplateExpre
return (JS.TemplateExpression.Value) super.visitTemplateExpressionValue(value, p);
}

@Override
public JS.Tuple visitTuple(JS.Tuple tuple, P p) {
return (JS.Tuple) super.visitTuple(tuple, p);
}

@Override
public JS.TypeDeclaration visitTypeDeclaration(JS.TypeDeclaration typeDeclaration, P p) {
return (JS.TypeDeclaration) super.visitTypeDeclaration(typeDeclaration, p);
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,20 @@ public J visitTemplateExpressionValue(JS.TemplateExpression.Value value, P p) {
return s;
}

public J visitTuple(JS.Tuple tuple, P p) {
JS.Tuple t = tuple;
t = t.withPrefix(visitSpace(t.getPrefix(), JsSpace.Location.TUPLE_PREFIX, p));
t = t.withMarkers(visitMarkers(t.getMarkers(), p));
Expression temp = (Expression) visitExpression(t, p);
if (!(temp instanceof JS.Tuple)) {
return temp;
} else {
t = (JS.Tuple) temp;
}
t = t.withType(visitType(t.getType(), p));
return t;
}

public J visitTypeDeclaration(JS.TypeDeclaration typeDeclaration, P p) {
JS.TypeDeclaration t = typeDeclaration;
t = t.withPrefix(visitSpace(t.getPrefix(), JsSpace.Location.TYPE_DECLARATION_PREFIX, p));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,14 @@ public J visitTemplateExpressionValue(JS.TemplateExpression.Value value, PrintOu
return value;
}

@Override
public J visitTuple(JS.Tuple tuple, PrintOutputCapture<P> p) {
beforeSyntax(tuple, JsSpace.Location.TUPLE_PREFIX, p);
visitContainer("[", tuple.getPadding().getTypes(), JsContainer.Location.TUPLE_ELEMENT, ",", "]", p);
afterSyntax(tuple, p);
return tuple;
}

@Override
public J visitTypeDeclaration(JS.TypeDeclaration typeDeclaration, PrintOutputCapture<P> p) {
beforeSyntax(typeDeclaration, JsSpace.Location.TYPE_DECLARATION_PREFIX, p);
Expand Down Expand Up @@ -589,6 +597,8 @@ public void visitModifier(J.Modifier mod, PrintOutputCapture<P> p) {
case Volatile:
keyword = "volatile";
break;
default:
keyword = mod.getKeyword();
}
beforeSyntax(mod, Space.Location.MODIFIER_PREFIX, p);
p.append(keyword);
Expand Down

Large diffs are not rendered by default.

75 changes: 75 additions & 0 deletions src/main/java/org/openrewrite/javascript/tree/JS.java
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,81 @@ public TemplateExpression withTag(@Nullable JRightPadded<Expression> tag) {
}
}

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

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

@EqualsAndHashCode.Include
@With
UUID id;

@With
Space prefix;

@With
Markers markers;

JContainer<J> types;

@With
@Nullable
JavaType type;

public List<J> getTypes() {
return types.getElements();
}

public Tuple withBindings(List<J> types) {
return getPadding().withTypes(JContainer.withElements(this.types, types));
}

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

@Transient
@Override
public CoordinateBuilder.Expression getCoordinates() {
return new CoordinateBuilder.Expression(this);
}

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

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

public JContainer<J> getTypes() {
return t.types;
}

public Tuple withTypes(JContainer<J> types) {
return t.types == types ? t : new Tuple(t.id, t.prefix, t.markers, types, t.type);
}
}
}

@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
@RequiredArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public enum Location {
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),
IMPORT_ELEMENT(JsSpace.Location.IMPORT_ELEMENTS, JsRightPadded.Location.IMPORT_ELEMENT_SUFFIX);
IMPORT_ELEMENT(JsSpace.Location.IMPORT_ELEMENTS, JsRightPadded.Location.IMPORT_ELEMENT_SUFFIX),
TUPLE_ELEMENT(JsSpace.Location.TUPLE_ELEMENT, JsRightPadded.Location.TUPLE_ELEMENT_SUFFIX);

private final JsSpace.Location beforeLocation;
private final JsRightPadded.Location elementLocation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public enum Location {
IMPORT_ELEMENT_SUFFIX(JsSpace.Location.IMPORT_ELEMENT_SUFFIX),
IMPORT_NAME_SUFFIX(JsSpace.Location.IMPORT_NAME_SUFFIX),
TAG(JsSpace.Location.TAG_SUFFIX),
TUPLE_ELEMENT_SUFFIX(JsSpace.Location.TUPLE_ELEMENT_SUFFIX),
UNION_TYPE(JsSpace.Location.UNION_TYPE_SUFFIX);

private final JsSpace.Location afterLocation;
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 @@ -53,6 +53,9 @@ public enum Location {
TEMPLATE_EXPRESSION_PREFIX,
TEMPLATE_EXPRESSION_VALUE_PREFIX,
TEMPLATE_EXPRESSION_VALUE_SUFFIX,
TUPLE_PREFIX,
TUPLE_ELEMENT,
TUPLE_ELEMENT_SUFFIX,
TYPE_DECLARATION_PREFIX,
TYPE_DECLARATION_INITIALIZER_PREFIX,
TYPE_OPERATOR_PREFIX,
Expand Down
45 changes: 41 additions & 4 deletions src/test/java/org/openrewrite/javascript/tree/TupleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
@SuppressWarnings({"JSUnresolvedVariable", "JSUnusedLocalSymbols"})
class TupleTest implements RewriteTest {

@ExpectedToFail
@Test
void emptyTuple() {
rewriteRun(
Expand All @@ -36,19 +35,17 @@ void emptyTuple() {
);
}

@ExpectedToFail
@Test
void tuple() {
rewriteRun(
javaScript(
"""
let tuple : [ number , boolean , ] = [ 1 , true ]
let tuple : [ number , boolean ] = [ 1 , true ]
"""
)
);
}

@ExpectedToFail
@Test
void typed() {
rewriteRun(
Expand All @@ -59,4 +56,44 @@ void typed() {
)
);
}

@ExpectedToFail("Requires spread operator")
@Test
void spreadOperators() {
rewriteRun(
javaScript(
"""
function concat(arr1, arr2) {
return [...arr1, ...arr2]
}
"""
)
);
}

@ExpectedToFail("Requires ArrayBindingPattern.")
@Test
void arrayBindingPattern() {
rewriteRun(
javaScript(
"""
function tail(arg) {
const [_, ...result] = arg;
return result;
}
"""
)
);
}

@Test
void trailingCommas() {
rewriteRun(
javaScript(
"""
let input : [ number , boolean , ] = [ 1 , true , ]
"""
)
);
}
}

0 comments on commit 58c9086

Please sign in to comment.