Skip to content

Commit

Permalink
Fix formatting of trailing unescaped quotes in raw triple quoted stri…
Browse files Browse the repository at this point in the history
…ngs (#6202)

**Summary** This prevents us from turning `r'''\""'''` into
`r"""\"""""`, which is invalid syntax.

This PR fixes CI, which is currently broken on main (in a way that still
passes on linter PRs and allows merging formatter PRs, but it's bad to
have a job be red). Once merged, i'll make the formatted ecosystem
checks a required check.

**Test Plan** Added a regression test.
  • Loading branch information
konstin authored Jul 31, 2023
1 parent dbd60b2 commit 9063f45
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
r"Test"
R"Test"

# Block conversion if there is an unescaped quote just before the end of the triple
# quoted string
r'''\""'''
r'''""'''
r'\""'

'This string will not include \
backslashes or newline characters.'

Expand Down
19 changes: 13 additions & 6 deletions crates/ruff_python_formatter/src/expression/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,21 @@ fn preferred_quotes_raw(
break true;
}

if chars.peek() == Some(&configured_quote_char) {
// `""` or `''`
chars.next();
match chars.peek() {
// We can't turn `r'''\""'''` into `r"""\"""""`, this would confuse the parser
// about where the closing triple quotes start
None => break true,
Some(next) if *next == configured_quote_char => {
// `""` or `''`
chars.next();

if chars.peek() == Some(&configured_quote_char) {
// `"""` or `'''`
break true;
// We can't turn `r'''""'''` into `r""""""""`, nor can we have
// `"""` or `'''` respectively inside the string
if chars.peek().is_none() || chars.peek() == Some(&configured_quote_char) {
break true;
}
}
_ => {}
}
}
Some(_) => continue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ U"Test"
r"Test"
R"Test"
# Block conversion if there is an unescaped quote just before the end of the triple
# quoted string
r'''\""'''
r'''""'''
r'\""'
'This string will not include \
backslashes or newline characters.'
Expand Down Expand Up @@ -162,6 +168,12 @@ magic-trailing-comma = Respect
r"Test"
R"Test"
# Block conversion if there is an unescaped quote just before the end of the triple
# quoted string
r'''\""'''
r'''""'''
r'\""'
"This string will not include \
backslashes or newline characters."
Expand Down Expand Up @@ -310,6 +322,12 @@ magic-trailing-comma = Respect
r'Test'
R'Test'
# Block conversion if there is an unescaped quote just before the end of the triple
# quoted string
r'''\""'''
r'''""'''
r'\""'
'This string will not include \
backslashes or newline characters.'
Expand Down

0 comments on commit 9063f45

Please sign in to comment.