From f53b65a2034b036e7ee5877cc7afd70ca7a17673 Mon Sep 17 00:00:00 2001 From: zoomdong <1344492820@qq.com> Date: Fri, 17 Jan 2025 10:53:54 +0800 Subject: [PATCH] fix(analyzer): suppression comment fails with inner comments in functions --- ..._fails_with_inner_comments_in_functions.md | 5 +++ crates/biome_analyze/src/lib.rs | 27 ++++++------ crates/biome_analyze/src/registry.rs | 4 ++ crates/biome_js_analyze/src/lib.rs | 42 +++++++++++++++++++ 4 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 .changeset/fixanalyzer_suppression_comment_fails_with_inner_comments_in_functions.md diff --git a/.changeset/fixanalyzer_suppression_comment_fails_with_inner_comments_in_functions.md b/.changeset/fixanalyzer_suppression_comment_fails_with_inner_comments_in_functions.md new file mode 100644 index 000000000000..ef0c048213ca --- /dev/null +++ b/.changeset/fixanalyzer_suppression_comment_fails_with_inner_comments_in_functions.md @@ -0,0 +1,5 @@ +--- +biome_analyze: patch +--- + +# fix suppression comment fails with inner comments in functions diff --git a/crates/biome_analyze/src/lib.rs b/crates/biome_analyze/src/lib.rs index 7df64cc5a2b1..ab1935384eb0 100644 --- a/crates/biome_analyze/src/lib.rs +++ b/crates/biome_analyze/src/lib.rs @@ -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; @@ -403,7 +402,9 @@ 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) -> ControlFlow { + // 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 { @@ -411,6 +412,8 @@ where } } + println!("line_suppressions: {:?}", self.suppressions.line_suppressions); + if self .suppressions .top_level_suppression @@ -443,6 +446,8 @@ 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), @@ -450,22 +455,20 @@ where 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() }); - 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; diff --git a/crates/biome_analyze/src/registry.rs b/crates/biome_analyze/src/registry.rs index 6a37cec0d944..6c6595172818 100644 --- a/crates/biome_analyze/src/registry.rs +++ b/crates/biome_analyze/src/registry.rs @@ -431,6 +431,10 @@ impl RegistryRule { params.options, )); + println!("signal_queue text_range is 222: {:?}", text_range); + + // println!("signal: {:?}", params.root); + params.signal_queue.push(SignalEntry { signal, rule: RuleKey::rule::(), diff --git a/crates/biome_js_analyze/src/lib.rs b/crates/biome_js_analyze/src/lib.rs index b342e9c0fd75..9aab399dcef0 100644 --- a/crates/biome_js_analyze/src/lib.rs +++ b/crates/biome_js_analyze/src/lib.rs @@ -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::::Continue(()) + }, + ); + } + #[test] fn unused_range_suppression() { const SOURCE: &str = "