Skip to content

Commit b68e4b7

Browse files
committed
Fix special case for andialbrecht#284 when statement starts with orphaned AS.
1 parent ce71250 commit b68e4b7

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

sqlparse/engine/grouping.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ def _group(tlist, cls, match,
380380

381381
if match(token):
382382
nidx, next_ = tlist.token_next(tidx)
383-
if valid_prev(prev_) and valid_next(next_):
383+
if prev_ and valid_prev(prev_) and valid_next(next_):
384384
from_idx, to_idx = post(tlist, pidx, tidx, nidx)
385385
grp = tlist.group_tokens(cls, from_idx, to_idx, extend=extend)
386386

tests/test_regressions.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,10 @@ def test_token_next_doesnt_ignore_skip_cm():
316316
assert tok.value == 'select'
317317

318318

319-
def test_issue284_as_grouping():
320-
sql = 'SELECT x AS'
321-
p = sqlparse.parse(sql)[0]
322-
assert sql == str(p)
319+
@pytest.mark.parametrize('s', [
320+
'SELECT x AS',
321+
'AS'
322+
])
323+
def test_issue284_as_grouping(s):
324+
p = sqlparse.parse(s)[0]
325+
assert s == str(p)

0 commit comments

Comments
 (0)