fix: avoid gluing a slash onto a comment in dialects where // starts a line comment - #977
Open
Jeremy-xuan wants to merge 1 commit into
Open
Jeremy-xuan wants to merge 1 commit into
Jeremy-xuan wants to merge 1 commit into
Conversation
…a line comment
Layout.add() had a guard that kept a layout item starting with "-" from being
glued onto one ending with "-", because that forms "--", which re-parses as a
line comment and swallows the rest of the line. The same hazard exists for any
other line comment marker a dialect uses, and Snowflake is the only dialect
where "//" starts a line comment:
SELECT a / /* c */ b FROM t; with denseOperators
came out as
SELECT
a//* c */ b
FROM
t;
so "b" became comment text and the statement quietly changed meaning. It also
swallowed a following /* sql-formatter-disable */ marker.
The guard now works off the dialect's own lineCommentTypes instead of a
hard-coded "--", following the same path already used for identifierDashes.
Behavior for "--" is unchanged, and dialects that do not use "//" are
unaffected.
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.
fix: avoid gluing a slash onto a comment in dialects where
//starts a line commentWhat's wrong
Snowflake is the only dialect where
//starts a line comment. WithdenseOperatorson, a/operator directly in front of a comment gets glued to it,forming
//— so the rest of the line becomes comment text:bis now inside the comment. The statement quietly means something else, and itdoes not look wrong enough to catch by eye. A
/* sql-formatter-disable */markeris swallowed the same way, so the user's "do not touch this" instruction stops
working:
Root cause
Layout.add()already had this exact guard for--:The reasoning is right, but it is hard-coded to
--. Every other line commentmarker a dialect uses has the same hazard, and
//is one of them.The fix
The guard now asks the dialect for its own line comment markers instead of assuming
--. The markers already exist on the tokenizer options, so they are carried throughthe same path as
identifierDashes:ProcessedDialectFormatOptionsgainslineCommentTypesdialect.tsfills it fromtokenizerOptions.lineCommentTypes ?? ['--']Layouttakes it as a constructor argument and checks it inwouldFormLineComment()Behavior for
--is unchanged — the existing testdoes not glue a "-" in front of another "-" in dense modestill passes — and dialects that do not use//are notaffected.
lastItemEndsWith()was left with no callers, so it is gone.Tests
Three tests in
test/snowflake.test.ts: a block comment after/, asql-formatter-disablecomment after/, and a plaina / bthat must keep beingdensed (so the fix does not disable dense operators wholesale). The first two fail on
master.Regression check
masterversus the fix over 12,605 combinations (6 dialects × 3 option sets × ~700 SQLliterals harvested from the test suite), with word-multiset preservation against the
input as the criterion: 7,688 byte-identical, 0 regressions. Only Snowflake
changes, and only where a
/meets a comment.