Rules: colons: Apply to '?' also

pull/4/head
Adrien Vergé 9 years ago
parent 9d8b0d4d2c
commit 8288a6f331

@ -152,6 +152,21 @@ class ColonTestCase(RuleTestCase):
'a: {b: {c: d, e : f}}\n', conf, 'a: {b: {c: d, e : f}}\n', conf,
problem1=(2, 12), problem2=(2, 20)) 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): def test_after_max(self):
conf = 'colons: {max-spaces-before: -1, max-spaces-after: 3}' conf = 'colons: {max-spaces-before: -1, max-spaces-after: 3}'
self.check('---\n' self.check('---\n'

@ -16,7 +16,7 @@
import yaml 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' ID = 'colons'
@ -38,3 +38,10 @@ def check(conf, token, prev, next, context):
max_desc='too many spaces after colon') max_desc='too many spaces after colon')
if problem is not None: if problem is not None:
yield problem 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

@ -101,3 +101,15 @@ def get_comments_between_tokens(token1, token2, skip_first_line=False):
pointer += len(line) + 1 pointer += len(line) + 1
line_no += 1 line_no += 1
column_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] == '?')

@ -17,6 +17,7 @@
import yaml import yaml
from yamllint.errors import LintProblem from yamllint.errors import LintProblem
from yamllint.rules.common import is_explicit_key
ID = 'indentation' ID = 'indentation'
@ -128,16 +129,7 @@ def check(conf, token, prev, next, context):
context['stack'].append(Parent(KEY, indent)) context['stack'].append(Parent(KEY, indent))
# explicit key: context['stack'][-1].explicit_key = is_explicit_key(token)
# ? 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] == '?')
if context['stack'][-1].type == VAL: if context['stack'][-1].type == VAL:
context['stack'].pop() context['stack'].pop()

Loading…
Cancel
Save