Skip to content

Commit 66ae504

Browse files
committed
token_next shouldn't ignore skip_cm
1 parent b3700f4 commit 66ae504

2 files changed

Lines changed: 13 additions & 19 deletions

File tree

sqlparse/sql.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -257,37 +257,25 @@ def token_prev(self, idx, skip_ws=True, skip_cm=False):
257257
"""Returns the previous token relative to *idx*.
258258
259259
If *skip_ws* is ``True`` (the default) whitespace tokens are ignored.
260+
If *skip_cm* is ``True`` comments are ignored.
260261
``None`` is returned if there's no previous token.
261262
"""
262-
if idx is None:
263-
return None, None
264-
idx += 1 # alot of code usage current pre-compensates for this
265-
funcs = lambda tk: not ((skip_ws and tk.is_whitespace()) or
266-
(skip_cm and imt(tk, t=T.Comment, i=Comment)))
267-
return self._token_matching(funcs, idx, reverse=True)
263+
return self.token_next(idx, skip_ws, skip_cm, _reverse=True)
268264

269-
# TODO: May need to implement skip_cm for upstream changes.
270265
# TODO: May need to re-add default value to idx
271-
def token_next(self, idx, skip_ws=True, skip_cm=False):
266+
def token_next(self, idx, skip_ws=True, skip_cm=False, _reverse=False):
272267
"""Returns the next token relative to *idx*.
273268
274269
If *skip_ws* is ``True`` (the default) whitespace tokens are ignored.
270+
If *skip_cm* is ``True`` comments are ignored.
275271
``None`` is returned if there's no next token.
276272
"""
277273
if idx is None:
278274
return None, None
279275
idx += 1 # alot of code usage current pre-compensates for this
280-
try:
281-
if not skip_ws:
282-
return idx, self.tokens[idx]
283-
else:
284-
while True:
285-
token = self.tokens[idx]
286-
if not token.is_whitespace():
287-
return idx, token
288-
idx += 1
289-
except IndexError:
290-
return None, None
276+
funcs = lambda tk: not ((skip_ws and tk.is_whitespace()) or
277+
(skip_cm and imt(tk, t=T.Comment, i=Comment)))
278+
return self._token_matching(funcs, idx, reverse=_reverse)
291279

292280
def token_index(self, token, start=0):
293281
"""Return list index of token."""

tests/test_regressions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,9 @@ def test_issue207_runaway_format():
312312
" 2 as two,",
313313
" 3",
314314
" from dual) t0"])
315+
316+
317+
def token_next_doesnt_ignore_skip_cm():
318+
sql = '--comment\nselect 1'
319+
tok = sqlparse.parse(sql)[0].token_next(-1, skip_cm=True)[1]
320+
assert tok.value == 'select'

0 commit comments

Comments
 (0)