Skip to content

Preserve significant trailing space in UseTextBlocks (#1132)#1133

Merged
timtebeek merged 1 commit into
mainfrom
tim/fix-trailing-space-1132
Jun 15, 2026
Merged

Preserve significant trailing space in UseTextBlocks (#1132)#1133
timtebeek merged 1 commit into
mainfrom
tim/fix-trailing-space-1132

Conversation

@timtebeek

@timtebeek timtebeek commented Jun 15, 2026

Copy link
Copy Markdown
Member

Problem

When the last (or only) piece of content in a concatenation ends with a significant trailing space and no newline, UseTextBlocks placed the closing """ on the same line as that content:

String message = """
    Connection failed:
     connect timed out
    Transfer id: """;

A text block strips trailing whitespace from every line before escape processing, so the space before the closing delimiter is removed. The runtime value silently changed from ...Transfer id: to ...Transfer id: — no exception, compiles fine, easy to miss.

The recipe already handled trailing spaces before a newline via the \n\s\n replacement, but the trailing space on the final line (no following newline) fell through.

Fix

When the joined content ends with a space, escape that trailing space as \s (which is interpreted after whitespace stripping) and add a \ line continuation so the closing delimiter sits on its own line without introducing a spurious trailing newline:

String message = """
    Connection failed:
     connect timed out
    Transfer id:\s\
    """;

This produces a value byte-for-byte identical to the original.

Tests

  • Added preserveTrailingSpaceOnLastLine reproducing the issue.
  • Updated the existing preserveTrailingWhiteSpaces test, which previously asserted the buggy WHERE something = 1; """ output that dropped the trailing space.

@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Jun 15, 2026
@timtebeek timtebeek merged commit 741bc50 into main Jun 15, 2026
1 check passed
@timtebeek timtebeek deleted the tim/fix-trailing-space-1132 branch June 15, 2026 08:29
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Jun 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

UseTextBlocks drops a significant trailing space, silently changing the string's value

1 participant