Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow backslash-escaped special characters in double-quoted strings (#700) #703

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions tests/rules/test_quoted_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from tests.common import RuleTestCase
import yaml.reader

from yamllint import config

Expand Down Expand Up @@ -475,6 +476,30 @@ def test_only_when_needed_extras(self):
'- "foo bar"\n',
conf, problem1=(3, 3), problem2=(7, 3), problem3=(11, 3))

def test_only_when_needed_special_characters(self):
conf = 'quoted-strings: {required: only-when-needed}\n'
self.check('---\n'
'k1: "\\u001b"\n',
conf)
self.check('---\n'
"k1: '\\u001b'\n",
conf, problem=(2, 5))
self.check('---\n'
'k1: \\u001b\n',
conf)
self.assertRaises(yaml.reader.ReaderError, self.check,
'---\n'
'k1: "\u001b"\n',
conf)
self.assertRaises(yaml.reader.ReaderError, self.check,
'---\n'
"k1: '\u001b'\n",
conf)
self.assertRaises(yaml.reader.ReaderError, self.check,
'---\n'
'k1: \u001b\n',
conf)

def test_octal_values(self):
conf = 'quoted-strings: {required: true}\n'

Expand Down
11 changes: 10 additions & 1 deletion yamllint/rules/quoted_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,19 @@ def _quote_match(quote_type, token_style):
(quote_type == 'double' and token_style == '"'))


def _quotes_are_needed(string, is_inside_a_flow):
def _quotes_are_needed(string, style, is_inside_a_flow):
# Quotes needed on strings containing flow tokens
if is_inside_a_flow and set(string) & {',', '[', ']', '{', '}'}:
return True

if style == '"':
try:
yaml.reader.Reader('').check_printable('key: ' + string)
except yaml.reader.ReaderError:
# Special characters in a double-quoted string
# are assumed to have been backslash-escaped
return True

loader = yaml.BaseLoader('key: ' + string)
# Remove the 5 first tokens corresponding to 'key: ' (StreamStartToken,
# BlockMappingStartToken, KeyToken, ScalarToken(value=key), ValueToken)
Expand Down Expand Up @@ -299,6 +307,7 @@ def check(conf, token, prev, next, nextnext, context):
# Quotes are not strictly needed here
if (token.style and tag == DEFAULT_SCALAR_TAG and token.value and
not _quotes_are_needed(token.value,
token.style,
context['flow_nest_count'] > 0)):
is_extra_required = any(re.search(r, token.value)
for r in conf['extra-required'])
Expand Down
Loading