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:
1 parent
46e6a17daf
commit
f0a09a4a1b
4 files changed
+58
-9
No files matched your search
@@ -241,6 +241,27 @@ def test_grouping_identifier_list_with_inline_comments():
|
||||
assert isinstance(p.tokens[0].tokens[3], sql.Identifier)
|
||||
|
||||
|
||||
def test_grouping_identifier_with_comments():
|
||||
# issue712: comments between the tokens of a (possibly aliased) dotted
|
||||
# identifier must not prevent it from being grouped.
|
||||
s = 'a\n-- c1\n.\n-- c2\nb'
|
||||
p = sqlparse.parse(s)[0]
|
||||
assert str(p) == s
|
||||
assert len([t for t in p.tokens if not t.is_whitespace]) == 1
|
||||
assert isinstance(p.tokens[0], sql.Identifier)
|
||||
assert p.tokens[0].get_parent_name() == 'a'
|
||||
assert p.tokens[0].get_real_name() == 'b'
|
||||
|
||||
s = 'ns -- a\n. -- b\n"tbl" -- c\nas -- d\nal'
|
||||
p = sqlparse.parse(s)[0]
|
||||
assert str(p) == s
|
||||
assert len([t for t in p.tokens if not t.is_whitespace]) == 1
|
||||
assert isinstance(p.tokens[0], sql.Identifier)
|
||||
assert p.tokens[0].get_parent_name() == 'ns'
|
||||
assert p.tokens[0].get_real_name() == 'tbl'
|
||||
assert p.tokens[0].get_alias() == 'al'
|
||||
|
||||
|
||||
def test_grouping_identifiers_with_operators():
|
||||
p = sqlparse.parse('a+b as c from table where (d-e)%2= 1')[0]
|
||||
assert len([x for x in p.flatten() if x.ttype == T.Name]) == 5
|
||||
|
||||
Reference in new issue
Block a user