diff --git a/tests/rules/test_quoted_strings.py b/tests/rules/test_quoted_strings.py index 04ee492..973660c 100644 --- a/tests/rules/test_quoted_strings.py +++ b/tests/rules/test_quoted_strings.py @@ -30,6 +30,8 @@ class QuotedTestCase(RuleTestCase): 'foo: \'bar\'\n', conf) self.check('---\n' 'bar: 123\n', conf) + self.check('---\n' + 'bar: "123"\n', conf) def test_quote_type_any(self): conf = 'quoted-strings: {quote-type: any}\n' @@ -37,11 +39,14 @@ class QuotedTestCase(RuleTestCase): 'boolean1: true\n' 'number1: 123\n' 'string1: foo\n' # fails - 'string2: "foo"\n' - 'string3: \'bar\'\n' - 'string4: !!str genericstring\n' - 'string5: !!str 456\n' - 'string6: !!str "quotedgenericstring"\n' + 'string2: "true"\n' + 'string3: "123"\n' + 'string4: \'true\'\n' + 'string5: "foo"\n' + 'string6: \'bar\'\n' + 'string7: !!str genericstring\n' + 'string8: !!str 456\n' + 'string9: !!str "quotedgenericstring"\n' 'binary: !!binary binstring\n' 'integer: !!int intstring\n' 'boolean2: !!bool boolstring\n' @@ -69,15 +74,18 @@ class QuotedTestCase(RuleTestCase): 'number1: 123\n' 'string1: foo\n' # fails 'string2: "foo"\n' # fails - 'string3: \'bar\'\n' - 'string4: !!str genericstring\n' - 'string5: !!str 456\n' - 'string6: !!str "quotedgenericstring"\n' + 'string3: "true"\n' # fails + 'string4: "123"\n' # fails + 'string5: \'bar\'\n' + 'string6: !!str genericstring\n' + 'string7: !!str 456\n' + 'string8: !!str "quotedgenericstring"\n' 'binary: !!binary binstring\n' 'integer: !!int intstring\n' 'boolean2: !!bool boolstring\n' 'boolean3: !!bool "quotedboolstring"\n', - conf, problem1=(4, 10), problem2=(5, 10)) + conf, problem1=(4, 10), problem2=(5, 10), + problem3=(6, 10), problem4=(7, 10)) self.check('---\n' 'multiline string 1: |\n' ' line 1\n' @@ -100,15 +108,18 @@ class QuotedTestCase(RuleTestCase): 'number1: 123\n' 'string1: foo\n' # fails 'string2: "foo"\n' - 'string3: \'bar\'\n' # fails - 'string4: !!str genericstring\n' - 'string5: !!str 456\n' - 'string6: !!str "quotedgenericstring"\n' + 'string3: \'true\'\n' # fails + 'string4: \'123\'\n' # fails + 'string5: \'bar\'\n' # fails + 'string6: !!str genericstring\n' + 'string7: !!str 456\n' + 'string8: !!str "quotedgenericstring"\n' 'binary: !!binary binstring\n' 'integer: !!int intstring\n' 'boolean2: !!bool boolstring\n' 'boolean3: !!bool "quotedboolstring"\n', - conf, problem1=(4, 10), problem2=(6, 10)) + conf, problem1=(4, 10), problem2=(6, 10), + problem3=(7, 10), problem4=(8, 10)) self.check('---\n' 'multiline string 1: |\n' ' line 1\n' diff --git a/yamllint/rules/quoted_strings.py b/yamllint/rules/quoted_strings.py index b019261..4fc5aa2 100644 --- a/yamllint/rules/quoted_strings.py +++ b/yamllint/rules/quoted_strings.py @@ -52,27 +52,30 @@ DEFAULT = {'quote-type': 'any'} def check(conf, token, prev, next, nextnext, context): quote_type = conf['quote-type'] - if (isinstance(token, yaml.tokens.ScalarToken) and + if not (isinstance(token, yaml.tokens.ScalarToken) and isinstance(prev, (yaml.ValueToken, yaml.TagToken))): - # Ignore explicit types, e.g. !!str testtest or !!int 42 - if (prev and isinstance(prev, yaml.tokens.TagToken) and - prev.value[0] == '!!'): - return + return + + # Ignore explicit types, e.g. !!str testtest or !!int 42 + if (prev and isinstance(prev, yaml.tokens.TagToken) and + prev.value[0] == '!!'): + return - # Ignore numbers, booleans, etc. + # Ignore numbers, booleans, etc. + if token.plain: resolver = yaml.resolver.Resolver() if resolver.resolve(yaml.nodes.ScalarNode, token.value, (True, False)) != 'tag:yaml.org,2002:str': return - # Ignore multi-line strings - if (not token.plain) and (token.style == "|" or token.style == ">"): - return - - 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, - "string value is not quoted with %s quotes" % (quote_type)) + # Ignore multi-line strings + if (not token.plain) and (token.style == "|" or token.style == ">"): + return + + 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, + "string value is not quoted with %s quotes" % (quote_type))