From e587cd889d084fe8f913be67c72c109c052df18e Mon Sep 17 00:00:00 2001 From: Kushagra Nigam <93257324+TheMarvelFan@users.noreply.github.com> Date: Fri, 1 Dec 2023 21:18:38 +0530 Subject: [PATCH] Added Recipe for removing Empty XML tags (#3613) * Added Recipe for removing Empty XML tags Added the recipe to check for and remove empty tags with no children. Added a test for this recipe. * added recipe Added recipe to remove empty and self-closing tags in xml tree. * Restored ExecutionContextParameterName.java I changed this file really early by mistake, and forgot to remove this change from origin main. * Restored build.gradle.kts in rewrite-xml * added missing curly brace * Delete Unit) * Add Gradle license headers * Revert unintended changes * Remove `isSelfClosing` and inline `isEmptyTag` & visitor * Verify empty parents are already removed * Reuse existing RemoveContentVisitor * Inline `RemoveEmptyTagsVisitor`, as we already have `RemoveContentVisitor` * Remove excess cast * Apply suggestions from code review --------- Co-authored-by: Tim te Beek --- .../openrewrite/xml/RemoveContentVisitor.java | 2 +- .../openrewrite/xml/RemoveEmptyXmlTags.java | 47 ++++++++ .../xml/RemoveEmptyXmlTagsTest.java | 102 ++++++++++++++++++ .../org/openrewrite/xml/RemoveXmlTagTest.java | 56 ++++++++++ 4 files changed, 206 insertions(+), 1 deletion(-) create mode 100644 rewrite-xml/src/main/java/org/openrewrite/xml/RemoveEmptyXmlTags.java create mode 100644 rewrite-xml/src/test/java/org/openrewrite/xml/RemoveEmptyXmlTagsTest.java diff --git a/rewrite-xml/src/main/java/org/openrewrite/xml/RemoveContentVisitor.java b/rewrite-xml/src/main/java/org/openrewrite/xml/RemoveContentVisitor.java index 07fb859610f..e9822497e4b 100644 --- a/rewrite-xml/src/main/java/org/openrewrite/xml/RemoveContentVisitor.java +++ b/rewrite-xml/src/main/java/org/openrewrite/xml/RemoveContentVisitor.java @@ -41,7 +41,7 @@ public Xml visitTag(Xml.Tag tag, P p) { List contents = new ArrayList<>(t.getContent()); contents.remove(content); - if (removeEmptyAncestors && contents.isEmpty()) { + if (removeEmptyAncestors && contents.isEmpty() && t.getAttributes().isEmpty()) { if (getCursor().getParentOrThrow().getValue() instanceof Xml.Document) { return t.withContent(null).withClosing(null); } else { diff --git a/rewrite-xml/src/main/java/org/openrewrite/xml/RemoveEmptyXmlTags.java b/rewrite-xml/src/main/java/org/openrewrite/xml/RemoveEmptyXmlTags.java new file mode 100644 index 00000000000..f04ac2fd1a4 --- /dev/null +++ b/rewrite-xml/src/main/java/org/openrewrite/xml/RemoveEmptyXmlTags.java @@ -0,0 +1,47 @@ +/* + * 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.xml; + +import org.openrewrite.Recipe; +import org.openrewrite.ExecutionContext; +import org.openrewrite.TreeVisitor; +import org.openrewrite.xml.tree.Xml; + +public class RemoveEmptyXmlTags extends Recipe { + @Override + public String getDisplayName() { + return "Remove empty XML Tag"; + } + + @Override + public String getDescription() { + return "Removes XML tags that do not have attributes or children, including self closing tags."; + } + + @Override + public TreeVisitor getVisitor() { + return new XmlIsoVisitor() { + @Override + public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { + Xml.Tag t = super.visitTag(tag, ctx); + if (t != null && (t.getContent() == null || t.getContent().isEmpty()) && t.getAttributes().isEmpty()) { + doAfterVisit(new RemoveContentVisitor<>(t, true)); + } + return t; + } + }; + } +} diff --git a/rewrite-xml/src/test/java/org/openrewrite/xml/RemoveEmptyXmlTagsTest.java b/rewrite-xml/src/test/java/org/openrewrite/xml/RemoveEmptyXmlTagsTest.java new file mode 100644 index 00000000000..a6de07060fd --- /dev/null +++ b/rewrite-xml/src/test/java/org/openrewrite/xml/RemoveEmptyXmlTagsTest.java @@ -0,0 +1,102 @@ +/* + * 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.xml; + +import org.junit.jupiter.api.Test; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.xml.Assertions.xml; + +class RemoveEmptyXmlTagsTest implements RewriteTest { + @Test + void removeEmptyPluginRepositories() { + rewriteRun( + spec -> spec.recipe(new RemoveEmptyXmlTags()), + xml( + """ + + 4.0.0 + com.example + sample-app + 1.0-SNAPSHOT + + + + """, + """ + + 4.0.0 + com.example + sample-app + 1.0-SNAPSHOT + + """ + ) + ); + } + + @Test + void removeNestedEmptyTags() { + rewriteRun( + spec -> spec.recipe(new RemoveEmptyXmlTags()), + xml( + """ + + 4.0.0 + com.example + sample-app + 1.0-SNAPSHOT + + + + + + + + + """, + """ + + 4.0.0 + com.example + sample-app + 1.0-SNAPSHOT + + """ + ) + ); + } + + @Test + void retainWhenThereAttributes() { + rewriteRun( + spec -> spec.recipe(new RemoveEmptyXmlTags()), + xml( + """ + + 4.0.0 + com.example + sample-app + 1.0-SNAPSHOT + + + + + """ + ) + ); + } +} \ No newline at end of file diff --git a/rewrite-xml/src/test/java/org/openrewrite/xml/RemoveXmlTagTest.java b/rewrite-xml/src/test/java/org/openrewrite/xml/RemoveXmlTagTest.java index 1eddad69405..5737af78f46 100644 --- a/rewrite-xml/src/test/java/org/openrewrite/xml/RemoveXmlTagTest.java +++ b/rewrite-xml/src/test/java/org/openrewrite/xml/RemoveXmlTagTest.java @@ -102,4 +102,60 @@ void fileMatcherEmpty() { ) ); } + + @Test + void removeEmptyParentTag() { + rewriteRun( + spec -> spec.recipe(new RemoveXmlTag("//project/build/pluginManagement/plugins/plugin", null)), + xml( + """ + + 4.0.0 + com.example + sample-app + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 11 + + + + + + """, + """ + + 4.0.0 + com.example + sample-app + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 11 + + + + + + """ + ) + ); + } }