From 464bb8264f55a44204a50403ef4efc8c30428781 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 11 Aug 2024 19:04:44 -0400 Subject: [PATCH] Avoid parsing joint rule codes as distinct codes in # noqa --- crates/ruff_linter/src/noqa.rs | 18 ++++++++++++--- ...linter__noqa__tests__noqa_empty_comma.snap | 23 +++++++++++++++++++ ...ter__noqa__tests__noqa_squashed_codes.snap | 19 +++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__noqa_empty_comma.snap create mode 100644 crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__noqa_squashed_codes.snap diff --git a/crates/ruff_linter/src/noqa.rs b/crates/ruff_linter/src/noqa.rs index 948cd534f95da..643df2ba983d0 100644 --- a/crates/ruff_linter/src/noqa.rs +++ b/crates/ruff_linter/src/noqa.rs @@ -183,7 +183,7 @@ impl<'a> Directive<'a> { // Extract, e.g., the `401` in `F401`. let suffix = line[prefix..] .chars() - .take_while(char::is_ascii_digit) + .take_while(char::is_ascii_alphanumeric) .count(); if prefix > 0 && suffix > 0 { Some(&line[..prefix + suffix]) @@ -549,7 +549,7 @@ impl<'a> ParsedFileExemption<'a> { // Extract, e.g., the `401` in `F401`. let suffix = line[prefix..] .chars() - .take_while(char::is_ascii_digit) + .take_while(char::is_ascii_alphanumeric) .count(); if prefix > 0 && suffix > 0 { Some(&line[..prefix + suffix]) @@ -895,7 +895,7 @@ pub(crate) struct NoqaDirectiveLine<'a> { pub(crate) directive: Directive<'a>, /// The codes that are ignored by the directive. pub(crate) matches: Vec, - // Whether the directive applies to range.end + /// Whether the directive applies to `range.end`. pub(crate) includes_end: bool, } @@ -1191,6 +1191,18 @@ mod tests { assert_debug_snapshot!(Directive::try_extract(source, TextSize::default())); } + #[test] + fn noqa_squashed_codes() { + let source = "# noqa: F401F841"; + assert_debug_snapshot!(Directive::try_extract(source, TextSize::default())); + } + + #[test] + fn noqa_empty_comma() { + let source = "# noqa: F401,,F841"; + assert_debug_snapshot!(Directive::try_extract(source, TextSize::default())); + } + #[test] fn noqa_invalid_suffix() { let source = "# noqa[F401]"; diff --git a/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__noqa_empty_comma.snap b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__noqa_empty_comma.snap new file mode 100644 index 0000000000000..7a83ed9db2271 --- /dev/null +++ b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__noqa_empty_comma.snap @@ -0,0 +1,23 @@ +--- +source: crates/ruff_linter/src/noqa.rs +expression: "Directive::try_extract(source, TextSize::default())" +--- +Ok( + Some( + Codes( + Codes { + range: 0..18, + codes: [ + Code { + code: "F401", + range: 8..12, + }, + Code { + code: "F841", + range: 14..18, + }, + ], + }, + ), + ), +) diff --git a/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__noqa_squashed_codes.snap b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__noqa_squashed_codes.snap new file mode 100644 index 0000000000000..8a6adc1cc7a11 --- /dev/null +++ b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__noqa_squashed_codes.snap @@ -0,0 +1,19 @@ +--- +source: crates/ruff_linter/src/noqa.rs +expression: "Directive::try_extract(source, TextSize::default())" +--- +Ok( + Some( + Codes( + Codes { + range: 0..16, + codes: [ + Code { + code: "F401F841", + range: 8..16, + }, + ], + }, + ), + ), +)