diff --git a/tests/rules/test_colons.py b/tests/rules/test_colons.py index a1e63d5..c9aaaf2 100644 --- a/tests/rules/test_colons.py +++ b/tests/rules/test_colons.py @@ -152,6 +152,21 @@ class ColonTestCase(RuleTestCase): 'a: {b: {c: d, e : f}}\n', conf, problem1=(2, 12), problem2=(2, 20)) + def test_after_enabled_question_mark(self): + conf = 'colons: {max-spaces-before: -1, max-spaces-after: 1}' + self.check('---\n' + '? key\n' + ': value\n', conf) + self.check('---\n' + '? key\n' + ': value\n', conf, problem=(2, 3)) + self.check('---\n' + '? key\n' + ': value\n', conf, problem1=(2, 3), problem2=(3, 3)) + self.check('---\n' + '- ? key\n' + ' : value\n', conf, problem1=(2, 5), problem2=(3, 5)) + def test_after_max(self): conf = 'colons: {max-spaces-before: -1, max-spaces-after: 3}' self.check('---\n' diff --git a/yamllint/rules/colons.py b/yamllint/rules/colons.py index d20f453..6868147 100644 --- a/yamllint/rules/colons.py +++ b/yamllint/rules/colons.py @@ -16,7 +16,7 @@ import yaml -from yamllint.rules.common import spaces_after, spaces_before +from yamllint.rules.common import spaces_after, spaces_before, is_explicit_key ID = 'colons' @@ -38,3 +38,10 @@ def check(conf, token, prev, next, context): max_desc='too many spaces after colon') if problem is not None: yield problem + + if isinstance(token, yaml.KeyToken) and is_explicit_key(token): + problem = spaces_after(token, prev, next, + max=conf['max-spaces-after'], + max_desc='too many spaces after question mark') + if problem is not None: + yield problem diff --git a/yamllint/rules/common.py b/yamllint/rules/common.py index d38fd92..400479c 100644 --- a/yamllint/rules/common.py +++ b/yamllint/rules/common.py @@ -101,3 +101,15 @@ def get_comments_between_tokens(token1, token2, skip_first_line=False): pointer += len(line) + 1 line_no += 1 column_no = 1 + + +def is_explicit_key(token): + # explicit key: + # ? key + # : v + # or + # ? + # key + # : v + return (token.start_mark.pointer < token.end_mark.pointer and + token.start_mark.buffer[token.start_mark.pointer] == '?') diff --git a/yamllint/rules/indentation.py b/yamllint/rules/indentation.py index eab3a40..67bfccc 100644 --- a/yamllint/rules/indentation.py +++ b/yamllint/rules/indentation.py @@ -17,6 +17,7 @@ import yaml from yamllint.errors import LintProblem +from yamllint.rules.common import is_explicit_key ID = 'indentation' @@ -128,16 +129,7 @@ def check(conf, token, prev, next, context): context['stack'].append(Parent(KEY, indent)) - # explicit key: - # ? key - # : v - # or - # ? - # key - # : v - context['stack'][-1].explicit_key = ( - token.start_mark.pointer < token.end_mark.pointer and - token.start_mark.buffer[token.start_mark.pointer] == '?') + context['stack'][-1].explicit_key = is_explicit_key(token) if context['stack'][-1].type == VAL: context['stack'].pop()