diff --git a/tests/rules/test_document_start.py b/tests/rules/test_document_start.py index 20de9ad..f74bb79 100644 --- a/tests/rules/test_document_start.py +++ b/tests/rules/test_document_start.py @@ -102,3 +102,49 @@ class DocumentStartTestCase(RuleTestCase): '---\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)) diff --git a/yamllint/rules/document_start.py b/yamllint/rules/document_start.py index 9bab4b4..0e5d8a0 100644 --- a/yamllint/rules/document_start.py +++ b/yamllint/rules/document_start.py @@ -21,6 +21,10 @@ Use this rule to require or forbid the use of document start marker (``---``). * 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) @@ -79,8 +83,12 @@ from yamllint.linter import LintProblem 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): @@ -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 + + 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,