diff --git a/tests/rules/test_commas.py b/tests/rules/test_commas.py index 09fe578..4f51e8b 100644 --- a/tests/rules/test_commas.py +++ b/tests/rules/test_commas.py @@ -152,3 +152,36 @@ class CommaTestCase(RuleTestCase): problem1=(2, 12), problem2=(2, 16), problem3=(2, 31), problem4=(2, 36), problem5=(2, 50), problem6=(4, 8), problem7=(5, 11), problem8=(8, 13)) + + def test_comma_on_new_line(self): + conf = 'commas: {max-spaces-before: 0, max-spaces-after: 1}' + self.check('---\n' + 'flow-seq: [1, 2, 3\n' + ' , 4, 5, 6]\n' + '...\n', conf, problem=(3, 11)) + self.check('---\n' + 'flow-map: {a: 1, b: 2\n' + ' , c: 3}\n' + '...\n', conf, problem=(3, 11)) + conf = ('commas: {max-spaces-before: 0, max-spaces-after: 1}\n' + 'indentation: disable\n') + self.check('---\n' + 'flow-seq: [1, 2, 3\n' + ' , 4, 5, 6]\n' + '...\n', conf, problem=(3, 9)) + self.check('---\n' + 'flow-map: {a: 1, b: 2\n' + ' , c: 3}\n' + '...\n', conf, problem=(3, 9)) + self.check('---\n' + '[\n' + '1,\n' + '2\n' + ', 3\n' + ']\n', conf, problem=(5, 1)) + self.check('---\n' + '{\n' + 'a: 1,\n' + 'b: 2\n' + ', c: 3\n' + '}\n', conf, problem=(5, 1)) diff --git a/yamllint/rules/commas.py b/yamllint/rules/commas.py index 5cdb5de..3ef3b99 100644 --- a/yamllint/rules/commas.py +++ b/yamllint/rules/commas.py @@ -16,6 +16,7 @@ import yaml +from yamllint.errors import LintProblem from yamllint.rules.common import spaces_after, spaces_before @@ -27,11 +28,16 @@ CONF = {'max-spaces-before': int, def check(conf, token, prev, next, context): if isinstance(token, yaml.FlowEntryToken): - problem = spaces_before(token, prev, next, - max=conf['max-spaces-before'], - max_desc='too many spaces before comma') - if problem is not None: - yield problem + if prev is not None and prev.end_mark.line < token.start_mark.line: + yield LintProblem(token.start_mark.line + 1, + max(1, token.start_mark.column), + 'too many spaces before comma') + else: + problem = spaces_before(token, prev, next, + max=conf['max-spaces-before'], + max_desc='too many spaces before comma') + if problem is not None: + yield problem problem = spaces_after(token, prev, next, max=conf['max-spaces-after'],