Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add forbid-in-block-sequences feature for detecting empty values in block sequences #604

Merged
merged 5 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions tests/rules/test_empty_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,60 @@ def test_in_flow_mappings_comments(self):
problem1=(4, 7),
problem2=(7, 9),
problem3=(10, 5))

def test_in_list_items_disabled(self):
conf = ('empty-values: {forbid-in-block-mappings: false,\n'
' forbid-in-flow-mappings: false,\n'
' forbid-in-list-items: false}\n'
'braces: disable\n'
'commas: disable\n')
ericalmeidasp marked this conversation as resolved.
Show resolved Hide resolved
self.check('---\n'
'foo:\n'
' - bar\n'
' -\n', conf)
self.check('---\n'
'foo:\n'
' -\n', conf)

def test_in_list_items_primative_item(self):
conf = ('empty-values: {forbid-in-block-mappings: false,\n'
' forbid-in-flow-mappings: false,\n'
' forbid-in-list-items: true}\n'
'braces: disable\n'
'commas: disable\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'
' - null\n', conf)

def test_in_list_items_various_explicit_null(self):
conf = ('empty-values: {forbid-in-block-mappings: false,\n'
' forbid-in-flow-mappings: false,\n'
' forbid-in-list-items: true}\n'
'braces: disable\n'
'commas: disable\n')
self.check('---\n'
'foo:\n'
' - null\n', conf)
self.check('---\n'
'- null\n', conf)
self.check('---\n'
'foo:\n'
' - bar: null\n', conf)
self.check('---\n'
'- null\n'
'- null\n', conf)
35 changes: 33 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-list-items`` to prevent empty values in lists.

.. 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-list-items: false

.. rubric:: Examples

Expand Down Expand Up @@ -72,6 +74,26 @@

{a: 1, b:, c: 3}

#. With ``empty-values: {forbid-in-list-items: true}``

the following code snippet would **PASS**:
::

some-list:
- string item

the following code snippets would **FAIL**:
::

some-list:
-

::

some-list:
- string item
-

"""

import yaml
Expand All @@ -82,9 +104,11 @@
ID = 'empty-values'
TYPE = 'token'
CONF = {'forbid-in-block-mappings': bool,
'forbid-in-flow-mappings': bool}
'forbid-in-flow-mappings': bool,
'forbid-in-list-items': bool}
DEFAULT = {'forbid-in-block-mappings': True,
'forbid-in-flow-mappings': True}
'forbid-in-flow-mappings': True,
'forbid-in-list-items': False}


def check(conf, token, prev, next, nextnext, context):
Expand All @@ -102,3 +126,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-list-items']:
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 list items')
Loading