diff --git a/tests/rules/test_quoted.py b/tests/rules/test_quoted.py new file mode 100644 index 0000000..43dec75 --- /dev/null +++ b/tests/rules/test_quoted.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- +# Copyright (C) 2018 ClearScore +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +from tests.common import RuleTestCase + + +class QuotedTestCase(RuleTestCase): + rule_id = 'quoted' + + def test_disabled(self): + conf = 'quoted: disable' + self.check('---\n' + 'foo: bar\n', conf) + self.check('---\n' + 'bar: 123\n', conf) + + def test_quote_type_any(self): + conf = 'quoted: {quote-type: any}\n' + self.check('---\n' + 'string1: "foo"\n' + 'number1: 123\n' + 'string2: foo\n' + 'string3: \'yes\'\n', + conf, problem1=(3, 10), problem2=(4, 10)) + + def test_quote_type_single(self): + conf = 'quoted: {quote-type: single}\n' + self.check('---\n' + 'string1: \'foo\'\n' + 'number1: 123\n' + 'string2: foo\n' + 'string3: "yes"\n', + conf, problem1=(3, 10), problem2=(4, 10), problem3=(5, 10)) + + def test_quote_type_double(self): + conf = 'quoted: {quote-type: double}\n' + self.check('---\n' + 'string1: "foo"\n' + 'number1: 123\n' + 'string2: foo\n' + 'string3: \'yes\'\n', + conf, problem1=(3, 10), problem2=(4, 10), problem3=(5, 10)) diff --git a/yamllint/rules/__init__.py b/yamllint/rules/__init__.py index a084d6e..891f5a3 100644 --- a/yamllint/rules/__init__.py +++ b/yamllint/rules/__init__.py @@ -36,6 +36,7 @@ from yamllint.rules import ( quoted_strings, trailing_spaces, truthy, + quoted, ) _RULES = { @@ -60,6 +61,7 @@ _RULES = { quoted_strings.ID: quoted_strings, trailing_spaces.ID: trailing_spaces, truthy.ID: truthy, + quoted.ID: quoted, } diff --git a/yamllint/rules/quoted.py b/yamllint/rules/quoted.py new file mode 100644 index 0000000..aa98720 --- /dev/null +++ b/yamllint/rules/quoted.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +# Copyright (C) 2018 ClearScore +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +""" +Use this rule to forbid any values that are not quoted. You can also enforce +the type of the quote used - single or double. + +.. rubric:: Examples + +#. With ``quoted: {quote-type: any}`` + + the following code snippet would **PASS**: + :: + + foo: "bar" + bar: 'foo' + + the following code snippet would **FAIL**: + :: + + foo: bar + bar: 123 +""" + +import yaml + +from yamllint.linter import LintProblem + +ID = 'quoted' +TYPE = 'token' +CONF = {'quote-type': ('any', 'single', 'double')} + + +def check(conf, token, prev, next, nextnext, context): + if prev and isinstance(prev, yaml.tokens.TagToken): + return + + if isinstance(token, yaml.tokens.ScalarToken): + + if isinstance(prev, yaml.tokens.ValueToken): + quote_type = conf['quote-type'] + if ((quote_type == 'single' and token.style != "'") or + (quote_type == 'double' and token.style != '"') or + (quote_type == 'any' and token.style is None)): + yield LintProblem( + token.start_mark.line + 1, + token.start_mark.column + 1, + "value is not quoted (using %s quote)" % quote_type + )