Don't omit escaping the second consecutive quote

This would produce invalid code for strings like `"x = ''; y = \"\""`.
This commit is contained in:
Łukasz Langa 2018-04-11 16:25:47 -07:00
parent bc6b912fcb
commit cf8e998f46
2 changed files with 19 additions and 0 deletions

View File

@ -1998,6 +1998,9 @@ def normalize_string_quotes(leaf: Leaf) -> None:
else:
new_body = escaped_orig_quote.sub(rf"\1{orig_quote}", body)
new_body = unescaped_new_quote.sub(rf"\1\\{new_quote}", new_body)
# Add escapes again for consecutive occurences of new_quote (sub
# doesn't match overlapping substrings).
new_body = unescaped_new_quote.sub(rf"\1\\{new_quote}", new_body)
if new_quote == '"""' and new_body[-1] == '"':
# edge case:
new_body = new_body[:-1] + '\\"'

View File

@ -30,6 +30,14 @@
the \'lazy\' dog.\n\
'
re.compile(r'[\\"]')
"x = ''; y = \"\""
"x = '''; y = \"\""
"x = ''''; y = \"\""
"x = '' ''; y = \"\""
"x = ''; y = \"\"\""
"x = '''; y = \"\"\"\""
"x = ''''; y = \"\"\"\"\""
"x = '' ''; y = \"\"\"\"\""
# output
@ -65,3 +73,11 @@
the 'lazy' dog.\n\
"
re.compile(r'[\\"]')
"x = ''; y = \"\""
"x = '''; y = \"\""
"x = ''''; y = \"\""
"x = '' ''; y = \"\""
'x = \'\'; y = """'
'x = \'\'\'; y = """"'
'x = \'\'\'\'; y = """""'
'x = \'\' \'\'; y = """""'