diff --git a/yamllint/rules/colons.py b/yamllint/rules/colons.py index 4d644aa..3e23ea0 100644 --- a/yamllint/rules/colons.py +++ b/yamllint/rules/colons.py @@ -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, - 'too many spaces after colon') + problem = max_spaces_after(conf['max-spaces-after'], token, prev, next, + 'too many spaces after colon') + if problem is not None: + yield problem diff --git a/yamllint/rules/common.py b/yamllint/rules/common.py new file mode 100644 index 0000000..77ea7d6 --- /dev/null +++ b/yamllint/rules/common.py @@ -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 . + +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) diff --git a/yamllint/rules/hyphens.py b/yamllint/rules/hyphens.py index 1231762..335f92c 100644 --- a/yamllint/rules/hyphens.py +++ b/yamllint/rules/hyphens.py @@ -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, - 'too many spaces after hyphen') + problem = max_spaces_after(conf['max-spaces-after'], token, prev, next, + 'too many spaces after hyphen') + if problem is not None: + yield problem