Skip to content

Commit

Permalink
Do multiline string test for W293 too (#10049)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanxiyn committed Feb 19, 2024
1 parent 4ac1999 commit 1c8851e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
Expand Up @@ -14,3 +14,6 @@ class Chassis(RobotModuleTemplate):
" \
\

'''blank line with whitespace
inside a multiline string'''
Expand Up @@ -86,30 +86,33 @@ pub(crate) fn trailing_whitespace(
.sum();
if whitespace_len > TextSize::from(0) {
let range = TextRange::new(line.end() - whitespace_len, line.end());

// Removing trailing whitespace is not safe inside multiline strings.
let applicability = if indexer.multiline_ranges().contains_range(range) {
Applicability::Unsafe
} else {
Applicability::Safe
};
if range == line.range() {
if settings.rules.enabled(Rule::BlankLineWithWhitespace) {
let mut diagnostic = Diagnostic::new(BlankLineWithWhitespace, range);
// Remove any preceding continuations, to avoid introducing a potential
// syntax error.
diagnostic.set_fix(Fix::safe_edit(Edit::range_deletion(TextRange::new(
indexer
.preceded_by_continuations(line.start(), locator)
.unwrap_or(range.start()),
range.end(),
))));
diagnostic.set_fix(Fix::applicable_edit(
Edit::range_deletion(TextRange::new(
indexer
.preceded_by_continuations(line.start(), locator)
.unwrap_or(range.start()),
range.end(),
)),
applicability,
));
return Some(diagnostic);
}
} else if settings.rules.enabled(Rule::TrailingWhitespace) {
let mut diagnostic = Diagnostic::new(TrailingWhitespace, range);
diagnostic.set_fix(Fix::applicable_edit(
Edit::range_deletion(range),
// Removing trailing whitespace is not safe inside multiline strings.
if indexer.multiline_ranges().contains_range(range) {
Applicability::Unsafe
} else {
Applicability::Safe
},
applicability,
));
return Some(diagnostic);
}
Expand Down
Expand Up @@ -11,7 +11,7 @@ W293.py:4:1: W293 [*] Blank line contains whitespace
|
= help: Remove whitespace from blank line

Safe fix
Unsafe fix
1 1 | # See: https://github.com/astral-sh/ruff/issues/9323
2 2 | class Chassis(RobotModuleTemplate):
3 3 | """底盘信息推送控制
Expand Down Expand Up @@ -48,6 +48,7 @@ W293.py:16:1: W293 [*] Blank line contains whitespace
15 | \
16 |
| ^^^^ W293
17 | '''blank line with whitespace
|
= help: Remove whitespace from blank line

Expand All @@ -59,5 +60,25 @@ W293.py:16:1: W293 [*] Blank line contains whitespace
15 |- \
16 |-
14 |+ "
17 15 | '''blank line with whitespace
18 16 |
19 17 | inside a multiline string'''

W293.py:18:1: W293 [*] Blank line contains whitespace
|
17 | '''blank line with whitespace
18 |
| ^ W293
19 | inside a multiline string'''
|
= help: Remove whitespace from blank line

Unsafe fix
15 15 | \
16 16 |
17 17 | '''blank line with whitespace
18 |-
18 |+
19 19 | inside a multiline string'''


0 comments on commit 1c8851e

Please sign in to comment.