Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions tests/rules/test_indentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,25 @@ def test_broken_flows(self):
' y, z\n'
']\n', conf, problem=(3, 4))

def test_unmatched_flow_end(self):
# On invalid YAML, a closing flow token ('}' or ']') can appear with no
# matching opening one; PyYAML still emits the corresponding
# FlowMapping/FlowSequenceEndToken. yamllint must report the syntax
# error instead of crashing in the indentation rule.
# https://github.com/adrienverge/yamllint/issues/771
conf = ('indentation: {spaces: consistent}\n'
'document-start: disable\n')
self.check('data:\n'
' config.alloy: |\n'
'\n'
' }\n', conf, problem=(4, 3, 'syntax'))
self.check('data:\n'
' config.alloy: |\n'
'\n'
' ]\n', conf, problem=(4, 3, 'syntax'))
self.check('}\n', conf, problem=(1, 1, 'syntax'))
self.check(']\n', conf, problem=(1, 1, 'syntax'))

def test_cleared_flows(self):
# flow:
# [
Expand Down
8 changes: 7 additions & 1 deletion yamllint/rules/indentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,13 @@ def detect_indent(base_indent, next):
not isinstance(token, yaml.ValueToken)):
expected = detect_indent(expected, token)

if found_indentation != expected:
# A closing flow token (']' or '}') is expected to align with the line
# that opened the collection, so `expected` is read from that opening
# token. On invalid YAML, PyYAML can emit a closing flow token with no
# matching opening token on the stack, leaving `expected` unset; skip
# the indentation check in that case and let the syntax error be
# reported instead of crashing.
if expected is not None and found_indentation != expected:
if expected < 0:
message = (f'wrong indentation: expected at least '
f'{found_indentation + 1}')
Expand Down
Loading