Skip to content

Commit

Permalink
[document-end] adds min/max empty lines before
Browse files Browse the repository at this point in the history
  • Loading branch information
wookietreiber committed Jul 30, 2021
1 parent 0edff4f commit 88d302e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
12 changes: 12 additions & 0 deletions tests/rules/test_document_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,15 @@ def test_multiple_documents(self):
'---\n'
'third: document\n'
'...\n', conf, problem=(6, 1))

def test_empty_lines(self):
conf = ('document-end:\n'
' present: true\n'
' max-empty-lines-before: 0\n')
self.check('---\n'
'doc: ument\n'
'...\n', conf)
self.check('---\n'
'doc: ument\n'
'\n'
'...\n', conf, problem=(3, 0))
27 changes: 25 additions & 2 deletions yamllint/rules/document_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
* Set ``present`` to ``true`` when the document end marker is required, or to
``false`` when it is forbidden.
* ``min-empty-lines-before`` defines the minimal number of empty lines before
the document end marker.
* ``max-empty-lines-before`` defines the maximal number of empty lines before
the document end marker.
.. rubric:: Default values (when enabled)
Expand Down Expand Up @@ -89,8 +93,12 @@

ID = 'document-end'
TYPE = 'token'
CONF = {'present': bool}
DEFAULT = {'present': True}
CONF = {'present': bool,
'max-empty-lines-before': int,
'min-empty-lines-before': int}
DEFAULT = {'present': True,
'max-empty-lines-before': -1,
'min-empty-lines-before': 0}


def check(conf, token, prev, next, nextnext, context):
Expand All @@ -108,6 +116,21 @@ def check(conf, token, prev, next, nextnext, context):
yield LintProblem(token.start_mark.line + 1, 1,
'missing document end "..."')

if isinstance(next, yaml.DocumentEndToken):
empty_lines = next.start_mark.line - prev.start_mark.line - 1

if (conf['max-empty-lines-before'] >= 0 and
empty_lines > conf['max-empty-lines-before']):
yield LintProblem(token.start_mark.line,
token.start_mark.column,
'too many empty lines before document end')

if (conf['min-empty-lines-before'] > 0 and
empty_lines < conf['min-empty-lines-before']):
yield LintProblem(token.start_mark.line,
token.start_mark.column,
'too few empty lines before document end')

else:
if isinstance(token, yaml.DocumentEndToken):
yield LintProblem(token.start_mark.line + 1,
Expand Down

0 comments on commit 88d302e

Please sign in to comment.