Skip to content

fix(cli): insert text literally when moving comments onto their own line - #2490

Open
dmargol1 wants to merge 1 commit into
dataform-co:mainfrom
dmargol1:fix/format-dollar-replacement
Open

dmargol1 wants to merge 1 commit into
dataform-co:mainfrom
dmargol1:fix/format-dollar-replacement

Conversation

@dmargol1

Copy link
Copy Markdown
Contributor

Fixes #2489.

When the SQLX formatter moves a comment, or a multi-line ${...}, onto its own line, it rebuilds the line with String.prototype.replace and 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.ts check the exact output, and that a second format pass changes nothing, for:

  • each of $', $`, $& and $$ in a comment
  • $$ in a string literal and in a ${...} on the same line as a comment
  • $' in a multi-line ${...}
  • a comment with $& and 17 $$, through formatFile, checking the file that's written: on main this passes the formatter's checks and writes corrupted text
  • $5 as a control

All fail on main except the control. //sqlx/... passes.

I also ran dataform format, with CLI bundles built from main and from this branch, on all 255 .sqlx files in GoogleCloudPlatform/bigquery-utils at d5bde4f:

  • The only changes are three cw_nvp2json files that fail on main and now format. Their content is unchanged apart from whitespace and keyword case.
  • The other 247 files that formatted on main give byte-identical output.
  • The remaining five fail with the same error as on main.

The reproductions in #1955 and in the later comments on #1740 still fail, with identical first-pass output.

🤖 Generated with Claude Code

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
@dmargol1
dmargol1 requested a review from a team as a code owner November 24, 2026 21:39
@dmargol1
dmargol1 requested review from apilaskowski and removed request for a team November 24, 2026 21:39
Comment thread sqlx/format.ts
newLines.push(`${indent}${textAfterPlaceholder.trim()}`);
}
return sqlx.replace(wholeLine, newLines.join("\n"));
return sqlx.replace(wholeLine, () => newLines.join("\n"));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

No deployments
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.

dataform format expands $', $`, $& and $$ when moving comments onto their own line

2 participants