xml: extend RemoveEmptyXmlTags with XPath whitelist, file matcher, and file deletion#7834
Open
Jenson3210 wants to merge 2 commits into
Open
xml: extend RemoveEmptyXmlTags with XPath whitelist, file matcher, and file deletion#7834Jenson3210 wants to merge 2 commits into
RemoveEmptyXmlTags with XPath whitelist, file matcher, and file deletion#7834Jenson3210 wants to merge 2 commits into
Conversation
…and file deletion Adds three optional, JSON-deserializable options to `RemoveEmptyXmlTags`: - `xPaths` — whitelist of XPath expressions; attribute predicates act as an attribute allowlist (`@*` wildcard, `@a`/`@a or @b` enumerated names), so a tag matches only when its attribute set is a subset of the allowed names. Empty/null preserves the existing "remove empty no-attribute tag" behavior. - `fileMatcher` — restrict the recipe to matching files. - `deleteFileIfEmpty` — when the root tag would itself be removed, delete the source file. Defaults to true. The visitor runs inside `Repeat.repeatUntilStable` so nested empties cascade and trigger file deletion in a single recipe run. The original no-arg constructor is preserved via Lombok `@NoArgsConstructor(force = true)` so existing callers and tests are unaffected.
timtebeek
reviewed
May 29, 2026
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Extends
org.openrewrite.xml.RemoveEmptyXmlTagswith three optional, JSON-deserializable options so it can serve as the generic cleanup recipe described in moderneinc/customer-requests#2460 §11:xPaths— whitelist of XPath expressions identifying which empty tags are eligible for removal. Attribute predicates are reinterpreted as an attribute allowlist: a candidate tag matches only when its attribute set is a subset of the union of attribute names appearing in the matched predicates.@*is the wildcard,@nameenumerates a single allowed name, and@a or @ballows either. So/serveronly matches attribute-free<server>tags, while/server[@*]matches any,/server[@description]matches no-attribute or description-only servers, and/server[@description or @other]matches when attributes are a subset of{description, other}.fileMatcher— glob restricting which files the recipe touches (wired viaPreconditions.check(new FindSourceFiles(...), …)).deleteFileIfEmpty— when the root tag itself would be a removal candidate, delete the entire source file. Defaults totrue.The visitor is wrapped in
Repeat.repeatUntilStable(..., 50)so nested empties cascade and trigger file deletion in a single recipe run. The original no-arg constructor is preserved via Lombok@NoArgsConstructor(force = true), and the explicit all-args constructor is@JsonCreator-annotated. Existing callers and tests are unaffected — the defaults reproduce the prior behavior on documents whose root retains content.Motivating example (Liberty
mp-telemetry.xmlcleanup once another recipe strips<feature>mpTelemetry-2.0</feature>):collapses the now-empty
<featureManager>, then<server>(the[@*]opts in to itsdescriptionattribute), then deletes the file.Test plan
./gradlew :rewrite-xml:test --tests "org.openrewrite.xml.RemoveEmptyXmlTagsTest"— 14 tests pass (3 original + 11 new covering file deletion, attribute-allowlist semantics,fileMatcherscoping, and multi-level nested collapse)../gradlew :rewrite-xml:recipeCsvGenerateregeneratesrewrite-xml/.../recipes.csv;recipeCsvValidateContentpasses../gradlew :rewrite-xml:licenseMain :rewrite-xml:licenseTestpass.