Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(analyzer): suppression comment fails with inner comments in functions #4714

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
biome_analyze: patch
---

# fix suppression comment fails with inner comments in functions
27 changes: 15 additions & 12 deletions crates/biome_analyze/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use biome_console::markup;
use biome_parser::AnyParse;
use std::cmp::Ordering;
use std::collections::{BTreeMap, BinaryHeap};
use std::fmt::{Debug, Display, Formatter};
use std::ops;
Expand Down Expand Up @@ -403,14 +402,18 @@ where
/// Flush all pending query signals in the queue. If `cutoff` is specified,
/// signals that start after this position in the file will be skipped
fn flush_matches(&mut self, cutoff: Option<TextSize>) -> ControlFlow<Break> {
// println!("cutoff is: {:?}", cutoff);
while let Some(entry) = self.signal_queue.peek() {
println!("entry is {:?}, cutoff is {:?}", entry.text_range, cutoff);
let start = entry.text_range.start();
if let Some(cutoff) = cutoff {
if start >= cutoff {
break;
}
}

println!("line_suppressions: {:?}", self.suppressions.line_suppressions);

if self
.suppressions
.top_level_suppression
Expand Down Expand Up @@ -443,29 +446,29 @@ where
suppression.line_index == *self.line_index
&& suppression.text_range.start() <= start
});

println!("suppression before binary_search: {:?}", suppression);

let suppression = match suppression {
Some(suppression) => Some(suppression),
None => {
let index =
self.suppressions
.line_suppressions
.binary_search_by(|suppression| {
if suppression.text_range.end() < entry.text_range.start() {
Ordering::Less
} else if entry.text_range.end() < suppression.text_range.start() {
Ordering::Greater
} else {
Ordering::Equal
}
.partition_point(|suppression| {
suppression.text_range.end() < entry.text_range.start()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on the Rust documentation for partition_point, is my understanding correct that the order suppression.text_range.end() < entry.text_range.start() follows Rust conventions for finding the boundary point of non-overlapping ranges?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, i think this will be the right way here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

});

index
.ok()
.map(|index| &mut self.suppressions.line_suppressions[index])
if index >= self.suppressions.line_suppressions.len() {
None
} else {
Some(&mut self.suppressions.line_suppressions[index])
}
}
};

println!("final suppression: {:?}", suppression);

let suppression = suppression.filter(|suppression| {
if suppression.suppress_all {
return true;
Expand Down
4 changes: 4 additions & 0 deletions crates/biome_analyze/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,10 @@ impl<L: Language + Default> RegistryRule<L> {
params.options,
));

println!("signal_queue text_range is 222: {:?}", text_range);

// println!("signal: {:?}", params.root);

params.signal_queue.push(SignalEntry {
signal,
rule: RuleKey::rule::<R>(),
Expand Down
42 changes: 42 additions & 0 deletions crates/biome_js_analyze/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,48 @@ let d;
);
}

#[test]
fn suppression_range_should_report_when_contains_inner_comment() {
const SOURCE: &str = "// biome-ignore lint/complexity/useArrowFunction: single rule
const foo0 = function (bar: string) {
// biome-ignore lint/style/noParameterAssign: single rule
bar = 'baz';
};";

let parsed = parse(SOURCE, JsFileSource::js_module(), JsParserOptions::default());

let enabled_rules = vec![
RuleFilter::Rule("complexity", "useArrowFunction"),
RuleFilter::Rule("style", "noParameterAssign"),
];

let filter = AnalysisFilter {
categories: RuleCategoriesBuilder::default().with_lint().build(),
enabled_rules: Some(enabled_rules.as_slice()),
..AnalysisFilter::default()
};
let options = AnalyzerOptions::default();
analyze(
&parsed.tree(),
filter,
&options,
Vec::new(),
JsFileSource::js_module(),
Default::default(),
|signal| {
if let Some(diag) = signal.diagnostic() {
let error = diag
.with_file_path("dummyFile")
.with_file_source_code(SOURCE);
let text = print_diagnostic_to_string(&error);
eprintln!("{text}");
}

ControlFlow::<Never>::Continue(())
},
);
}

#[test]
fn unused_range_suppression() {
const SOURCE: &str = "
Expand Down
Loading