fix: group dotted identifiers split by interleaved comments (issue712)

Identifiers may carry comments between their tokens (e.g. name -- c\n . -- c\n obj). _group only skipped whitespace when locating the tokens around a joining token, so an interleaved comment defeated the prev/next checks and the identifier was never formed.

Add an opt-in skip_comments flag to _group and enable it for group_period and group_as; the comments stay inside the resulting group. Also skip comments in get_parent_name and avoid a redundant separating space in StripCommentsFilter for the newly reachable interleaved-comment identifiers.
This commit is contained in:
dualfroz committed 2026-09-04 22:24:36 +02:00
1 parent 46e6a17daf
commit f0a09a4a1b
4 files changed
+58 -9

No files matched your search

+14 -2
View File
@@ -63,8 +63,20 @@ class StripCommentsFilter:
):
# Insert a whitespace to ensure the following SQL produces
# a valid SQL (see #425).
if prev_ is not None and not prev_.match(T.Punctuation, '('):
tlist.tokens.insert(tidx, _get_insert_token(token))
insert_tok = _get_insert_token(token)
# A plain space separator is redundant when a neighbour is
# already whitespace, which happens for comments interleaved
# inside a grouped identifier. A line break carried by the
# comment is always kept.
redundant_space = (
insert_tok.is_whitespace and not insert_tok.is_newline
and ((prev_ is not None and prev_.is_whitespace)
or (next_ is not None and next_.is_whitespace))
)
if (prev_ is not None
and not prev_.match(T.Punctuation, '(')
and not redundant_space):
tlist.tokens.insert(tidx, insert_tok)
tlist.tokens.remove(token)
tidx -= 1
else: