fix: escape trailing whitespace before newlines in template literals … - #2506
Open
Monu01123 wants to merge 3 commits into
Open
fix: escape trailing whitespace before newlines in template literals …#2506Monu01123 wants to merge 3 commits into
Monu01123 wants to merge 3 commits into
Conversation
Member
|
Waiting on fix build failures. |
bitwiseman
reviewed
Jul 13, 2026
| var pass = result === expected; | ||
| console.log((pass ? '✓' : '✗') + ' ' + description); | ||
| if (!pass) { | ||
| console.log(' INPUT : ' + JSON.stringify(input)); |
Member
There was a problem hiding this comment.
Use the built in test/data/html/tests.js file to test.
bitwiseman
requested changes
Jul 13, 2026
bitwiseman
left a comment
Member
There was a problem hiding this comment.
@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
…& integrate tests into standard suite
Monu01123
force-pushed
the
fix/template-literal-newline-preservation
branch
from
July 16, 2026 11:15
7371950 to
840f36e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
\nescape sequences with actual real newline characters inside template literals (backtick strings),js-beautifycorrectly preserves the real newlines.However, if the line ends with horizontal whitespace (spaces or tabs) immediately before that newline,
js-beautifypreviously 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):