Fix quoted-strings rules not working for string values matching scalars

pull/235/head
Rui Pinge 5 years ago committed by GitHub
parent 91763f5476
commit 15aea73fbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -30,6 +30,8 @@ class QuotedTestCase(RuleTestCase):
'foo: \'bar\'\n', conf) 'foo: \'bar\'\n', conf)
self.check('---\n' self.check('---\n'
'bar: 123\n', conf) 'bar: 123\n', conf)
self.check('---\n'
'bar: "123"\n', conf)
def test_quote_type_any(self): def test_quote_type_any(self):
conf = 'quoted-strings: {quote-type: any}\n' conf = 'quoted-strings: {quote-type: any}\n'
@ -37,11 +39,14 @@ class QuotedTestCase(RuleTestCase):
'boolean1: true\n' 'boolean1: true\n'
'number1: 123\n' 'number1: 123\n'
'string1: foo\n' # fails 'string1: foo\n' # fails
'string2: "foo"\n' 'string2: "true"\n'
'string3: \'bar\'\n' 'string3: "123"\n'
'string4: !!str genericstring\n' 'string4: \'true\'\n'
'string5: !!str 456\n' 'string5: "foo"\n'
'string6: !!str "quotedgenericstring"\n' 'string6: \'bar\'\n'
'string7: !!str genericstring\n'
'string8: !!str 456\n'
'string9: !!str "quotedgenericstring"\n'
'binary: !!binary binstring\n' 'binary: !!binary binstring\n'
'integer: !!int intstring\n' 'integer: !!int intstring\n'
'boolean2: !!bool boolstring\n' 'boolean2: !!bool boolstring\n'
@ -69,15 +74,18 @@ class QuotedTestCase(RuleTestCase):
'number1: 123\n' 'number1: 123\n'
'string1: foo\n' # fails 'string1: foo\n' # fails
'string2: "foo"\n' # fails 'string2: "foo"\n' # fails
'string3: \'bar\'\n' 'string3: "true"\n' # fails
'string4: !!str genericstring\n' 'string4: "123"\n' # fails
'string5: !!str 456\n' 'string5: \'bar\'\n'
'string6: !!str "quotedgenericstring"\n' 'string6: !!str genericstring\n'
'string7: !!str 456\n'
'string8: !!str "quotedgenericstring"\n'
'binary: !!binary binstring\n' 'binary: !!binary binstring\n'
'integer: !!int intstring\n' 'integer: !!int intstring\n'
'boolean2: !!bool boolstring\n' 'boolean2: !!bool boolstring\n'
'boolean3: !!bool "quotedboolstring"\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' self.check('---\n'
'multiline string 1: |\n' 'multiline string 1: |\n'
' line 1\n' ' line 1\n'
@ -100,15 +108,18 @@ class QuotedTestCase(RuleTestCase):
'number1: 123\n' 'number1: 123\n'
'string1: foo\n' # fails 'string1: foo\n' # fails
'string2: "foo"\n' 'string2: "foo"\n'
'string3: \'bar\'\n' # fails 'string3: \'true\'\n' # fails
'string4: !!str genericstring\n' 'string4: \'123\'\n' # fails
'string5: !!str 456\n' 'string5: \'bar\'\n' # fails
'string6: !!str "quotedgenericstring"\n' 'string6: !!str genericstring\n'
'string7: !!str 456\n'
'string8: !!str "quotedgenericstring"\n'
'binary: !!binary binstring\n' 'binary: !!binary binstring\n'
'integer: !!int intstring\n' 'integer: !!int intstring\n'
'boolean2: !!bool boolstring\n' 'boolean2: !!bool boolstring\n'
'boolean3: !!bool "quotedboolstring"\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' self.check('---\n'
'multiline string 1: |\n' 'multiline string 1: |\n'
' line 1\n' ' line 1\n'

@ -52,14 +52,17 @@ DEFAULT = {'quote-type': 'any'}
def check(conf, token, prev, next, nextnext, context): def check(conf, token, prev, next, nextnext, context):
quote_type = conf['quote-type'] 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))): isinstance(prev, (yaml.ValueToken, yaml.TagToken))):
return
# Ignore explicit types, e.g. !!str testtest or !!int 42 # Ignore explicit types, e.g. !!str testtest or !!int 42
if (prev and isinstance(prev, yaml.tokens.TagToken) and if (prev and isinstance(prev, yaml.tokens.TagToken) and
prev.value[0] == '!!'): prev.value[0] == '!!'):
return return
# Ignore numbers, booleans, etc. # Ignore numbers, booleans, etc.
if token.plain:
resolver = yaml.resolver.Resolver() resolver = yaml.resolver.Resolver()
if resolver.resolve(yaml.nodes.ScalarNode, token.value, if resolver.resolve(yaml.nodes.ScalarNode, token.value,
(True, False)) != 'tag:yaml.org,2002:str': (True, False)) != 'tag:yaml.org,2002:str':

Loading…
Cancel
Save