Skip to content

Commit

Permalink
empty-values: Add forbid-in-block-sequences option
Browse files Browse the repository at this point in the history
Implement forbid-in-block-sequences option for detecting empty list items.
  • Loading branch information
ericalmeidasp authored Nov 9, 2023
1 parent e1d45c3 commit a26dd00
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 18 deletions.
140 changes: 124 additions & 16 deletions tests/rules/test_empty_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def test_disabled(self):

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'
Expand All @@ -51,7 +52,8 @@ def test_in_block_mappings_disabled(self):

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'
Expand All @@ -63,7 +65,8 @@ def test_in_block_mappings_single_line(self):

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'
Expand All @@ -72,30 +75,34 @@ def test_in_block_mappings_all_lines(self):

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'
' aaa\n', conf, problem1=(2, 5))

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'
'aaa: bbb\n', conf, problem1=(3, 6))

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'
Expand All @@ -107,14 +114,16 @@ def test_in_block_mappings_empty_flow_mapping(self):

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'
Expand All @@ -137,7 +146,8 @@ def test_in_block_mappings_not_empty_or_explicit_null(self):

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'
Expand All @@ -147,7 +157,8 @@ def test_in_block_mappings_various_explicit_null(self):

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'
Expand All @@ -158,7 +169,8 @@ def test_in_block_mappings_comments(self):

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'
Expand All @@ -175,7 +187,8 @@ def test_in_flow_mappings_disabled(self):

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'
Expand All @@ -201,7 +214,8 @@ def test_in_flow_mappings_single_line(self):

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'
Expand All @@ -226,7 +240,8 @@ def test_in_flow_mappings_multi_line(self):

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'
Expand All @@ -240,7 +255,8 @@ def test_in_flow_mappings_various_explicit_null(self):

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')
Expand All @@ -258,3 +274,95 @@ def test_in_flow_mappings_comments(self):
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)
40 changes: 38 additions & 2 deletions yamllint/rules/empty_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* 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)
Expand All @@ -30,6 +31,7 @@
empty-values:
forbid-in-block-mappings: true
forbid-in-flow-mappings: true
forbid-in-block-sequences: true
.. rubric:: Examples
Expand Down Expand Up @@ -72,6 +74,31 @@
{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
Expand All @@ -82,9 +109,11 @@
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):
Expand All @@ -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')

0 comments on commit a26dd00

Please sign in to comment.