Rules: Add the 'braces' rule

This commit is contained in:
Adrien Vergé
2016-01-13 19:31:03 +01:00
parent d08eb22081
commit 5c4c208b98
5 changed files with 163 additions and 6 deletions

View File

@@ -3,12 +3,12 @@
rules:
#block-sequence-indentation:
# present: yes
braces:
min-spaces-inside: 0
max-spaces-inside: 0
brackets:
min-spaces-inside: 0
max-spaces-inside: 0
#braces:
# min-spaces-inside: 0
# max-spaces-inside: 0
colons:
max-spaces-before: 0
max-spaces-after: 1

View File

@@ -15,6 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from yamllint.rules import (
braces,
brackets,
colons,
commas,
@@ -30,6 +31,7 @@ from yamllint.rules import (
)
_RULES = {
braces.ID: braces,
brackets.ID: brackets,
colons.ID: colons,
commas.ID: commas,

47
yamllint/rules/braces.py Normal file
View 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 = 'braces'
TYPE = 'token'
CONF = {'min-spaces-inside': int,
'max-spaces-inside': int}
def check(conf, token, prev, next):
if isinstance(token, yaml.FlowMappingStartToken):
problem = spaces_after(token, prev, next,
min=conf['min-spaces-inside'],
max=conf['max-spaces-inside'],
min_desc='too few spaces inside braces',
max_desc='too many spaces inside braces')
if problem is not None:
yield problem
elif (isinstance(token, yaml.FlowMappingEndToken) and
(prev is None or
not isinstance(prev, yaml.FlowMappingStartToken))):
problem = spaces_before(token, prev, next,
min=conf['min-spaces-inside'],
max=conf['max-spaces-inside'],
min_desc='too few spaces inside braces',
max_desc='too many spaces inside braces')
if problem is not None:
yield problem