Handle SyntaxErrors in a slightly smarter way

SyntaxErrors are strange and mystical beasts. On top of the problems we
encountered previously in GitLab#237, it's now apparent that
SyntaxErrors can also occur across multiple lines (in fact, across the
rest of a file). In the event of a "multi-line" SyntaxError, we need to
determine what row to report and what the column number is.

For now, we're going to use the row number of the first line and limit
the column number to be less than the end of the line. It may not be
perfect, but it is slightly better.

Related-to #237
Closes #259
This commit is contained in:
Ian Cordasco 2016-11-17 17:17:48 -06:00
parent fd1cc38435
commit 07c187b8d3
No known key found for this signature in database
GPG key ID: 656D3395E4A9791A

View file

@ -470,23 +470,22 @@ class FileChecker(object):
# NOTE(sigmavirus24): SyntaxErrors report 1-indexed column # NOTE(sigmavirus24): SyntaxErrors report 1-indexed column
# numbers. We need to decrement the column number by 1 at # numbers. We need to decrement the column number by 1 at
# least. # least.
offset = 1 column_offset = 1
# See also: https://gitlab.com/pycqa/flake8/issues/237
physical_line = token[-1] physical_line = token[-1]
if len(physical_line) == column and physical_line[-1] == '\n':
# NOTE(sigmavirus24): By default, we increment the column # NOTE(sigmavirus24): SyntaxErrors also don't exactly have a
# value so that it's always 1-indexed. The SyntaxError that # "physical" line so much as what was accumulated by the point
# we are trying to handle here will end up being 2 past # tokenizing failed.
# the end of the line. This happens because the # See also: https://gitlab.com/pycqa/flake8/issues/237
# SyntaxError is technically the character after the lines = physical_line.rstrip('\n').split('\n')
# new-line. For example, if the code is ``foo(\n`` then row_offset = len(lines) - 1
# ``\n`` will be 4, the empty string will be 5 but most logical_line = lines[0]
# tools want to report the at column 4, i.e., the opening logical_line_length = len(logical_line)
# parenthesis. Semantically, having a column number of 6 is if column > logical_line_length:
# correct but not useful for tooling (e.g., editors that column = logical_line_length
# constantly run Flake8 for users). row -= row_offset
# See also: https://gitlab.com/pycqa/flake8/issues/237 column -= column_offset
offset += 1
column -= offset
return row, column return row, column
def run_ast_checks(self): def run_ast_checks(self):