Skip to content

Commit

Permalink
Fix continuation detection following multi-line strings (#9332)
Browse files Browse the repository at this point in the history
## Summary

The logic that detects continuations assumed that tokens themselves
cannot span multiple lines. However, strings _can_ -- even single-quoted
strings.

Closes #9323.
  • Loading branch information
charliermarsh authored Dec 31, 2023
1 parent 003851c commit b3789cd
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
16 changes: 16 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pycodestyle/W293.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# See: https://github.com/astral-sh/ruff/issues/9323
class Chassis(RobotModuleTemplate):
"""底盘信息推送控制
"""\


"""""" \
\



"abc\
" \
\

1 change: 1 addition & 0 deletions crates/ruff_linter/src/rules/pycodestyle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod tests {
#[test_case(Rule::LambdaAssignment, Path::new("E731.py"))]
#[test_case(Rule::BareExcept, Path::new("E722.py"))]
#[test_case(Rule::BlankLineWithWhitespace, Path::new("W29.py"))]
#[test_case(Rule::BlankLineWithWhitespace, Path::new("W293.py"))]
#[test_case(Rule::InvalidEscapeSequence, Path::new("W605_0.py"))]
#[test_case(Rule::InvalidEscapeSequence, Path::new("W605_1.py"))]
#[test_case(Rule::LineTooLong, Path::new("E501.py"))]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs
---
W293.py:4:1: W293 [*] Blank line contains whitespace
|
2 | class Chassis(RobotModuleTemplate):
3 | """底盘信息推送控制
4 |
| ^^^^ W293
5 | """\
|
= help: Remove whitespace from blank line

Safe fix
1 1 | # See: https://github.com/astral-sh/ruff/issues/9323
2 2 | class Chassis(RobotModuleTemplate):
3 3 | """底盘信息推送控制
4 |-
4 |+
5 5 | """\
6 6 |
7 7 |

W293.py:10:1: W293 [*] Blank line contains whitespace
|
8 | """""" \
9 | \
10 |
| ^^^^ W293
|
= help: Remove whitespace from blank line

Safe fix
5 5 | """\
6 6 |
7 7 |
8 |-"""""" \
9 |- \
10 |-
8 |+""""""
11 9 |
12 10 |
13 11 | "abc\

W293.py:16:1: W293 [*] Blank line contains whitespace
|
14 | " \
15 | \
16 |
| ^^^^ W293
|
= help: Remove whitespace from blank line

Safe fix
11 11 |
12 12 |
13 13 | "abc\
14 |- " \
15 |- \
16 |-
14 |+ "


13 changes: 11 additions & 2 deletions crates/ruff_python_index/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,22 @@ impl Indexer {
comment_ranges_builder.visit_token(tok, *range);
fstring_ranges_builder.visit_token(tok, *range);

if matches!(tok, Tok::Newline | Tok::NonLogicalNewline) {
line_start = range.end();
match tok {
Tok::Newline | Tok::NonLogicalNewline => {
line_start = range.end();
}
Tok::String { .. } => {
// If the previous token was a string, find the start of the line that contains
// the closing delimiter, since the token itself can span multiple lines.
line_start = locator.line_start(range.end());
}
_ => {}
}

prev_token = Some(tok);
prev_end = range.end();
}

Self {
comment_ranges: comment_ranges_builder.finish(),
continuation_lines,
Expand Down

0 comments on commit b3789cd

Please sign in to comment.