Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -17,13 +17,15 @@

import lombok.Getter;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.tree.Comment;
import org.openrewrite.java.tree.Space;
import org.openrewrite.java.tree.TextComment;
import org.openrewrite.staticanalysis.java.JavaFileChecker;

public class DanglingDocCommentToBlockComment extends Recipe {

Expand All @@ -38,7 +40,9 @@ public class DanglingDocCommentToBlockComment extends Recipe {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {
// Kotlin, Groovy and JavaScript parse every comment, KDoc and JSDoc included, to a TextComment,
// so they would all look dangling. The javac lint only concerns Java sources anyway.
return Preconditions.check(new JavaFileChecker<>(), new JavaIsoVisitor<ExecutionContext>() {
@Override
public Space visitSpace(Space space, Space.Location loc, ExecutionContext ctx) {
// A leading comment is conventionally the license header, and javac does not flag it.
Expand All @@ -47,7 +51,7 @@ public Space visitSpace(Space space, Space.Location loc, ExecutionContext ctx) {
}
return space.withComments(ListUtils.map(space.getComments(), DanglingDocCommentToBlockComment::toBlockComment));
}
};
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.groovy.Assertions.groovy;
import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.kotlin.Assertions.kotlin;

class DanglingDocCommentToBlockCommentTest implements RewriteTest {

Expand Down Expand Up @@ -199,4 +201,36 @@ void a() {
)
);
}

@Test
void leaveKotlinAlone() {
rewriteRun(
kotlin(
"""
import java.util.UUID

/*******************************************************************
* KDoc is parsed as a plain comment, so it would otherwise look dangling.
*/
val id = UUID.randomUUID()
"""
)
);
}

@Test
void leaveGroovyAlone() {
rewriteRun(
groovy(
"""
import java.util.UUID

/*******************************************************************
* Groovydoc is parsed as a plain comment, so it would otherwise look dangling.
*/
def id = UUID.randomUUID()
"""
)
);
}
}
Loading