Rules: Make max-spaces-* generic

The goal being to use them in the 'colons', 'hyphens', 'commas', etc.
rules.
pull/4/head
Adrien Vergé 9 years ago
parent 07c5b4177c
commit cfea0661ed

@ -16,7 +16,7 @@
import yaml
from yamllint.errors import LintProblem
from yamllint.rules.common import max_spaces_after, max_spaces_before
ID = 'colons'
@ -27,20 +27,12 @@ CONF = {'max-spaces-before': int,
def check(conf, token, prev, next):
if isinstance(token, yaml.ValueToken):
if (prev is not None and
prev.end_mark.line == token.start_mark.line and
conf['max-spaces-before'] != - 1 and
(prev.end_mark.pointer + conf['max-spaces-before'] <
token.start_mark.pointer)):
yield LintProblem(token.start_mark.line + 1,
token.start_mark.column,
'too many spaces before colon')
problem = max_spaces_before(conf['max-spaces-before'], token, prev,
next, 'too many spaces before colon')
if problem is not None:
yield problem
if (next is not None and
token.end_mark.line == next.start_mark.line and
conf['max-spaces-after'] != - 1 and
(next.start_mark.pointer - token.end_mark.pointer >
conf['max-spaces-after'])):
yield LintProblem(token.start_mark.line + 1,
next.start_mark.column,
problem = max_spaces_after(conf['max-spaces-after'], token, prev, next,
'too many spaces after colon')
if problem is not None:
yield problem

@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2016 Adrien Vergé
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from yamllint.errors import LintProblem
def max_spaces_after(nb, token, prev, next, description):
if (next is not None and token.end_mark.line == next.start_mark.line and
nb != - 1 and
next.start_mark.pointer - token.end_mark.pointer > nb):
return LintProblem(token.start_mark.line + 1, next.start_mark.column,
description)
def max_spaces_before(nb, token, prev, next, description):
if (prev is not None and
prev.end_mark.line == token.start_mark.line and
nb != - 1 and
prev.end_mark.pointer + nb < token.start_mark.pointer):
return LintProblem(token.start_mark.line + 1, token.start_mark.column,
description)

@ -16,7 +16,7 @@
import yaml
from yamllint.errors import LintProblem
from yamllint.rules.common import max_spaces_after
ID = 'hyphens'
@ -26,9 +26,7 @@ CONF = {'max-spaces-after': int}
def check(conf, token, prev, next):
if isinstance(token, yaml.BlockEntryToken):
if token.end_mark.line == next.start_mark.line:
if (next.start_mark.pointer - token.end_mark.pointer >
conf['max-spaces-after']):
yield LintProblem(token.start_mark.line + 1,
next.start_mark.column,
problem = max_spaces_after(conf['max-spaces-after'], token, prev, next,
'too many spaces after hyphen')
if problem is not None:
yield problem

Loading…
Cancel
Save