Skip to content

Commit

Permalink
Support delete expression
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Oct 23, 2023
1 parent b8a1206 commit 0e16f72
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 0 deletions.
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 @@ -145,6 +145,20 @@ public J visitDefaultType(JS.DefaultType defaultType, P p) {
return d;
}

public J visitDelete(JS.Delete delete, P p) {
JS.Delete d = delete;
d = d.withPrefix(visitSpace(d.getPrefix(), JsSpace.Location.DELETE_PREFIX, p));
d = d.withMarkers(visitMarkers(d.getMarkers(), p));
Expression temp = (Expression) visitExpression(d, p);
if (!(temp instanceof JS.Delete)) {
return temp;
} else {
d = (JS.Delete) temp;
}
d = d.withType(visitType(d.getType(), p));
return d;
}

public J visitExport(JS.Export export, P p) {
JS.Export e = export;
e = e.withPrefix(visitSpace(e.getPrefix(), JsSpace.Location.EXPORT_PREFIX, p));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ public J visitDefaultType(JS.DefaultType defaultType, PrintOutputCapture<P> p) {
return defaultType;
}

@Override
public J visitDelete(JS.Delete delete, PrintOutputCapture<P> p) {
beforeSyntax(delete, JsSpace.Location.DELETE_PREFIX, p);
p.append("delete");
visit(delete.getExpression(), p);
afterSyntax(delete, p);
return delete;
}

@Override
public J visitExport(JS.Export export, PrintOutputCapture<P> p) {
beforeSyntax(export, JsSpace.Location.EXPORT_PREFIX, p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,17 @@ private J visitDefaultClause(TSCNode node) {
);
}

private J visitDeleteExpression(TSCNode node) {
Space prefix = sourceBefore(TSCSyntaxKind.DeleteKeyword);
return new JS.Delete(
randomId(),
prefix,
Markers.EMPTY,
(Expression) visitNode(node.getNodeProperty("expression")),
typeMapping.type(node)
);
}

private J.DoWhileLoop visitDoStatement(TSCNode node) {
Space prefix = sourceBefore(TSCSyntaxKind.DoKeyword);
JRightPadded<Statement> body = maybeSemicolon((Statement) visitNode(node.getNodeProperty("statement")));
Expand Down Expand Up @@ -2906,6 +2917,9 @@ private J visitNode(@Nullable TSCNode node) {
case DefaultClause:
j = visitDefaultClause(node);
break;
case DeleteExpression:
j = visitDeleteExpression(node);
break;
case DoStatement:
j = visitDoStatement(node);
break;
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/org/openrewrite/javascript/tree/JS.java
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,35 @@ public CoordinateBuilder.Expression getCoordinates() {
}
}

@SuppressWarnings("unchecked")
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
@Data
@With
final class Delete implements JS, Expression, Statement {

@EqualsAndHashCode.Include
UUID id;

Space prefix;
Markers markers;
Expression expression;

@Nullable
JavaType type;

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

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

@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
@RequiredArgsConstructor
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/openrewrite/javascript/tree/JsSpace.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public enum Location {
BINDING_INITIALIZER_PREFIX,
BINDING_PROPERTY_NAME_SUFFIX,
DEFAULT_TYPE_PREFIX,
DELETE_PREFIX,
EXPORT_PREFIX,
EXPORT_ELEMENTS,
EXPORT_ELEMENT_SUFFIX,
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/org/openrewrite/javascript/tree/DeleteTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.javascript.tree;

import org.junit.jupiter.api.Test;

@SuppressWarnings({"JSUnusedLocalSymbols", "JSUnresolvedVariable"})
class DeleteTest extends ParserTest {

@Test
void delete() {
rewriteRun(
javaScript(
"""
delete console.log(1)
"""
)
);
}

}

0 comments on commit 0e16f72

Please sign in to comment.