mirror of
https://github.com/adrienverge/yamllint.git
synced 2024-11-22 07:36:25 -06:00
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.
This commit is contained in:
parent
0a88c55194
commit
333ae52c78
@ -31,6 +31,36 @@ class ColonTestCase(RuleTestCase):
|
||||
'dict6: { a: 1, b, c: 3 }\n'
|
||||
'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):
|
||||
conf = ('braces:\n'
|
||||
' max-spaces-inside: -1\n'
|
||||
|
@ -31,6 +31,35 @@ class ColonTestCase(RuleTestCase):
|
||||
'array6: [ a, b, c ]\n'
|
||||
'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):
|
||||
conf = ('brackets:\n'
|
||||
' max-spaces-inside: -1\n'
|
||||
|
@ -15,10 +15,14 @@
|
||||
# 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
|
||||
|
||||
* ``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
|
||||
braces.
|
||||
* ``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:
|
||||
braces:
|
||||
forbid: false
|
||||
min-spaces-inside: 0
|
||||
max-spaces-inside: 0
|
||||
min-spaces-inside-empty: -1
|
||||
@ -41,6 +46,20 @@ Use this rule to control the number of spaces inside braces (``{`` and ``}``).
|
||||
|
||||
.. 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}``
|
||||
|
||||
the following code snippet would **PASS**:
|
||||
@ -103,23 +122,31 @@ Use this rule to control the number of spaces inside braces (``{`` and ``}``).
|
||||
|
||||
import yaml
|
||||
|
||||
from yamllint.linter import LintProblem
|
||||
from yamllint.rules.common import spaces_after, spaces_before
|
||||
|
||||
|
||||
ID = 'braces'
|
||||
TYPE = 'token'
|
||||
CONF = {'min-spaces-inside': int,
|
||||
CONF = {'forbid': bool,
|
||||
'min-spaces-inside': int,
|
||||
'max-spaces-inside': int,
|
||||
'min-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,
|
||||
'min-spaces-inside-empty': -1,
|
||||
'max-spaces-inside-empty': -1}
|
||||
|
||||
|
||||
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)):
|
||||
problem = spaces_after(token, prev, next,
|
||||
min=(conf['min-spaces-inside-empty']
|
||||
|
@ -15,11 +15,14 @@
|
||||
# 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
|
||||
|
||||
* ``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
|
||||
brackets.
|
||||
* ``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:
|
||||
brackets:
|
||||
forbid: false
|
||||
min-spaces-inside: 0
|
||||
max-spaces-inside: 0
|
||||
min-spaces-inside-empty: -1
|
||||
@ -42,6 +46,21 @@ Use this rule to control the number of spaces inside brackets (``[`` and
|
||||
|
||||
.. 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}``
|
||||
|
||||
the following code snippet would **PASS**:
|
||||
@ -104,23 +123,31 @@ Use this rule to control the number of spaces inside brackets (``[`` and
|
||||
|
||||
import yaml
|
||||
|
||||
from yamllint.linter import LintProblem
|
||||
from yamllint.rules.common import spaces_after, spaces_before
|
||||
|
||||
|
||||
ID = 'brackets'
|
||||
TYPE = 'token'
|
||||
CONF = {'min-spaces-inside': int,
|
||||
CONF = {'forbid': bool,
|
||||
'min-spaces-inside': int,
|
||||
'max-spaces-inside': int,
|
||||
'min-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,
|
||||
'min-spaces-inside-empty': -1,
|
||||
'max-spaces-inside-empty': -1}
|
||||
|
||||
|
||||
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)):
|
||||
problem = spaces_after(token, prev, next,
|
||||
min=(conf['min-spaces-inside-empty']
|
||||
|
Loading…
Reference in New Issue
Block a user