diff --git a/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java b/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java index 4f00fec2..a4d08df9 100644 --- a/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java +++ b/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java @@ -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)); diff --git a/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java b/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java index dff072a5..be582f55 100644 --- a/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java +++ b/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java @@ -135,6 +135,15 @@ public J visitDefaultType(JS.DefaultType defaultType, PrintOutputCapture

p) { return defaultType; } + @Override + public J visitDelete(JS.Delete delete, PrintOutputCapture

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) { beforeSyntax(export, JsSpace.Location.EXPORT_PREFIX, p); diff --git a/src/main/java/org/openrewrite/javascript/internal/TypeScriptParserVisitor.java b/src/main/java/org/openrewrite/javascript/internal/TypeScriptParserVisitor.java index b01d21d4..e872464f 100644 --- a/src/main/java/org/openrewrite/javascript/internal/TypeScriptParserVisitor.java +++ b/src/main/java/org/openrewrite/javascript/internal/TypeScriptParserVisitor.java @@ -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 body = maybeSemicolon((Statement) visitNode(node.getNodeProperty("statement"))); @@ -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; diff --git a/src/main/java/org/openrewrite/javascript/tree/JS.java b/src/main/java/org/openrewrite/javascript/tree/JS.java index dde624b0..f867cd79 100644 --- a/src/main/java/org/openrewrite/javascript/tree/JS.java +++ b/src/main/java/org/openrewrite/javascript/tree/JS.java @@ -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

J acceptJavaScript(JavaScriptVisitor

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 diff --git a/src/main/java/org/openrewrite/javascript/tree/JsSpace.java b/src/main/java/org/openrewrite/javascript/tree/JsSpace.java index 0d1adaab..83fc84be 100644 --- a/src/main/java/org/openrewrite/javascript/tree/JsSpace.java +++ b/src/main/java/org/openrewrite/javascript/tree/JsSpace.java @@ -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, diff --git a/src/test/java/org/openrewrite/javascript/tree/DeleteTest.java b/src/test/java/org/openrewrite/javascript/tree/DeleteTest.java new file mode 100644 index 00000000..9d6e495e --- /dev/null +++ b/src/test/java/org/openrewrite/javascript/tree/DeleteTest.java @@ -0,0 +1,34 @@ +/* + * Copyright 2023 the original author or authors. + *

+ * 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 + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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) + """ + ) + ); + } + +}