Add 'forbid' configurations to the braces and brackets rules

Add 'forbid' configuration parameters to the braces and brackets rules
to allow users to forbid the use of flow style collections, flow
mappings and flow sequences.
pull/321/head
Satoru SATOH 4 years ago committed by Adrien Vergé
parent 0a88c55194
commit 333ae52c78

@ -31,6 +31,36 @@ class ColonTestCase(RuleTestCase):
'dict6: { a: 1, b, c: 3 }\n' 'dict6: { a: 1, b, c: 3 }\n'
'dict7: { a: 1, b, c: 3 }\n', conf) 'dict7: { a: 1, b, c: 3 }\n', conf)
def test_forbid(self):
conf = ('braces:\n'
' forbid: false\n')
self.check('---\n'
'dict: {}\n', conf)
self.check('---\n'
'dict: {a}\n', conf)
self.check('---\n'
'dict: {a: 1}\n', conf)
self.check('---\n'
'dict: {\n'
' a: 1\n'
'}\n', conf)
conf = ('braces:\n'
' forbid: true\n')
self.check('---\n'
'dict:\n'
' a: 1\n', conf)
self.check('---\n'
'dict: {}\n', conf, problem=(2, 8))
self.check('---\n'
'dict: {a}\n', conf, problem=(2, 8))
self.check('---\n'
'dict: {a: 1}\n', conf, problem=(2, 8))
self.check('---\n'
'dict: {\n'
' a: 1\n'
'}\n', conf, problem=(2, 8))
def test_min_spaces(self): def test_min_spaces(self):
conf = ('braces:\n' conf = ('braces:\n'
' max-spaces-inside: -1\n' ' max-spaces-inside: -1\n'

@ -31,6 +31,35 @@ class ColonTestCase(RuleTestCase):
'array6: [ a, b, c ]\n' 'array6: [ a, b, c ]\n'
'array7: [ a, b, c ]\n', conf) 'array7: [ a, b, c ]\n', conf)
def test_forbid(self):
conf = ('brackets:\n'
' forbid: false\n')
self.check('---\n'
'array: []\n', conf)
self.check('---\n'
'array: [a, b]\n', conf)
self.check('---\n'
'array: [\n'
' a,\n'
' b\n'
']\n', conf)
conf = ('brackets:\n'
' forbid: true\n')
self.check('---\n'
'array:\n'
' - a\n'
' - b\n', conf)
self.check('---\n'
'array: []\n', conf, problem=(2, 9))
self.check('---\n'
'array: [a, b]\n', conf, problem=(2, 9))
self.check('---\n'
'array: [\n'
' a,\n'
' b\n'
']\n', conf, problem=(2, 9))
def test_min_spaces(self): def test_min_spaces(self):
conf = ('brackets:\n' conf = ('brackets:\n'
' max-spaces-inside: -1\n' ' max-spaces-inside: -1\n'

@ -15,10 +15,14 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
Use this rule to control the number of spaces inside braces (``{`` and ``}``). Use this rule to control the use of flow mappings or number of spaces inside
braces (``{`` and ``}``).
.. rubric:: Options .. rubric:: Options
* ``forbid`` is used to forbid the use of flow mappings which are denoted by
surrounding braces (``{`` and ``}``). Use ``true`` to forbid the use of flow
mappings completely.
* ``min-spaces-inside`` defines the minimal number of spaces required inside * ``min-spaces-inside`` defines the minimal number of spaces required inside
braces. braces.
* ``max-spaces-inside`` defines the maximal number of spaces allowed inside * ``max-spaces-inside`` defines the maximal number of spaces allowed inside
@ -34,6 +38,7 @@ Use this rule to control the number of spaces inside braces (``{`` and ``}``).
rules: rules:
braces: braces:
forbid: false
min-spaces-inside: 0 min-spaces-inside: 0
max-spaces-inside: 0 max-spaces-inside: 0
min-spaces-inside-empty: -1 min-spaces-inside-empty: -1
@ -41,6 +46,20 @@ Use this rule to control the number of spaces inside braces (``{`` and ``}``).
.. rubric:: Examples .. rubric:: Examples
#. With ``braces: {forbid: true}``
the following code snippet would **PASS**:
::
object:
key1: 4
key2: 8
the following code snippet would **FAIL**:
::
object: { key1: 4, key2: 8 }
#. With ``braces: {min-spaces-inside: 0, max-spaces-inside: 0}`` #. With ``braces: {min-spaces-inside: 0, max-spaces-inside: 0}``
the following code snippet would **PASS**: the following code snippet would **PASS**:
@ -103,23 +122,31 @@ Use this rule to control the number of spaces inside braces (``{`` and ``}``).
import yaml import yaml
from yamllint.linter import LintProblem
from yamllint.rules.common import spaces_after, spaces_before from yamllint.rules.common import spaces_after, spaces_before
ID = 'braces' ID = 'braces'
TYPE = 'token' TYPE = 'token'
CONF = {'min-spaces-inside': int, CONF = {'forbid': bool,
'min-spaces-inside': int,
'max-spaces-inside': int, 'max-spaces-inside': int,
'min-spaces-inside-empty': int, 'min-spaces-inside-empty': int,
'max-spaces-inside-empty': int} 'max-spaces-inside-empty': int}
DEFAULT = {'min-spaces-inside': 0, DEFAULT = {'forbid': False,
'min-spaces-inside': 0,
'max-spaces-inside': 0, 'max-spaces-inside': 0,
'min-spaces-inside-empty': -1, 'min-spaces-inside-empty': -1,
'max-spaces-inside-empty': -1} 'max-spaces-inside-empty': -1}
def check(conf, token, prev, next, nextnext, context): def check(conf, token, prev, next, nextnext, context):
if (isinstance(token, yaml.FlowMappingStartToken) and if conf['forbid'] and isinstance(token, yaml.FlowMappingStartToken):
yield LintProblem(token.start_mark.line + 1,
token.end_mark.column + 1,
'forbidden flow mapping')
elif (isinstance(token, yaml.FlowMappingStartToken) and
isinstance(next, yaml.FlowMappingEndToken)): isinstance(next, yaml.FlowMappingEndToken)):
problem = spaces_after(token, prev, next, problem = spaces_after(token, prev, next,
min=(conf['min-spaces-inside-empty'] min=(conf['min-spaces-inside-empty']

@ -15,11 +15,14 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
Use this rule to control the number of spaces inside brackets (``[`` and Use this rule to control the use of flow sequences or the number of spaces
``]``). inside brackets (``[`` and ``]``).
.. rubric:: Options .. rubric:: Options
* ``forbid`` is used to forbid the use of flow sequences which are denoted by
surrounding brackets (``[`` and ``]``). Use ``true`` to forbid the use of
flow sequences completely.
* ``min-spaces-inside`` defines the minimal number of spaces required inside * ``min-spaces-inside`` defines the minimal number of spaces required inside
brackets. brackets.
* ``max-spaces-inside`` defines the maximal number of spaces allowed inside * ``max-spaces-inside`` defines the maximal number of spaces allowed inside
@ -35,6 +38,7 @@ Use this rule to control the number of spaces inside brackets (``[`` and
rules: rules:
brackets: brackets:
forbid: false
min-spaces-inside: 0 min-spaces-inside: 0
max-spaces-inside: 0 max-spaces-inside: 0
min-spaces-inside-empty: -1 min-spaces-inside-empty: -1
@ -42,6 +46,21 @@ Use this rule to control the number of spaces inside brackets (``[`` and
.. rubric:: Examples .. rubric:: Examples
#. With ``brackets: {forbid: true}``
the following code snippet would **PASS**:
::
object:
- 1
- 2
- abc
the following code snippet would **FAIL**:
::
object: [ 1, 2, abc ]
#. With ``brackets: {min-spaces-inside: 0, max-spaces-inside: 0}`` #. With ``brackets: {min-spaces-inside: 0, max-spaces-inside: 0}``
the following code snippet would **PASS**: the following code snippet would **PASS**:
@ -104,23 +123,31 @@ Use this rule to control the number of spaces inside brackets (``[`` and
import yaml import yaml
from yamllint.linter import LintProblem
from yamllint.rules.common import spaces_after, spaces_before from yamllint.rules.common import spaces_after, spaces_before
ID = 'brackets' ID = 'brackets'
TYPE = 'token' TYPE = 'token'
CONF = {'min-spaces-inside': int, CONF = {'forbid': bool,
'min-spaces-inside': int,
'max-spaces-inside': int, 'max-spaces-inside': int,
'min-spaces-inside-empty': int, 'min-spaces-inside-empty': int,
'max-spaces-inside-empty': int} 'max-spaces-inside-empty': int}
DEFAULT = {'min-spaces-inside': 0, DEFAULT = {'forbid': False,
'min-spaces-inside': 0,
'max-spaces-inside': 0, 'max-spaces-inside': 0,
'min-spaces-inside-empty': -1, 'min-spaces-inside-empty': -1,
'max-spaces-inside-empty': -1} 'max-spaces-inside-empty': -1}
def check(conf, token, prev, next, nextnext, context): def check(conf, token, prev, next, nextnext, context):
if (isinstance(token, yaml.FlowSequenceStartToken) and if conf['forbid'] and isinstance(token, yaml.FlowSequenceStartToken):
yield LintProblem(token.start_mark.line + 1,
token.end_mark.column + 1,
'forbidden flow sequence')
elif (isinstance(token, yaml.FlowSequenceStartToken) and
isinstance(next, yaml.FlowSequenceEndToken)): isinstance(next, yaml.FlowSequenceEndToken)):
problem = spaces_after(token, prev, next, problem = spaces_after(token, prev, next,
min=(conf['min-spaces-inside-empty'] min=(conf['min-spaces-inside-empty']

Loading…
Cancel
Save