From 731a9f52eaa2ea34fda02d059e1faa54f8adae16 Mon Sep 17 00:00:00 2001 From: Satoru SATOH Date: Fri, 4 Sep 2020 03:39:53 +0900 Subject: [PATCH] Add a pair of rules to forbid flow collection styles as needed Add a pair of new rules to forbid flow collection styles (flow mapping and flow sequence) as needed, add those test cases and update default configuration to disable these rules by default. --- tests/test_flow_mapping.py | 34 ++++++++++++++++++++++++++ tests/test_flow_sequence.py | 36 ++++++++++++++++++++++++++++ yamllint/conf/default.yaml | 2 ++ yamllint/conf/relaxed.yaml | 2 ++ yamllint/rules/__init__.py | 4 ++++ yamllint/rules/flow_mapping.py | 42 +++++++++++++++++++++++++++++++++ yamllint/rules/flow_sequence.py | 39 ++++++++++++++++++++++++++++++ 7 files changed, 159 insertions(+) create mode 100644 tests/test_flow_mapping.py create mode 100644 tests/test_flow_sequence.py create mode 100644 yamllint/rules/flow_mapping.py create mode 100644 yamllint/rules/flow_sequence.py diff --git a/tests/test_flow_mapping.py b/tests/test_flow_mapping.py new file mode 100644 index 0000000..56f562b --- /dev/null +++ b/tests/test_flow_mapping.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +# Copyright (C) 2020 Satoru SATOH +# +# SPDX-License-Identifier: GPL-3.0-or-later +# +# pylint: disable=missing-function-docstring +"""test cases for flow-mapping rule. +""" +import tests.common + + +class FlowMappingTestCase(tests.common.RuleTestCase): + """Flow Mapping test cases. + """ + rule_id = 'flow-mapping' + + def test_disabled(self): + conf = 'flow-mapping: disable' + self.check('---\n' + '1: {"a": 2}\n', conf) + + def test_enabled(self): + conf = 'flow-mapping: enable\n' + self.check('---\n' + '1: {"a": 2}\n', + conf, problem1=(2, 4)) + self.check('---\n' + '1:\n' + ' a: 2\n', conf) + self.check('---\n' + '1: {\n' + ' "a": 2,\n' + '}\n', conf, + problem1=(2, 4)) diff --git a/tests/test_flow_sequence.py b/tests/test_flow_sequence.py new file mode 100644 index 0000000..a73ff05 --- /dev/null +++ b/tests/test_flow_sequence.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +# Copyright (C) 2020 Satoru SATOH +# +# SPDX-License-Identifier: GPL-3.0-or-later +# +# pylint: disable=missing-function-docstring +"""test cases for flow-sequence rule. +""" +import tests.common + + +class FlowSequenceTestCase(tests.common.RuleTestCase): + """Flow Sequence test cases. + """ + rule_id = 'flow-sequence' + + def test_disabled(self): + conf = 'flow-sequence: disable' + self.check('---\n' + '1: [2, 3]\n', conf) + + def test_enabled(self): + conf = 'flow-sequence: enable\n' + self.check('---\n' + '1: [2, 3]\n', + conf, problem1=(2, 4)) + self.check('---\n' + '1:\n' + ' - 2\n' + ' - 3\n', conf) + self.check('---\n' + '[\n' + ' 1,\n' + ' 2\n' + ']\n', conf, + problem1=(2, 1)) diff --git a/yamllint/conf/default.yaml b/yamllint/conf/default.yaml index 0720ded..732fa04 100644 --- a/yamllint/conf/default.yaml +++ b/yamllint/conf/default.yaml @@ -19,6 +19,8 @@ rules: level: warning empty-lines: enable empty-values: disable + flow-mapping: disable + flow-sequence: disable hyphens: enable indentation: enable key-duplicates: enable diff --git a/yamllint/conf/relaxed.yaml b/yamllint/conf/relaxed.yaml index 83f5340..dc603cc 100644 --- a/yamllint/conf/relaxed.yaml +++ b/yamllint/conf/relaxed.yaml @@ -18,6 +18,8 @@ rules: document-start: disable empty-lines: level: warning + flow-mapping: disable + flow-sequence: disable hyphens: level: warning indentation: diff --git a/yamllint/rules/__init__.py b/yamllint/rules/__init__.py index a084d6e..d88c9e7 100644 --- a/yamllint/rules/__init__.py +++ b/yamllint/rules/__init__.py @@ -25,6 +25,8 @@ from yamllint.rules import ( document_start, empty_lines, empty_values, + flow_mapping, + flow_sequence, hyphens, indentation, key_duplicates, @@ -49,6 +51,8 @@ _RULES = { document_start.ID: document_start, empty_lines.ID: empty_lines, empty_values.ID: empty_values, + flow_mapping.ID: flow_mapping, + flow_sequence.ID: flow_sequence, hyphens.ID: hyphens, indentation.ID: indentation, key_duplicates.ID: key_duplicates, diff --git a/yamllint/rules/flow_mapping.py b/yamllint/rules/flow_mapping.py new file mode 100644 index 0000000..5173edf --- /dev/null +++ b/yamllint/rules/flow_mapping.py @@ -0,0 +1,42 @@ +# +# Copyright (C) 2020 Satoru SATOH +# SPDX-License-Identifier: GPL-3.0-or-later +# +""" +Use this rule to forbid flow mappings of which content is denoted by +surrounding "{" and "}". + +.. rubric:: Examples + +#. The following code snippet would **PASS**: + :: + + foo: + bar: 1 + baz: + a: b + +#. The following code snippet would **FAIL**: + :: + + foo: {'bar': 1} + baz: { + 'a': 'b' + } +""" +import yaml + +from yamllint.linter import LintProblem + + +ID = 'flow-mapping' +TYPE = 'token' + + +def check(conf, token, _prev, _next, _nextnext, _context): + """Check if the toke starts flow mapping is found. + """ + if isinstance(token, yaml.FlowMappingStartToken): + yield LintProblem(token.start_mark.line + 1, + token.start_mark.column + 1, + "Flow mappings are forbidden.") diff --git a/yamllint/rules/flow_sequence.py b/yamllint/rules/flow_sequence.py new file mode 100644 index 0000000..8eb142f --- /dev/null +++ b/yamllint/rules/flow_sequence.py @@ -0,0 +1,39 @@ +# +# Copyright (C) 2020 Satoru SATOH +# SPDX-License-Identifier: GPL-3.0-or-later +# +""" +Use this rule to forbid flow sequences of which content is denoted by +surrounding "[" and "]". + +.. rubric:: Examples + +#. The following code snippet would **PASS**: + :: + + foo: + - bar + - baz + +#. The following code snippet would **FAIL**: + :: + + foo: ['bar', 'baz'] + +""" +import yaml + +from yamllint.linter import LintProblem + + +ID = 'flow-sequence' +TYPE = 'token' + + +def check(conf, token, _prev, _next, _nextnext, _context): + """Check if the toke starts flow sequnce is found. + """ + if isinstance(token, yaml.FlowSequenceStartToken): + yield LintProblem(token.start_mark.line + 1, + token.start_mark.column + 1, + "Flow Sequences are forbidden.")