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