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

adds min/max empty lines after document start/end #393

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
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))
46 changes: 46 additions & 0 deletions tests/rules/test_document_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,49 @@ def test_directives(self):
'---\n'
'doc: 2\n'
'...\n', conf)

def test_empty_lines(self):
conf = ('document-start:\n'
' present: true\n'
' max-empty-lines-after: 0\n')
self.check('---\n'
'doc: ument\n', conf)
self.check('---\n'
'\n'
'doc: ument\n', conf, problem=(3, 1))

conf = ('document-start:\n'
' present: true\n'
' max-empty-lines-after: 1\n')
self.check('---\n'
'doc: ument\n', conf)
self.check('---\n'
'\n'
'doc: ument\n', conf)
self.check('---\n'
'\n'
'\n'
'doc: ument\n', conf, problem=(4, 1))

conf = ('document-start:\n'
' present: true\n'
' min-empty-lines-after: 1\n')
self.check('---\n'
'\n'
'doc: ument\n', conf)
self.check('---\n'
'doc: ument\n', conf, problem=(2, 1))

conf = ('document-start:\n'
' present: true\n'
' max-empty-lines-after: 1\n'
' min-empty-lines-after: 1\n')
self.check('---\n'
'doc: ument\n', conf, problem=(2, 1))
self.check('---\n'
'\n'
'doc: ument\n', conf)
self.check('---\n'
'\n'
'\n'
'doc: ument\n', conf, problem=(4, 1))
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
27 changes: 25 additions & 2 deletions yamllint/rules/document_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

* Set ``present`` to ``true`` when the document start marker is required, or to
``false`` when it is forbidden.
* ``min-empty-lines-after`` defines the minimal number of empty lines after the
document start marker.
* ``max-empty-lines-after`` defines the maximal number of empty lines after the
document start marker.

.. rubric:: Default values (when enabled)

Expand Down Expand Up @@ -79,8 +83,12 @@

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


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

if isinstance(prev, yaml.DocumentStartToken):
empty_lines = token.start_mark.line - prev.start_mark.line - 1
wookietreiber marked this conversation as resolved.
Show resolved Hide resolved

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

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

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