Rules: Add the 'brackets' rule
This commit is contained in:
107
tests/rules/test_brackets.py
Normal file
107
tests/rules/test_brackets.py
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
# -*- 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 tests.rules.common import RuleTestCase
|
||||||
|
|
||||||
|
|
||||||
|
class ColonTestCase(RuleTestCase):
|
||||||
|
rule_id = 'brackets'
|
||||||
|
|
||||||
|
def test_disabled(self):
|
||||||
|
conf = 'brackets: disable'
|
||||||
|
self.check('---\n'
|
||||||
|
'array1: []\n'
|
||||||
|
'array2: [ ]\n'
|
||||||
|
'array3: [ a, b]\n'
|
||||||
|
'array4: [a, b, c ]\n'
|
||||||
|
'array5: [a, b, c ]\n'
|
||||||
|
'array6: [ a, b, c ]\n'
|
||||||
|
'array7: [ a, b, c ]\n', conf)
|
||||||
|
|
||||||
|
def test_min_spaces(self):
|
||||||
|
conf = 'brackets: {max-spaces-inside: -1, min-spaces-inside: 0}'
|
||||||
|
self.check('---\n'
|
||||||
|
'array: []\n', conf)
|
||||||
|
|
||||||
|
conf = 'brackets: {max-spaces-inside: -1, min-spaces-inside: 1}'
|
||||||
|
self.check('---\n'
|
||||||
|
'array: []\n', conf, problem=(2, 9))
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ ]\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [a, b]\n', conf, problem1=(2, 9), problem2=(2, 13))
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ a, b ]\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [\n'
|
||||||
|
' a,\n'
|
||||||
|
' b\n'
|
||||||
|
']\n', conf)
|
||||||
|
|
||||||
|
conf = 'brackets: {max-spaces-inside: -1, min-spaces-inside: 3}'
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ a, b ]\n', conf,
|
||||||
|
problem1=(2, 10), problem2=(2, 15))
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ a, b ]\n', conf)
|
||||||
|
|
||||||
|
def test_max_spaces(self):
|
||||||
|
conf = 'brackets: {max-spaces-inside: 0, min-spaces-inside: -1}'
|
||||||
|
self.check('---\n'
|
||||||
|
'array: []\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ ]\n', conf, problem=(2, 9))
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [a, b]\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ a, b ]\n', conf,
|
||||||
|
problem1=(2, 9), problem2=(2, 14))
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ a, b ]\n', conf,
|
||||||
|
problem1=(2, 11), problem2=(2, 18))
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [\n'
|
||||||
|
' a,\n'
|
||||||
|
' b\n'
|
||||||
|
']\n', conf)
|
||||||
|
|
||||||
|
conf = 'brackets: {max-spaces-inside: 3, min-spaces-inside: -1}'
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ a, b ]\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ a, b ]\n', conf,
|
||||||
|
problem1=(2, 12), problem2=(2, 21))
|
||||||
|
|
||||||
|
def test_min_and_max_spaces(self):
|
||||||
|
conf = 'brackets: {max-spaces-inside: 0, min-spaces-inside: 0}'
|
||||||
|
self.check('---\n'
|
||||||
|
'array: []\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ ]\n', conf, problem=(2, 9))
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ a, b]\n', conf, problem=(2, 11))
|
||||||
|
|
||||||
|
conf = 'brackets: {max-spaces-inside: 1, min-spaces-inside: 1}'
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [a, b, c ]\n', conf, problem=(2, 9))
|
||||||
|
|
||||||
|
conf = 'brackets: {max-spaces-inside: 2, min-spaces-inside: 0}'
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [a, b, c ]\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ a, b, c ]\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ a, b, c ]\n', conf, problem=(2, 11))
|
||||||
@@ -32,7 +32,7 @@ class ColonTestCase(RuleTestCase):
|
|||||||
' val\n'
|
' val\n'
|
||||||
' property : value\n'
|
' property : value\n'
|
||||||
' prop2 : val2\n'
|
' prop2 : val2\n'
|
||||||
' propriété : [ valeur ]\n'
|
' propriété : [valeur]\n'
|
||||||
' o:\n'
|
' o:\n'
|
||||||
' k1: [v1, v2]\n'
|
' k1: [v1, v2]\n'
|
||||||
' p:\n'
|
' p:\n'
|
||||||
@@ -54,7 +54,7 @@ class ColonTestCase(RuleTestCase):
|
|||||||
' val\n'
|
' val\n'
|
||||||
' property: value\n'
|
' property: value\n'
|
||||||
' prop2: val2\n'
|
' prop2: val2\n'
|
||||||
' propriété: [ valeur ]\n'
|
' propriété: [valeur]\n'
|
||||||
' o:\n'
|
' o:\n'
|
||||||
' k1: [v1, v2]\n', conf)
|
' k1: [v1, v2]\n', conf)
|
||||||
self.check('---\n'
|
self.check('---\n'
|
||||||
|
|||||||
@@ -3,6 +3,12 @@
|
|||||||
rules:
|
rules:
|
||||||
#block-sequence-indentation:
|
#block-sequence-indentation:
|
||||||
# present: yes
|
# present: yes
|
||||||
|
brackets:
|
||||||
|
min-spaces-inside: 0
|
||||||
|
max-spaces-inside: 0
|
||||||
|
#braces:
|
||||||
|
# min-spaces-inside: 0
|
||||||
|
# max-spaces-inside: 0
|
||||||
colons:
|
colons:
|
||||||
max-spaces-before: 0
|
max-spaces-before: 0
|
||||||
max-spaces-after: 1
|
max-spaces-after: 1
|
||||||
@@ -30,10 +36,4 @@ rules:
|
|||||||
new-line-at-end-of-file: {level: error}
|
new-line-at-end-of-file: {level: error}
|
||||||
new-lines:
|
new-lines:
|
||||||
type: unix
|
type: unix
|
||||||
#spaces-in-brackets: [ 1, 2 ]
|
|
||||||
# min: 1
|
|
||||||
# max: 1
|
|
||||||
#spaces-in-braces: { df: d }
|
|
||||||
# min: 1
|
|
||||||
# max: 1
|
|
||||||
trailing-spaces: {}
|
trailing-spaces: {}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
from yamllint.rules import (
|
from yamllint.rules import (
|
||||||
|
brackets,
|
||||||
colons,
|
colons,
|
||||||
commas,
|
commas,
|
||||||
document_end,
|
document_end,
|
||||||
@@ -29,6 +30,7 @@ from yamllint.rules import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
_RULES = {
|
_RULES = {
|
||||||
|
brackets.ID: brackets,
|
||||||
colons.ID: colons,
|
colons.ID: colons,
|
||||||
commas.ID: commas,
|
commas.ID: commas,
|
||||||
document_end.ID: document_end,
|
document_end.ID: document_end,
|
||||||
|
|||||||
47
yamllint/rules/brackets.py
Normal file
47
yamllint/rules/brackets.py
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
# -*- 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/>.
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
from yamllint.rules.common import spaces_after, spaces_before
|
||||||
|
|
||||||
|
|
||||||
|
ID = 'brackets'
|
||||||
|
TYPE = 'token'
|
||||||
|
CONF = {'min-spaces-inside': int,
|
||||||
|
'max-spaces-inside': int}
|
||||||
|
|
||||||
|
|
||||||
|
def check(conf, token, prev, next):
|
||||||
|
if isinstance(token, yaml.FlowSequenceStartToken):
|
||||||
|
problem = spaces_after(token, prev, next,
|
||||||
|
min=conf['min-spaces-inside'],
|
||||||
|
max=conf['max-spaces-inside'],
|
||||||
|
min_desc='too few spaces inside brackets',
|
||||||
|
max_desc='too many spaces inside brackets')
|
||||||
|
if problem is not None:
|
||||||
|
yield problem
|
||||||
|
|
||||||
|
elif (isinstance(token, yaml.FlowSequenceEndToken) and
|
||||||
|
(prev is None or
|
||||||
|
not isinstance(prev, yaml.FlowSequenceStartToken))):
|
||||||
|
problem = spaces_before(token, prev, next,
|
||||||
|
min=conf['min-spaces-inside'],
|
||||||
|
max=conf['max-spaces-inside'],
|
||||||
|
min_desc='too few spaces inside brackets',
|
||||||
|
max_desc='too many spaces inside brackets')
|
||||||
|
if problem is not None:
|
||||||
|
yield problem
|
||||||
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from yamllint.rules.common import max_spaces_after, max_spaces_before
|
from yamllint.rules.common import spaces_after, spaces_before
|
||||||
|
|
||||||
|
|
||||||
ID = 'colons'
|
ID = 'colons'
|
||||||
@@ -27,12 +27,14 @@ CONF = {'max-spaces-before': int,
|
|||||||
|
|
||||||
def check(conf, token, prev, next):
|
def check(conf, token, prev, next):
|
||||||
if isinstance(token, yaml.ValueToken):
|
if isinstance(token, yaml.ValueToken):
|
||||||
problem = max_spaces_before(conf['max-spaces-before'], token, prev,
|
problem = spaces_before(token, prev, next,
|
||||||
next, 'too many spaces before colon')
|
max=conf['max-spaces-before'],
|
||||||
|
max_desc='too many spaces before colon')
|
||||||
if problem is not None:
|
if problem is not None:
|
||||||
yield problem
|
yield problem
|
||||||
|
|
||||||
problem = max_spaces_after(conf['max-spaces-after'], token, prev, next,
|
problem = spaces_after(token, prev, next,
|
||||||
'too many spaces after colon')
|
max=conf['max-spaces-after'],
|
||||||
|
max_desc='too many spaces after colon')
|
||||||
if problem is not None:
|
if problem is not None:
|
||||||
yield problem
|
yield problem
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from yamllint.rules.common import max_spaces_after, max_spaces_before
|
from yamllint.rules.common import spaces_after, spaces_before
|
||||||
|
|
||||||
|
|
||||||
ID = 'commas'
|
ID = 'commas'
|
||||||
@@ -27,12 +27,14 @@ CONF = {'max-spaces-before': int,
|
|||||||
|
|
||||||
def check(conf, token, prev, next):
|
def check(conf, token, prev, next):
|
||||||
if isinstance(token, yaml.FlowEntryToken):
|
if isinstance(token, yaml.FlowEntryToken):
|
||||||
problem = max_spaces_before(conf['max-spaces-before'], token, prev,
|
problem = spaces_before(token, prev, next,
|
||||||
next, 'too many spaces before comma')
|
max=conf['max-spaces-before'],
|
||||||
|
max_desc='too many spaces before comma')
|
||||||
if problem is not None:
|
if problem is not None:
|
||||||
yield problem
|
yield problem
|
||||||
|
|
||||||
problem = max_spaces_after(conf['max-spaces-after'], token, prev, next,
|
problem = spaces_after(token, prev, next,
|
||||||
'too many spaces after comma')
|
max=conf['max-spaces-after'],
|
||||||
|
max_desc='too many spaces after comma')
|
||||||
if problem is not None:
|
if problem is not None:
|
||||||
yield problem
|
yield problem
|
||||||
|
|||||||
@@ -17,18 +17,25 @@
|
|||||||
from yamllint.errors import LintProblem
|
from yamllint.errors import LintProblem
|
||||||
|
|
||||||
|
|
||||||
def max_spaces_after(nb, token, prev, next, description):
|
def spaces_after(token, prev, next, min=-1, max=-1,
|
||||||
if (next is not None and token.end_mark.line == next.start_mark.line and
|
min_desc=None, max_desc=None):
|
||||||
nb != - 1 and
|
if next is not None and token.end_mark.line == next.start_mark.line:
|
||||||
next.start_mark.pointer - token.end_mark.pointer > nb):
|
spaces = next.start_mark.pointer - token.end_mark.pointer
|
||||||
return LintProblem(token.start_mark.line + 1, next.start_mark.column,
|
if max != - 1 and spaces > max:
|
||||||
description)
|
return LintProblem(token.start_mark.line + 1,
|
||||||
|
next.start_mark.column, max_desc)
|
||||||
|
elif min != - 1 and spaces < min:
|
||||||
|
return LintProblem(token.start_mark.line + 1,
|
||||||
|
next.start_mark.column + 1, min_desc)
|
||||||
|
|
||||||
|
|
||||||
def max_spaces_before(nb, token, prev, next, description):
|
def spaces_before(token, prev, next, min=-1, max=-1,
|
||||||
if (prev is not None and
|
min_desc=None, max_desc=None):
|
||||||
prev.end_mark.line == token.start_mark.line and
|
if prev is not None and prev.end_mark.line == token.start_mark.line:
|
||||||
nb != - 1 and
|
spaces = token.start_mark.pointer - prev.end_mark.pointer
|
||||||
prev.end_mark.pointer + nb < token.start_mark.pointer):
|
if max != - 1 and spaces > max:
|
||||||
return LintProblem(token.start_mark.line + 1, token.start_mark.column,
|
return LintProblem(token.start_mark.line + 1,
|
||||||
description)
|
token.start_mark.column, max_desc)
|
||||||
|
elif min != - 1 and spaces < min:
|
||||||
|
return LintProblem(token.start_mark.line + 1,
|
||||||
|
token.start_mark.column + 1, min_desc)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from yamllint.rules.common import max_spaces_after
|
from yamllint.rules.common import spaces_after
|
||||||
|
|
||||||
|
|
||||||
ID = 'hyphens'
|
ID = 'hyphens'
|
||||||
@@ -26,7 +26,8 @@ CONF = {'max-spaces-after': int}
|
|||||||
|
|
||||||
def check(conf, token, prev, next):
|
def check(conf, token, prev, next):
|
||||||
if isinstance(token, yaml.BlockEntryToken):
|
if isinstance(token, yaml.BlockEntryToken):
|
||||||
problem = max_spaces_after(conf['max-spaces-after'], token, prev, next,
|
problem = spaces_after(token, prev, next,
|
||||||
'too many spaces after hyphen')
|
max=conf['max-spaces-after'],
|
||||||
|
max_desc='too many spaces after hyphen')
|
||||||
if problem is not None:
|
if problem is not None:
|
||||||
yield problem
|
yield problem
|
||||||
|
|||||||
Reference in New Issue
Block a user