Skip to content

fix: keep strings and comments intact when re-indenting expression macro bodies - #1496

Merged
kvlonge merged 1 commit into
mainfrom
fix/dedent-dialect-quoting
Nov 15, 2026
Merged

kvlonge merged 1 commit into
mainfrom
fix/dedent-dialect-quoting

Conversation

@kvlonge

@kvlonge kvlonge commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Why

After sqb format lays out an expression macro or scalar function body, it re-indents the body by tracking quotes itself. That tracker knew only doubled quotes as escapes and -- as a comment, so on Snowflake:

  • a multi-line string after a backslash-escaped quote ('O\'Brien) lost indentation inside the string on every run, changing its value;
  • an apostrophe in a // comment opened a phantom string, and later lines kept the wrong indent;
  • lines inside a block comment lost two spaces per run, on every dialect, so --check never settled;
  • a block comment kept on the SELECT line set the body's indent to zero.

Changes

  • The re-indent reads the formatted SQL with the dialect's own lexical rules (the existing dialect_lexical_syntax scanner). A line that starts inside a string or comment keeps its text; the indent comes from the first line of code.
  • The dedent's own quote tracker and its constants are removed.
  • DuckShop: the scoped edge-case macro now opens with a block comment. Compiled results are unchanged; the core area is untouched.

Verification

  • Unit tests per dialect for escaped quotes, // comments, block comments and a comment on the SELECT line; the escaped-quote cases fail without the fix.
  • Formatting twice gives the expected output for an escaped-quote and a block-comment macro; a CLI test covers the escaped quote through sqb format, --check and compile.
  • A sweep of 19 body shapes on DuckDB and Snowflake: no faults, strings unchanged, --check clean.
  • cargo test -p sqlbuild-rules, the format tests, clippy and fensu check pass.

…cro bodies

The dedent that removes the formatting SELECT from an expression macro or
scalar function body tracked quotes on its own: it knew only doubled
quotes as escapes and `--` as a comment. On Snowflake, a multi-line
string after a backslash-escaped quote lost indentation inside the string
on every run, changing its value. An apostrophe in a `//` comment opened
a phantom string, the lines inside a block comment lost two spaces per
run, and a block comment kept on the SELECT line set the body's indent.

The dedent now reads the formatted SQL with the dialect's own lexical
rules: a line that starts inside a string or comment keeps its text, and
the body's indent comes from its first line of code.

The DuckShop edge-case macro now opens with a block comment.
@kvlonge
kvlonge enabled auto-merge November 15, 2026 10:36
@kvlonge
kvlonge added this pull request to the merge queue Nov 15, 2026
Merged via the queue into main with commit 755b468 Nov 15, 2026
58 of 60 checks passed
@kvlonge
kvlonge deleted the fix/dedent-dialect-quoting branch November 15, 2026 11:32
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.

1 participant