From a8d080c8252a156955e1d0c69b450bfca71c2c9c Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 18 May 2023 09:05:27 -0400 Subject: [PATCH] Extend multi-line noqa directives to start-of-line (#4490) --- .../resources/test/fixtures/ruff/RUF100_0.py | 9 +++++++++ crates/ruff/src/directives.rs | 19 ++++++++++++------- .../ruff__rules__ruff__tests__ruf100_0.snap | 3 +++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/crates/ruff/resources/test/fixtures/ruff/RUF100_0.py b/crates/ruff/resources/test/fixtures/ruff/RUF100_0.py index f89729938b..5e51807ef9 100644 --- a/crates/ruff/resources/test/fixtures/ruff/RUF100_0.py +++ b/crates/ruff/resources/test/fixtures/ruff/RUF100_0.py @@ -88,3 +88,12 @@ import sys # noqa: F401, RUF100 print(sys.path) "shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]" # noqa: F401 + + +def f(): + # Ensure that the `noqa` applies to both the overlong line _and_ the unused + # variable. + a = """Lorem ipsum dolor sit amet. + + Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. + """ # noqa diff --git a/crates/ruff/src/directives.rs b/crates/ruff/src/directives.rs index 77e94177b4..5ebf5c5a56 100644 --- a/crates/ruff/src/directives.rs +++ b/crates/ruff/src/directives.rs @@ -1,12 +1,13 @@ //! Extract `# noqa` and `# isort: skip` directives from tokenized source. -use crate::noqa::NoqaMapping; use bitflags::bitflags; -use ruff_python_ast::source_code::{Indexer, Locator}; use ruff_text_size::{TextLen, TextRange, TextSize}; use rustpython_parser::lexer::LexResult; use rustpython_parser::Tok; +use ruff_python_ast::source_code::{Indexer, Locator}; + +use crate::noqa::NoqaMapping; use crate::settings::Settings; bitflags! { @@ -102,7 +103,10 @@ pub fn extract_noqa_line_for( .. } => { if locator.contains_line_break(*range) { - string_mappings.push(*range); + string_mappings.push(TextRange::new( + locator.line_start(range.start()), + range.end(), + )); } } @@ -219,11 +223,12 @@ pub fn extract_isort_directives(lxr: &[LexResult], locator: &Locator) -> IsortDi #[cfg(test)] mod tests { - use ruff_python_ast::source_code::{Indexer, Locator}; use ruff_text_size::{TextLen, TextRange, TextSize}; use rustpython_parser::lexer::LexResult; use rustpython_parser::{lexer, Mode}; + use ruff_python_ast::source_code::{Indexer, Locator}; + use crate::directives::{extract_isort_directives, extract_noqa_line_for}; use crate::noqa::NoqaMapping; @@ -271,7 +276,7 @@ y = 2 z = x + 1"; assert_eq!( noqa_mappings(contents), - NoqaMapping::from_iter([TextRange::new(TextSize::from(4), TextSize::from(22)),]) + NoqaMapping::from_iter([TextRange::new(TextSize::from(0), TextSize::from(22)),]) ); let contents = "x = 1 @@ -282,7 +287,7 @@ ghi z = 2"; assert_eq!( noqa_mappings(contents), - NoqaMapping::from_iter([TextRange::new(TextSize::from(10), TextSize::from(28))]) + NoqaMapping::from_iter([TextRange::new(TextSize::from(6), TextSize::from(28))]) ); let contents = "x = 1 @@ -292,7 +297,7 @@ ghi '''"; assert_eq!( noqa_mappings(contents), - NoqaMapping::from_iter([TextRange::new(TextSize::from(10), TextSize::from(28))]) + NoqaMapping::from_iter([TextRange::new(TextSize::from(6), TextSize::from(28))]) ); let contents = r#"x = \ diff --git a/crates/ruff/src/rules/ruff/snapshots/ruff__rules__ruff__tests__ruf100_0.snap b/crates/ruff/src/rules/ruff/snapshots/ruff__rules__ruff__tests__ruf100_0.snap index 7c3a3bf874..407e99d05a 100644 --- a/crates/ruff/src/rules/ruff/snapshots/ruff__rules__ruff__tests__ruf100_0.snap +++ b/crates/ruff/src/rules/ruff/snapshots/ruff__rules__ruff__tests__ruf100_0.snap @@ -260,5 +260,8 @@ RUF100_0.py:90:92: RUF100 [*] Unused `noqa` directive (unused: `F401`) 89 89 | 90 |-"shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]" # noqa: F401 90 |+"shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]" +91 91 | +92 92 | +93 93 | def f():