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.pull/314/head
							parent
							
								
									597e88bb7b
								
							
						
					
					
						commit
						731a9f52ea
					
				| @ -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)) | ||||||
| @ -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)) | ||||||
| @ -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.") | ||||||
| @ -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.") | ||||||
					Loading…
					
					
				
		Reference in New Issue