empty-values: Add forbid-in-block-sequences option

Implement forbid-in-block-sequences option for detecting empty list items.
This commit is contained in:
Éric Almeida 2023-11-09 06:29:01 -03:00 committed by GitHub
parent e1d45c3ae5
commit a26dd0084c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 162 additions and 18 deletions

View File

@ -42,7 +42,8 @@ class EmptyValuesTestCase(RuleTestCase):
def test_in_block_mappings_disabled(self):
conf = ('empty-values: {forbid-in-block-mappings: false,\n'
' forbid-in-flow-mappings: false}\n')
' forbid-in-flow-mappings: false,\n'
' forbid-in-block-sequences: false}\n')
self.check('---\n'
'foo:\n', conf)
self.check('---\n'
@ -51,7 +52,8 @@ class EmptyValuesTestCase(RuleTestCase):
def test_in_block_mappings_single_line(self):
conf = ('empty-values: {forbid-in-block-mappings: true,\n'
' forbid-in-flow-mappings: false}\n')
' forbid-in-flow-mappings: false,\n'
' forbid-in-block-sequences: false}\n')
self.check('---\n'
'implicitly-null:\n', conf, problem1=(2, 17))
self.check('---\n'
@ -63,7 +65,8 @@ class EmptyValuesTestCase(RuleTestCase):
def test_in_block_mappings_all_lines(self):
conf = ('empty-values: {forbid-in-block-mappings: true,\n'
' forbid-in-flow-mappings: false}\n')
' forbid-in-flow-mappings: false,\n'
' forbid-in-block-sequences: false}\n')
self.check('---\n'
'foo:\n'
'bar:\n'
@ -72,14 +75,16 @@ class EmptyValuesTestCase(RuleTestCase):
def test_in_block_mappings_explicit_end_of_document(self):
conf = ('empty-values: {forbid-in-block-mappings: true,\n'
' forbid-in-flow-mappings: false}\n')
' forbid-in-flow-mappings: false,\n'
' forbid-in-block-sequences: false}\n')
self.check('---\n'
'foo:\n'
'...\n', conf, problem1=(2, 5))
def test_in_block_mappings_not_end_of_document(self):
conf = ('empty-values: {forbid-in-block-mappings: true,\n'
' forbid-in-flow-mappings: false}\n')
' forbid-in-flow-mappings: false,\n'
' forbid-in-block-sequences: false}\n')
self.check('---\n'
'foo:\n'
'bar:\n'
@ -87,7 +92,8 @@ class EmptyValuesTestCase(RuleTestCase):
def test_in_block_mappings_different_level(self):
conf = ('empty-values: {forbid-in-block-mappings: true,\n'
' forbid-in-flow-mappings: false}\n')
' forbid-in-flow-mappings: false,\n'
' forbid-in-block-sequences: false}\n')
self.check('---\n'
'foo:\n'
' bar:\n'
@ -95,7 +101,8 @@ class EmptyValuesTestCase(RuleTestCase):
def test_in_block_mappings_empty_flow_mapping(self):
conf = ('empty-values: {forbid-in-block-mappings: true,\n'
' forbid-in-flow-mappings: false}\n'
' forbid-in-flow-mappings: false,\n'
' forbid-in-block-sequences: false}\n'
'braces: disable\n'
'commas: disable\n')
self.check('---\n'
@ -107,14 +114,16 @@ class EmptyValuesTestCase(RuleTestCase):
def test_in_block_mappings_empty_block_sequence(self):
conf = ('empty-values: {forbid-in-block-mappings: true,\n'
' forbid-in-flow-mappings: false}\n')
' forbid-in-flow-mappings: false,\n'
' forbid-in-block-sequences: false}\n')
self.check('---\n'
'foo:\n'
' -\n', conf)
def test_in_block_mappings_not_empty_or_explicit_null(self):
conf = ('empty-values: {forbid-in-block-mappings: true,\n'
' forbid-in-flow-mappings: false}\n')
' forbid-in-flow-mappings: false,\n'
' forbid-in-block-sequences: false}\n')
self.check('---\n'
'foo:\n'
' bar:\n'
@ -137,7 +146,8 @@ class EmptyValuesTestCase(RuleTestCase):
def test_in_block_mappings_various_explicit_null(self):
conf = ('empty-values: {forbid-in-block-mappings: true,\n'
' forbid-in-flow-mappings: false}\n')
' forbid-in-flow-mappings: false,\n'
' forbid-in-block-sequences: false}\n')
self.check('---\n'
'null-alias: ~\n', conf)
self.check('---\n'
@ -147,7 +157,8 @@ class EmptyValuesTestCase(RuleTestCase):
def test_in_block_mappings_comments(self):
conf = ('empty-values: {forbid-in-block-mappings: true,\n'
' forbid-in-flow-mappings: false}\n'
' forbid-in-flow-mappings: false,\n'
' forbid-in-block-sequences: false}\n'
'comments: disable\n')
self.check('---\n'
'empty: # comment\n'
@ -158,7 +169,8 @@ class EmptyValuesTestCase(RuleTestCase):
def test_in_flow_mappings_disabled(self):
conf = ('empty-values: {forbid-in-block-mappings: false,\n'
' forbid-in-flow-mappings: false}\n'
' forbid-in-flow-mappings: false,\n'
' forbid-in-block-sequences: false}\n'
'braces: disable\n'
'commas: disable\n')
self.check('---\n'
@ -175,7 +187,8 @@ class EmptyValuesTestCase(RuleTestCase):
def test_in_flow_mappings_single_line(self):
conf = ('empty-values: {forbid-in-block-mappings: false,\n'
' forbid-in-flow-mappings: true}\n'
' forbid-in-flow-mappings: true,\n'
' forbid-in-block-sequences: false}\n'
'braces: disable\n'
'commas: disable\n')
self.check('---\n'
@ -201,7 +214,8 @@ class EmptyValuesTestCase(RuleTestCase):
def test_in_flow_mappings_multi_line(self):
conf = ('empty-values: {forbid-in-block-mappings: false,\n'
' forbid-in-flow-mappings: true}\n'
' forbid-in-flow-mappings: true,\n'
' forbid-in-block-sequences: false}\n'
'braces: disable\n'
'commas: disable\n')
self.check('---\n'
@ -226,7 +240,8 @@ class EmptyValuesTestCase(RuleTestCase):
def test_in_flow_mappings_various_explicit_null(self):
conf = ('empty-values: {forbid-in-block-mappings: false,\n'
' forbid-in-flow-mappings: true}\n'
' forbid-in-flow-mappings: true,\n'
' forbid-in-block-sequences: false}\n'
'braces: disable\n'
'commas: disable\n')
self.check('---\n'
@ -240,7 +255,8 @@ class EmptyValuesTestCase(RuleTestCase):
def test_in_flow_mappings_comments(self):
conf = ('empty-values: {forbid-in-block-mappings: false,\n'
' forbid-in-flow-mappings: true}\n'
' forbid-in-flow-mappings: true,\n'
' forbid-in-block-sequences: false}\n'
'braces: disable\n'
'commas: disable\n'
'comments: disable\n')
@ -258,3 +274,95 @@ class EmptyValuesTestCase(RuleTestCase):
problem1=(4, 7),
problem2=(7, 9),
problem3=(10, 5))
def test_in_block_sequences_disabled(self):
conf = ('empty-values: {forbid-in-block-mappings: false,\n'
' forbid-in-flow-mappings: false,\n'
' forbid-in-block-sequences: false}\n')
self.check('---\n'
'foo:\n'
' - bar\n'
' -\n', conf)
self.check('---\n'
'foo:\n'
' -\n', conf)
def test_in_block_sequences_primative_item(self):
conf = ('empty-values: {forbid-in-block-mappings: false,\n'
' forbid-in-flow-mappings: false,\n'
' forbid-in-block-sequences: true}\n')
self.check('---\n'
'foo:\n'
' -\n', conf,
problem=(3, 4))
self.check('---\n'
'foo:\n'
' - bar\n'
' -\n', conf,
problem=(4, 4))
self.check('---\n'
'foo:\n'
' - 1\n'
' - 2\n'
' -\n', conf,
problem=(5, 4))
self.check('---\n'
'foo:\n'
' - true\n', conf)
def test_in_block_sequences_complex_objects(self):
conf = ('empty-values: {forbid-in-block-mappings: false,\n'
' forbid-in-flow-mappings: false,\n'
' forbid-in-block-sequences: true}\n')
self.check('---\n'
'foo:\n'
' - a: 1\n', conf)
self.check('---\n'
'foo:\n'
' - a: 1\n'
' -\n', conf,
problem=(4, 4))
self.check('---\n'
'foo:\n'
' - a: 1\n'
' b: 2\n'
' -\n', conf,
problem=(5, 4))
self.check('---\n'
'foo:\n'
' - a: 1\n'
' - b: 2\n'
' -\n', conf,
problem=(5, 4))
self.check('---\n'
'foo:\n'
' - - a\n'
' - b: 2\n'
' -\n', conf,
problem=(5, 6))
self.check('---\n'
'foo:\n'
' - - a\n'
' - b: 2\n'
' -\n', conf,
problem=(5, 4))
def test_in_block_sequences_various_explicit_null(self):
conf = ('empty-values: {forbid-in-block-mappings: false,\n'
' forbid-in-flow-mappings: false,\n'
' forbid-in-block-sequences: true}\n')
self.check('---\n'
'foo:\n'
' - null\n', conf)
self.check('---\n'
'- null\n', conf)
self.check('---\n'
'foo:\n'
' - bar: null\n'
' - null\n', conf)
self.check('---\n'
'- null\n'
'- null\n', conf)
self.check('---\n'
'- - null\n'
' - null\n', conf)

View File

@ -21,6 +21,7 @@ Use this rule to prevent nodes with empty content, that implicitly result in
* Use ``forbid-in-block-mappings`` to prevent empty values in block mappings.
* Use ``forbid-in-flow-mappings`` to prevent empty values in flow mappings.
* Use ``forbid-in-block-sequences`` to prevent empty values in block sequences.
.. rubric:: Default values (when enabled)
@ -30,6 +31,7 @@ Use this rule to prevent nodes with empty content, that implicitly result in
empty-values:
forbid-in-block-mappings: true
forbid-in-flow-mappings: true
forbid-in-block-sequences: true
.. rubric:: Examples
@ -72,6 +74,31 @@ Use this rule to prevent nodes with empty content, that implicitly result in
{a: 1, b:, c: 3}
#. With ``empty-values: {forbid-in-block-sequences: true}``
the following code snippet would **PASS**:
::
some-sequence:
- string item
::
some-sequence:
- null
the following code snippets would **FAIL**:
::
some-sequence:
-
::
some-sequence:
- string item
-
"""
import yaml
@ -82,9 +109,11 @@ from yamllint.linter import LintProblem
ID = 'empty-values'
TYPE = 'token'
CONF = {'forbid-in-block-mappings': bool,
'forbid-in-flow-mappings': bool}
'forbid-in-flow-mappings': bool,
'forbid-in-block-sequences': bool}
DEFAULT = {'forbid-in-block-mappings': True,
'forbid-in-flow-mappings': True}
'forbid-in-flow-mappings': True,
'forbid-in-block-sequences': True}
def check(conf, token, prev, next, nextnext, context):
@ -102,3 +131,10 @@ def check(conf, token, prev, next, nextnext, context):
yield LintProblem(token.start_mark.line + 1,
token.end_mark.column + 1,
'empty value in flow mapping')
if conf['forbid-in-block-sequences']:
if isinstance(token, yaml.BlockEntryToken) and isinstance(next, (
yaml.KeyToken, yaml.BlockEndToken, yaml.BlockEntryToken)):
yield LintProblem(token.start_mark.line + 1,
token.end_mark.column + 1,
'empty value in block sequence')