Avoid line continuation escapes in text blocks with mixed newline content by Jenson3210 · Pull Request #975 · openrewrite/rewrite-migrate-java (original) (raw)

Summary

Problem

When converting SQL query string concatenations to text blocks, the recipe inserts \ (line continuation) escapes between adjacent strings that aren't separated by \n. In a mixed concatenation like:

String query = "select count(1)\n" + "from my_table\n" + "where a.id = b.id\n" + "and a.stat_cd = 'ACTV'" + " and a.del_fl = 'N'" + " and a.rgn = :region";

The recipe would produce unexpected \ characters:

String query = """ ... and a.stat_cd = 'ACTV'
and a.del_fl = 'N'
and a.rgn = :region""";

While semantically correct, the \ line continuation is unfamiliar to many developers and perceived as a bug.

Solution

Added a contentHasNewlines guard to the passPhrase insertion logic. When the concatenated content already contains interior newlines (mixed case), non-newline-joined strings are simply concatenated on the same text block line instead of being split with \ continuations:

String query = """ ... and a.stat_cd = 'ACTV' and a.del_fl = 'N' and a.rgn = :region""";

Test plan