-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
1 parent
777a0f3
commit e587cd8
Showing
4 changed files
with
206 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
rewrite-xml/src/main/java/org/openrewrite/xml/RemoveEmptyXmlTags.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* 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.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<?, ExecutionContext> getVisitor() { | ||
return new XmlIsoVisitor<ExecutionContext>() { | ||
@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; | ||
} | ||
}; | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
rewrite-xml/src/test/java/org/openrewrite/xml/RemoveEmptyXmlTagsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* 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.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( | ||
""" | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.example</groupId> | ||
<artifactId>sample-app</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<pluginRepositories> | ||
</pluginRepositories> | ||
</project> | ||
""", | ||
""" | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.example</groupId> | ||
<artifactId>sample-app</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
</project> | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void removeNestedEmptyTags() { | ||
rewriteRun( | ||
spec -> spec.recipe(new RemoveEmptyXmlTags()), | ||
xml( | ||
""" | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.example</groupId> | ||
<artifactId>sample-app</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<build> | ||
<pluginManagement> | ||
<plugins> | ||
<plugin/> | ||
</plugins> | ||
</pluginManagement> | ||
</build> | ||
</project> | ||
""", | ||
""" | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.example</groupId> | ||
<artifactId>sample-app</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
</project> | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void retainWhenThereAttributes() { | ||
rewriteRun( | ||
spec -> spec.recipe(new RemoveEmptyXmlTags()), | ||
xml( | ||
""" | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.example</groupId> | ||
<artifactId>sample-app</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<build attr="true"> | ||
</build> | ||
<dependencies attr="false" /> | ||
</project> | ||
""" | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters