Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove maven properties should remove comment #4554

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private class RemovePropertyVisitor extends MavenVisitor<ExecutionContext> {
@Override
public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
if (isPropertyTag() && propertyName.equals(tag.getName())) {
doAfterVisit(new RemoveContentVisitor<>(tag, true));
doAfterVisit(new RemoveContentVisitor<>(tag, true, true));
maybeUpdateModel();
}
return super.visitTag(tag, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ void removeProperty() {
"""
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>

<properties>
<a.version>a</a.version>
<bla.version>b</bla.version>
Expand All @@ -55,11 +55,11 @@ void removeProperty() {
"""
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>

<properties>
<a.version>a</a.version>
</properties>
Expand All @@ -83,11 +83,11 @@ void removeOnlyProperty() {
"""
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>

<properties>
<bla.version>b</bla.version>
</properties>
Expand All @@ -96,7 +96,7 @@ void removeOnlyProperty() {
"""
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
Expand All @@ -111,4 +111,110 @@ void removeOnlyProperty() {
)
);
}

@Test
void removePropertyWithComment() {
rewriteRun(
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>

<properties>
<a.version>a</a.version>
<!-- I should remove this property -->
<bla.version>b</bla.version>
</properties>
</project>
""",
"""
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>

<properties>
<a.version>a</a.version>
</properties>
</project>
""",
sourceSpecs ->
sourceSpecs.afterRecipe(d -> {
MavenResolutionResult resolution = d.getMarkers().findFirst(MavenResolutionResult.class).orElseThrow();
Map<String, String> properties = resolution.getPom().getRequested().getProperties();
assertThat(properties.get("a.version")).isEqualTo("a");
assertThat(properties.get("bla.version")).isNull();
})
)
);
}

@Test
void removePropertyWithCommentAndEmptyParents() {
rewriteRun(
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>

<properties>
<!-- I should remove this property -->
<bla.version>b</bla.version>
</properties>
</project>
""",
"""
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</project>
"""
)
);
}

@Test
void removePropertyWithTwoComments() {
rewriteRun(
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>

<properties>
<!-- And also remove this comment -->
<!-- I should remove this property -->
<bla.version>b</bla.version>
</properties>
</project>
""",
"""
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</project>
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,17 @@ public class RemoveContentVisitor<P> extends XmlVisitor<P> {

private final Content scope;
private final boolean removeEmptyAncestors;
private final boolean removePrecedingComment;

@Deprecated
public RemoveContentVisitor(Content tag, boolean removeEmptyAncestors) {
this(tag, removeEmptyAncestors, false);
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
}

public RemoveContentVisitor(Content tag, boolean removeEmptyAncestors, boolean removePrecedingComment) {
this.scope = tag;
this.removeEmptyAncestors = removeEmptyAncestors;
this.removePrecedingComment = removePrecedingComment;
}

@Override
Expand All @@ -39,13 +46,20 @@ public Xml visitTag(Xml.Tag tag, P p) {
for (Content content : t.getContent()) {
if (scope.isScope(content)) {
List<Content> contents = new ArrayList<>(t.getContent());
contents.remove(content);
int indexOf = contents.indexOf(content);
contents.remove(indexOf);

if (removePrecedingComment) {
if (0 < indexOf && contents.get(indexOf - 1) instanceof Xml.Comment) {
doAfterVisit(new RemoveContentVisitor<>(contents.get(indexOf - 1), true, removePrecedingComment));
}
}

if (removeEmptyAncestors && contents.isEmpty() && t.getAttributes().isEmpty()) {
if (getCursor().getParentOrThrow().getValue() instanceof Xml.Document) {
return t.withContent(null).withClosing(null);
} else {
doAfterVisit(new RemoveContentVisitor<>(t, true));
doAfterVisit(new RemoveContentVisitor<>(t, true, removePrecedingComment));
}
} else {
return t.withContent(contents);
Expand Down
Loading