Repository navigation
Conversation
When the SQLX formatter moved a comment or a multi-line `${...}` onto its
own line, it rebuilt the whole line with String.prototype.replace and a
replacement string, so `$'`, `` $` ``, `$&` and `$$` anywhere on that line
were expanded. `dataform format` then failed with "Formatter unable to
determine final formatted form" or a non-whitespace character count error.
When the expansions balanced out, the corrupted text passed those checks
and was written to the file.
Pass a function instead, as dataform-co#993 did for the single-line branch.
Fixes dataform-co#2489
apilaskowski
approved these changes
Nov 24, 2026
| newLines.push(`${indent}${textAfterPlaceholder.trim()}`); | ||
| } | ||
| return sqlx.replace(wholeLine, newLines.join("\n")); | ||
| return sqlx.replace(wholeLine, () => newLines.join("\n")); |
Collaborator
There was a problem hiding this comment.
LGTM, thanks for the thorough testing. One question: replacePlaceholders (L198) also calls .replace(placeholderId, placeholderValue) with a plain string as the replacement. Could user $-sequences reach it? If so, using () => placeholderValue there too would make this complete.
This branch has not been deployed
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.
Fixes #2489.
When the SQLX formatter moves a comment, or a multi-line
${...}, onto its own line, it rebuilds the line withString.prototype.replaceand a replacement string, so$',$`,$&and$$anywhere on the line are expanded. This PR passes a function instead, so the text is inserted literally, as #993 did for the single-line branch just above.Testing
New tests in
sqlx/format_test.tscheck the exact output, and that a second format pass changes nothing, for:$',$`,$&and$$in a comment$$in a string literal and in a${...}on the same line as a comment$'in a multi-line${...}$&and 17$$, throughformatFile, checking the file that's written: onmainthis passes the formatter's checks and writes corrupted text$5as a controlAll fail on
mainexcept the control.//sqlx/...passes.I also ran
dataform format, with CLI bundles built frommainand from this branch, on all 255.sqlxfiles inGoogleCloudPlatform/bigquery-utilsat d5bde4f:cw_nvp2jsonfiles that fail onmainand now format. Their content is unchanged apart from whitespace and keyword case.maingive byte-identical output.main.The reproductions in #1955 and in the later comments on #1740 still fail, with identical first-pass output.
🤖 Generated with Claude Code