Formatter: Keep glued ERB output text together to avoid injected whitespace#1863
Open
joaoGabriel55 wants to merge 1 commit into
Conversation
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.
Overview
When ERB control-flow bodies (the statements inside
if,each,case,while, etc.) contained rendered output "glued" directly to adjacent text with no whitespace between them, the formatter would break the content onto separate lines. This injected whitespace into the rendered HTML that wasn't present in the source.For example:
The
<%= owner.name %>'s dogfragment would get split, changing the rendered output (Peggy's dog→Peggy 's dog).Related Issue
Closes #1729.
Affected Component(s)
Type of Change
Main Technical Changes
ERB statement bodies are now routed through a new
visitStatementshelper. It inspects the statements for a glued text-flow boundary: two adjacent nodes that both render visible output and touch with no whitespace between them (e.g.<%= user.name %>'s dog,<%= greeting %>,,John<%= suffix %>).<% … %>statement that renders nothing) → keep the existing per-node visiting that breaks cleanly at ERB boundaries.Changes:
format-printer.ts— AddedvisitStatements, which delegates tohasGluedTextFlowBoundaryto decide between text-flow visiting and per-node visiting. Wired every ERB control-flow visitor (if,in,else,when,begin,while,until,for,rescue,ensure,unless) to use it.text-flow-helpers.ts— AddedhasGluedTextFlowBoundaryand arendersVisibleOutputhelper. The latter treats non-empty text, ERB output tags (<%= %>/<%== %>), and inline elements as visible output; non-output<% … %>control statements render nothing and break the glue chain.text-flow-engine.ts— ExposedhasGluedTextFlowBoundaryon the engine.Breaking is only suppressed where it would actually alter rendered output. Whitespace-separated outputs and non-output control statements are unaffected, so existing formatting behavior is preserved everywhere that gluing doesn't matter.
How to Test
A new test suite (
test/erb/glued-text-flow.test.ts, 7 cases) covers:ifblocks (block and inline start)ifandcapture do)<%= … %>outputs still break at ERB boundaries, and a non-output<% i += 1 %>statement glued to an inline element still breaksRun the formatter test suite from
javascript/packages/formatter:Checklist
Component: Descriptionconvention