Skip to content

Commit

Permalink
improve handling of EOF in noqa
Browse files Browse the repository at this point in the history
  • Loading branch information
augustelalande committed Apr 24, 2024
1 parent e6f8e72 commit b1f451e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
45 changes: 29 additions & 16 deletions crates/ruff_linter/src/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ impl Ranged for NoqaDirectiveLine<'_> {
#[derive(Debug, Default)]
pub(crate) struct NoqaDirectives<'a> {
inner: Vec<NoqaDirectiveLine<'a>>,
last_directive_includes_eof: bool,
}

impl<'a> NoqaDirectives<'a> {
Expand Down Expand Up @@ -694,14 +695,17 @@ impl<'a> NoqaDirectives<'a> {
}
}

// Extend a mapping at the end of the file to also include the EOF token.
if let Some(last) = directives.last_mut() {
if last.range.end() == locator.contents().text_len() {
last.range = last.range.add_end(TextSize::from(1));
}
}
// Record whether last directive should include EOF token.
let last_directive_includes_eof = if let Some(last) = directives.last() {
last.range.end() == locator.contents().text_len()
} else {
false
};

Self { inner: directives }
Self {
inner: directives,
last_directive_includes_eof,
}
}

pub(crate) fn find_line_with_directive(&self, offset: TextSize) -> Option<&NoqaDirectiveLine> {
Expand All @@ -720,17 +724,26 @@ impl<'a> NoqaDirectives<'a> {
}

fn find_line_index(&self, offset: TextSize) -> Option<usize> {
self.inner
.binary_search_by(|directive| {
if directive.range.end() < offset {
std::cmp::Ordering::Less
} else if directive.range.contains(offset) {
std::cmp::Ordering::Equal
let index = self.inner.binary_search_by(|directive| {
if directive.range.end() < offset {
std::cmp::Ordering::Less
} else if directive.range.contains(offset) {
std::cmp::Ordering::Equal
} else {
std::cmp::Ordering::Greater
}
});

match index {
Ok(index) => Some(index),
Err(index) => {
if self.last_directive_includes_eof && index == self.inner.len() - 1 {
Some(index)
} else {
std::cmp::Ordering::Greater
None
}
})
.ok()
}
}
}

pub(crate) fn lines(&self) -> &[NoqaDirectiveLine] {
Expand Down
12 changes: 3 additions & 9 deletions crates/ruff_linter/src/rules/pygrep_hooks/rules/blanket_noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_trivia::Cursor;
use ruff_source_file::Locator;
use ruff_text_size::{Ranged, TextLen, TextRange};
use ruff_text_size::{Ranged, TextRange};

use crate::noqa::{Directive, NoqaDirectives};

Expand Down Expand Up @@ -88,14 +88,8 @@ pub(crate) fn blanket_noqa(
) {
for directive_line in noqa_directives.lines() {
if let Directive::All(all) = &directive_line.directive {
let line = if directive_line.end() > locator.contents().text_len() {
let range = TextRange::new(directive_line.start(), locator.contents().text_len());
locator.slice(range)
} else {
locator.slice(directive_line)
};
let offset = directive_line.start();
let noqa_end = all.end() - offset;
let line = locator.slice(directive_line);
let noqa_end = all.end() - directive_line.start();

// Skip the `# noqa`, plus any trailing whitespace.
let mut cursor = Cursor::new(&line[noqa_end.to_usize()..]);
Expand Down

0 comments on commit b1f451e

Please sign in to comment.