openrewrite / rewrite

Automated mass refactoring of source code.
https://docs.openrewrite.org
Apache License 2.0
1.97k stars 291 forks source link

ShortenFullyQualifiedTypeReferences removes file header from groovy #3736

Closed Bananeweizen closed 7 months ago

Bananeweizen commented 7 months ago

What version of OpenRewrite are you using?

I am using

What is the smallest, simplest way to reproduce the problem?

/*
 * header comment will be removed from this groovy script, but not from similar java file
 */
import java.util.regex.Pattern

def pattern = Pattern.compile("pattern")
def dependency = new org.apache.maven.model.Dependency()

What did you see instead?

import org.apache.maven.model.Dependency

import java.util.regex.Pattern

def pattern = Pattern.compile("pattenr")
def dependency = new Dependency()

The shortening of FQNs has worked fine, but the comment at the top is gone. I've tried creating a similar java reproducer with all kinds of comment position and style variations, but the comment was never deleted in the java files.

Are you interested in contributing a fix to OpenRewrite?

Probably not. However, I believe I have seen code in other recipes to explicitly copy comments during replacement, so maybe I can find something that might match this problem.

knutwannheden commented 7 months ago

Interesting! Thanks for the report. I will try to look into it tomorrow.

knutwannheden commented 7 months ago

I pushed a commit which I think fixes the issue, but I still want to investigate some more.