Skip to content

MigrateCollectionsUnmodifiableList should retain item comments #805

@protocol7

Description

@protocol7

What version of OpenRewrite are you using?

  • org.openrewrite:rewrite-core:8.59.1
  • org.openrewrite.recipe:rewrite-migrate-java:3.14.1

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

MigrateCollectionsUnmodifiableList should ensure that comments for list items are retained. Comments are used to explain each item for users and should not be removed.

  @Test
  public void listOfKeepComments() {
    rewriteRun(
        spec -> spec.recipe(new MigrateCollectionsUnmodifiableList()),
        java(
            """
            package com.helloworld;

            import java.util.Collections;
            import java.util.Arrays;
            import java.util.List;

            public static class Foo {
              List<String> bar = Collections.unmodifiableList(Arrays.asList(
                "1", // this is one
                "2" // this is two
              ));
            }
            """, """
            package com.helloworld;

            import java.util.List;

            public static class Foo {
              List<String> bar = List.of(
                "1", // this is one
                "2" // this is two
              );
            }
            """,
            s -> s.markers(new JavaVersion(randomId(), "", "", "9", "9"))));
  }
org.opentest4j.AssertionFailedError: [Unexpected result in "com/helloworld/Foo.java":
diff --git a/com/helloworld/Foo.java b/com/helloworld/Foo.java
index 015d29e..4a19e74 100644
--- a/com/helloworld/Foo.java
+++ b/com/helloworld/Foo.java
@@ -3,8 +3,5 @@
 import java.util.List;

 public static class Foo {
-  List<String> bar = List.of(
-    "1", // this is one
-    "2" // this is two
-  );
+  List<String> bar = List.of("1", "2");
 }
\ No newline at end of file
]
expected:
  "package com.helloworld;

  import java.util.List;

  public static class Foo {
    List<String> bar = List.of(
      "1", // this is one
      "2" // this is two
    );
  }"
 but was:
  "package com.helloworld;

  import java.util.List;

  public static class Foo {
    List<String> bar = List.of("1", "2");
  }"
	at org.openrewrite.test.RewriteTest.assertContentEquals(RewriteTest.java:622)
	at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:511)
	at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:130)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions