Avoid line continuation escapes in text blocks with mixed newline content by Jenson3210 · Pull Request #975 · openrewrite/rewrite-migrate-java (original) (raw)
Summary
- When
UseTextBlocksconverts a string concatenation that mixes newline-terminated strings with non-newline strings, it no longer inserts\line continuations for the non-newline joins - Instead, those parts are placed on the same text block line
- Line continuations are still used when the entire content has no newlines, where they provide useful visual structure
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
- Existing tests pass (all 27 UseTextBlocksTest cases)
- New tests:
noLineContinuationWhenContentHasNewlines— trailing non-newline strings - New tests:
noLineContinuationInMixedConcatenation— end-of-query non-newline strings - New tests:
noLineContinuationInMiddleOfMixedConcatenation— mid-query non-newline join - Fixes moderneinc/customer-requests#1763