mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:39:12 +00:00
Allow # fmt: skip
with interspersed same-line comments (#9395)
## Summary This is similar to https://github.com/astral-sh/ruff/pull/8876, but more limited in scope: 1. It only applies to `# fmt: skip` (like Black). Like `# isort: on`, `# fmt: on` needs to be on its own line (still). 2. It only delimits on `#`, so you can do `# fmt: skip # noqa`, but not `# fmt: skip - some other content` or `# fmt: skip; noqa`. If we want to support the `;`-delimited version, we should revisit later, since we don't support that in the linter (so `# fmt: skip; noqa` wouldn't register a `noqa`). Closes https://github.com/astral-sh/ruff/issues/8892.
This commit is contained in:
parent
4b8b3a1ced
commit
60ba7a7c0d
4 changed files with 66 additions and 13 deletions
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue