From 847f7e3fff3c1031e37b60fe26f5d2dce5f206d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Verg=C3=A9?= Date: Wed, 20 Jan 2016 08:38:14 +0100 Subject: [PATCH] Rules: comments: Fix bug when multi-line scalar YAML content like the following one produced an error, because the ScalarToken associated whose value is "this is plain text" ends at the beginning of the 5th line (the one with the comment): --- string: > this is plain text # comment --- tests/rules/test_comments.py | 16 ++++++++++++++++ yamllint/rules/comments.py | 12 +++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/tests/rules/test_comments.py b/tests/rules/test_comments.py index 4f64a97..e6468f1 100644 --- a/tests/rules/test_comments.py +++ b/tests/rules/test_comments.py @@ -131,3 +131,19 @@ class CommentsTestCase(RuleTestCase): ' require-starting-space: yes\n' ' min-spaces-from-content: 2\n') self.check('# comment\n', conf) + + def test_multi_line_scalar(self): + conf = ('comments:\n' + ' require-starting-space: yes\n' + ' min-spaces-from-content: 2\n' + 'trailing-spaces: disable\n') + self.check('---\n' + 'string: >\n' + ' this is plain text\n' + '\n' + '# comment\n', conf) + self.check('---\n' + '- string: >\n' + ' this is plain text\n' + ' \n' + ' # comment\n', conf) diff --git a/yamllint/rules/comments.py b/yamllint/rules/comments.py index c2ec209..854755e 100644 --- a/yamllint/rules/comments.py +++ b/yamllint/rules/comments.py @@ -30,11 +30,13 @@ def check(conf, token, prev, next, context): for comment in get_comments_between_tokens(token, next): if (conf['min-spaces-from-content'] != -1 and not isinstance(token, yaml.StreamStartToken) and - comment.line == token.end_mark.line + 1 and - comment.pointer - token.end_mark.pointer < - conf['min-spaces-from-content']): - yield LintProblem(comment.line, comment.column, - 'too few spaces before comment') + comment.line == token.end_mark.line + 1): + # Sometimes token end marks are on the next line + if token.end_mark.buffer[token.end_mark.pointer - 1] != '\n': + if (comment.pointer - token.end_mark.pointer < + conf['min-spaces-from-content']): + yield LintProblem(comment.line, comment.column, + 'too few spaces before comment') if (conf['require-starting-space'] and comment.pointer + 1 < len(comment.buffer) and