Don't omit escaping the second consecutive quote
This would produce invalid code for strings like `"x = ''; y = \"\""`.
This commit is contained in:
parent
bc6b912fcb
commit
cf8e998f46
3
black.py
3
black.py
@ -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] + '\\"'
|
||||
|
@ -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 = """""'
|
||||
|
Loading…
Reference in New Issue
Block a user