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 formatting of trailing unescaped quotes in raw triple quoted strings #6202

Merged
merged 3 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
r"Test"
R"Test"

# Block conversion if there is an unescaped quote just before the end of the triple
# quoted string
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"""\"""""`, the last previously inner quote
// we shorten the quoted part and turn the last triple quote char into an
// unterminated string start.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this comment. What I understand from the code is that the match now returns true for a single quote at the end of the string. What about a triple quoted string that ends with two quotes?
'''""'''

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, fixed

None => break true,
Some(next) if *next == configured_quote_char => {
// `""` or `''`
chars.next();

if chars.peek() == Some(&configured_quote_char) {
// `"""` or `'''`
break true;
if chars.peek() == Some(&configured_quote_char) {
// `"""` or `'''`
break true;
}
}
_ => {}
}
}
Some(_) => continue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ 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'\""'

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

Expand Down Expand Up @@ -162,6 +167,11 @@ 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'\""'

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

Expand Down Expand Up @@ -310,6 +320,11 @@ 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'\""'

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

Expand Down