From 333ae52c78c3968090ce5d69b15fbf58dc9dc9af Mon Sep 17 00:00:00 2001 From: Satoru SATOH Date: Thu, 10 Sep 2020 02:34:59 +0900 Subject: [PATCH] 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. --- tests/rules/test_braces.py | 30 +++++++++++++++++++++++++++++ tests/rules/test_brackets.py | 29 ++++++++++++++++++++++++++++ yamllint/rules/braces.py | 35 ++++++++++++++++++++++++++++++---- yamllint/rules/brackets.py | 37 +++++++++++++++++++++++++++++++----- 4 files changed, 122 insertions(+), 9 deletions(-) diff --git a/tests/rules/test_braces.py b/tests/rules/test_braces.py index 308715b..69c5461 100644 --- a/tests/rules/test_braces.py +++ b/tests/rules/test_braces.py @@ -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' diff --git a/tests/rules/test_brackets.py b/tests/rules/test_brackets.py index 273f1a7..a35eff9 100644 --- a/tests/rules/test_brackets.py +++ b/tests/rules/test_brackets.py @@ -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' diff --git a/yamllint/rules/braces.py b/yamllint/rules/braces.py index 0d28aee..759306e 100644 --- a/yamllint/rules/braces.py +++ b/yamllint/rules/braces.py @@ -15,10 +15,14 @@ # along with this program. If not, see . """ -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'] diff --git a/yamllint/rules/brackets.py b/yamllint/rules/brackets.py index 587c4f0..6ab02df 100644 --- a/yamllint/rules/brackets.py +++ b/yamllint/rules/brackets.py @@ -15,11 +15,14 @@ # along with this program. If not, see . """ -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']