Skip to content

fix: escape trailing whitespace before newlines in template literals … - #2506

Open
Monu01123 wants to merge 3 commits into
beautifier:mainfrom
Monu01123:fix/template-literal-newline-preservation
Open

fix: escape trailing whitespace before newlines in template literals …#2506
Monu01123 wants to merge 3 commits into
beautifier:mainfrom
Monu01123:fix/template-literal-newline-preservation

Conversation

@Monu01123

Copy link
Copy Markdown
Contributor

Summary

Fixes Issue #2390 where template literals containing real newlines and trailing horizontal whitespace get silently corrupted by editors that strip trailing whitespace on save (such as VS Code).


Problem & Root Cause

When packers or code generators replace \n escape sequences with actual real newline characters inside template literals (backtick strings), js-beautify correctly preserves the real newlines.

However, if the line ends with horizontal whitespace (spaces or tabs) immediately before that newline, js-beautify previously outputted that trailing whitespace as-is. When saved in an editor with "Strip Trailing Whitespace" enabled, those trailing characters are stripped away, silently altering the runtime string value of the template literal.

Example Input (packed code with actual newline instead of \n):

let pattern=`[ \t
\f\r]`;

@bitwiseman

Copy link
Copy Markdown
Member

Waiting on fix build failures.

Comment thread test-fix-2390.js Outdated
var pass = result === expected;
console.log((pass ? '✓' : '✗') + ' ' + description);
if (!pass) {
console.log(' INPUT : ' + JSON.stringify(input));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the built in test/data/html/tests.js file to test.

@bitwiseman bitwiseman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Monu01123
Waiting for fixes.

…eautifier#2390)

When a packer encodes \n as a real newline character inside a
template literal, js-beautify would output the template with the
real newline intact. This is semantically correct, but any trailing
whitespace characters (spaces, tabs) before that newline would be
silently stripped by editors like VS Code that strip trailing
whitespace on save -- corrupting the template string's semantic value.

Fix: after normalising line breaks in template literal tokens, escape
any trailing horizontal whitespace (space, tab, form-feed, vertical-
tab, NBSP) that appears immediately before a newline, replacing each
character with its JS escape sequence (\x20, \t, \f, \v, \u00a0).
The resulting output is semantically identical to the original, but
is safe to save with trailing-whitespace-stripping enabled.

Only template literals (backtick strings) are affected; regular
single-quote and double-quote strings cannot contain unescaped newlines
so no change is needed for them.

Reproducer (issue beautifier#2390):
  Input (packed -- real newline used instead of \n escape):
    let pattern=[ <TAB><NEWLINE>\f\r]
  Before fix:
    let pattern = [ <TAB>     <- editor strips trailing ws
    \f\r]
  After fix:
    let pattern = [\x20\t
    \f\r]  <- no trailing whitespace, safe to save
@Monu01123
Monu01123 force-pushed the fix/template-literal-newline-preservation branch from 7371950 to 840f36e Compare July 16, 2026 11:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants