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

Allow # fmt: skip with interspersed same-line comments #9395

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
@@ -0,0 +1,9 @@
# Supported
x = 1 # fmt: skip
x = 1 # fmt: skip # reason
x = 1 # reason # fmt: skip

# Unsupported
x = 1 # fmt: skip reason
x = 1 # fmt: skip - reason
x = 1 # fmt: skip; noqa
33 changes: 23 additions & 10 deletions crates/ruff_python_formatter/src/comments/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,24 +171,37 @@ impl SourceComment {

pub(crate) fn suppression_kind(&self, source: &str) -> Option<SuppressionKind> {
let text = self.slice.text(SourceCode::new(source));
let trimmed = text.strip_prefix('#').unwrap_or(text).trim_whitespace();

// Match against `# fmt: on`, `# fmt: off`, `# yapf: disable`, and `# yapf: enable`, which
// must be on their own lines.
let trimmed = text.strip_prefix('#').unwrap_or(text).trim_whitespace();
if let Some(command) = trimmed.strip_prefix("fmt:") {
match command.trim_whitespace_start() {
"off" => Some(SuppressionKind::Off),
"on" => Some(SuppressionKind::On),
"skip" => Some(SuppressionKind::Skip),
_ => None,
"off" => return Some(SuppressionKind::Off),
"on" => return Some(SuppressionKind::On),
"skip" => return Some(SuppressionKind::Skip),
_ => {}
}
} else if let Some(command) = trimmed.strip_prefix("yapf:") {
match command.trim_whitespace_start() {
"disable" => Some(SuppressionKind::Off),
"enable" => Some(SuppressionKind::On),
_ => None,
"disable" => return Some(SuppressionKind::Off),
"enable" => return Some(SuppressionKind::On),
_ => {}
}
} else {
None
}

// Search for `# fmt: skip` comments, which can be interspersed with other comments (e.g.,
// `# fmt: skip # noqa: E501`).
for segment in text.split('#') {
let trimmed = segment.trim_whitespace();
if let Some(command) = trimmed.strip_prefix("fmt:") {
if command.trim_whitespace_start() == "skip" {
return Some(SuppressionKind::Skip);
}
}
}

None
}

/// Returns true if this comment is a `fmt: off` or `yapf: disable` own line suppression comment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ skip_will_not_work2 = "a" + "b" # some text; fmt:skip happens to be part of i
--- Black
+++ Ruff
@@ -1,8 +1,8 @@
-foo = 123 # fmt: skip # noqa: E501 # pylint
+foo = 123 # fmt: skip # noqa: E501 # pylint
foo = 123 # fmt: skip # noqa: E501 # pylint
bar = (
- 123 ,
- ( 1 + 5 ) # pylint # fmt:skip
Expand All @@ -38,7 +37,7 @@ skip_will_not_work2 = "a" + "b" # some text; fmt:skip happens to be part of i
## Ruff Output

```python
foo = 123 # fmt: skip # noqa: E501 # pylint
foo = 123 # fmt: skip # noqa: E501 # pylint
bar = (
123,
(1 + 5), # pylint # fmt:skip
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/fmt_skip/reason.py
---
## Input
```python
# Supported
x = 1 # fmt: skip
x = 1 # fmt: skip # reason
x = 1 # reason # fmt: skip

# Unsupported
x = 1 # fmt: skip reason
x = 1 # fmt: skip - reason
x = 1 # fmt: skip; noqa
```

## Output
```python
# Supported
x = 1 # fmt: skip
x = 1 # fmt: skip # reason
x = 1 # reason # fmt: skip

# Unsupported
x = 1 # fmt: skip reason
x = 1 # fmt: skip - reason
x = 1 # fmt: skip; noqa
```



Loading