|
1 | | -use salsa; |
| 1 | +use ruff_python_parser::TokenKind; |
| 2 | +use ruff_source_file::LineRanges; |
| 3 | +use ruff_text_size::{Ranged, TextRange}; |
2 | 4 |
|
3 | | -use ruff_db::{files::File, parsed::comment_ranges, source::source_text}; |
4 | | -use ruff_index::{newtype_index, IndexVec}; |
| 5 | +use ruff_db::{files::File, parsed::parsed_module, source::source_text}; |
5 | 6 |
|
6 | 7 | use crate::{lint::LintId, Db}; |
7 | 8 |
|
8 | 9 | #[salsa::tracked(return_ref)] |
9 | | -pub(crate) fn suppressions(db: &dyn Db, file: File) -> IndexVec<SuppressionIndex, Suppression> { |
10 | | - let comments = comment_ranges(db.upcast(), file); |
| 10 | +pub(crate) fn suppressions(db: &dyn Db, file: File) -> Suppressions { |
11 | 11 | let source = source_text(db.upcast(), file); |
| 12 | + let parsed = parsed_module(db.upcast(), file); |
12 | 13 |
|
13 | | - let mut suppressions = IndexVec::default(); |
14 | | - |
15 | | - for range in comments { |
16 | | - let text = &source[range]; |
17 | | - |
18 | | - if text.starts_with("# type: ignore") { |
19 | | - suppressions.push(Suppression { |
20 | | - target: None, |
21 | | - kind: SuppressionKind::TypeIgnore, |
22 | | - }); |
23 | | - } else if text.starts_with("# knot: ignore") { |
24 | | - suppressions.push(Suppression { |
25 | | - target: None, |
26 | | - kind: SuppressionKind::KnotIgnore, |
27 | | - }); |
| 14 | + // TODO: Support `type: ignore` comments at the |
| 15 | + // [start of the file](https://typing.readthedocs.io/en/latest/spec/directives.html#type-ignore-comments). |
| 16 | + let mut suppressions = Vec::default(); |
| 17 | + let mut line_start = source.bom_start_offset(); |
| 18 | + |
| 19 | + for token in parsed.tokens() { |
| 20 | + match token.kind() { |
| 21 | + TokenKind::Comment => { |
| 22 | + let text = &source[token.range()]; |
| 23 | + |
| 24 | + let suppressed_range = TextRange::new(line_start, token.end()); |
| 25 | + |
| 26 | + if text.strip_prefix("# type: ignore").is_some_and(|suffix| { |
| 27 | + suffix.is_empty() |
| 28 | + || suffix.starts_with(char::is_whitespace) |
| 29 | + || suffix.starts_with('[') |
| 30 | + }) { |
| 31 | + suppressions.push(Suppression { suppressed_range }); |
| 32 | + } |
| 33 | + } |
| 34 | + TokenKind::Newline | TokenKind::NonLogicalNewline => { |
| 35 | + line_start = token.end(); |
| 36 | + } |
| 37 | + _ => {} |
28 | 38 | } |
29 | 39 | } |
30 | 40 |
|
31 | | - suppressions |
| 41 | + Suppressions { suppressions } |
32 | 42 | } |
33 | 43 |
|
34 | | -#[newtype_index] |
35 | | -pub(crate) struct SuppressionIndex; |
36 | | - |
37 | | -#[derive(Clone, Debug, Eq, PartialEq, Hash)] |
38 | | -pub(crate) struct Suppression { |
39 | | - target: Option<LintId>, |
40 | | - kind: SuppressionKind, |
| 44 | +/// The suppression comments of a single file. |
| 45 | +#[derive(Clone, Debug, Eq, PartialEq)] |
| 46 | +pub(crate) struct Suppressions { |
| 47 | + /// The suppressions sorted by the suppressed range. |
| 48 | + suppressions: Vec<Suppression>, |
41 | 49 | } |
42 | 50 |
|
43 | | -#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] |
44 | | -pub(crate) enum SuppressionKind { |
45 | | - /// A `type: ignore` comment |
46 | | - TypeIgnore, |
| 51 | +impl Suppressions { |
| 52 | + /// Finds a suppression for the specified lint. |
| 53 | + /// |
| 54 | + /// Returns the first matching suppression if more than one suppression apply to `range` and `id`. |
| 55 | + /// |
| 56 | + /// Returns `None` if the lint isn't suppressed. |
| 57 | + pub(crate) fn find_suppression(&self, range: TextRange, _id: LintId) -> Option<&Suppression> { |
| 58 | + // TODO(micha): |
| 59 | + // * Test if the suppression suppresses the passed lint |
| 60 | + self.for_range(range).next() |
| 61 | + } |
47 | 62 |
|
48 | | - /// A `knot: ignore` comment |
49 | | - KnotIgnore, |
| 63 | + /// Returns all suppression comments that apply for `range`. |
| 64 | + /// |
| 65 | + /// A suppression applies for the given range if it contains the range's |
| 66 | + /// start or end offset. This means the suppression is on the same line |
| 67 | + /// as the diagnostic's start or end. |
| 68 | + fn for_range(&self, range: TextRange) -> impl Iterator<Item = &Suppression> + '_ { |
| 69 | + // First find the index of the suppression comment that ends right before the range |
| 70 | + // starts. This allows us to skip suppressions that are not relevant for the range. |
| 71 | + let end_offset = self |
| 72 | + .suppressions |
| 73 | + .binary_search_by_key(&range.start(), |suppression| { |
| 74 | + suppression.suppressed_range.end() |
| 75 | + }) |
| 76 | + .unwrap_or_else(|index| index); |
| 77 | + |
| 78 | + // From here, search the remaining suppression comments for one that |
| 79 | + // contains the range's start or end offset. Stop the search |
| 80 | + // as soon as the suppression's range and the range no longer overlap. |
| 81 | + self.suppressions[end_offset..] |
| 82 | + .iter() |
| 83 | + // Stop searching if the suppression starts after the range we're looking for. |
| 84 | + .take_while(move |suppression| range.end() >= suppression.suppressed_range.start()) |
| 85 | + .filter(move |suppression| { |
| 86 | + // Don't use intersect to avoid that suppressions on inner-expression |
| 87 | + // ignore errors for outer expressions |
| 88 | + suppression.suppressed_range.contains(range.start()) |
| 89 | + || suppression.suppressed_range.contains(range.end()) |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/// A `type: ignore` or `knot: ignore` suppression comment. |
| 95 | +#[derive(Clone, Debug, Eq, PartialEq, Hash)] |
| 96 | +pub(crate) struct Suppression { |
| 97 | + /// The range for which this suppression applies. |
| 98 | + /// Most of the time, this is the range of the comment's line. |
| 99 | + /// However, there are few cases where the range gets expanded to |
| 100 | + /// cover multiple lines: |
| 101 | + /// * multiline strings: `expr + """multiline\nstring""" # type: ignore` |
| 102 | + /// * line continuations: `expr \ + "test" # type: ignore` |
| 103 | + suppressed_range: TextRange, |
50 | 104 | } |
0 commit comments