@@ -57,8 +57,8 @@ pub use crate::syntax::{Ast, SyntaxVisitor};
5757pub use crate :: visitor:: { NodeVisitor , Visitor , VisitorContext , VisitorFinishContext } ;
5858use biome_diagnostics:: { Diagnostic , DiagnosticExt , category} ;
5959use biome_rowan:: {
60- AstNode , BatchMutation , Direction , Language , SyntaxToken , TextRange , TextSize , TokenAtOffset ,
61- TriviaPieceKind ,
60+ AstNode , BatchMutation , Direction , Language , SyntaxKind as _ , SyntaxToken , TextRange , TextSize ,
61+ TokenAtOffset , TriviaPieceKind ,
6262} ;
6363use biome_suppression:: { Suppression , SuppressionKind } ;
6464pub use suppression_action:: { ApplySuppression , SuppressionAction } ;
@@ -172,6 +172,7 @@ where
172172 options : ctx. options ,
173173 suppressions : & mut suppressions,
174174 categories,
175+ deny_top_level_suppressions : false ,
175176 } ;
176177
177178 // The first phase being run will inspect the tokens and parse the
@@ -330,6 +331,8 @@ struct PhaseRunner<'analyzer, 'phase, L: Language, Matcher, Break, Diag> {
330331 suppressions : & ' phase mut Suppressions < ' analyzer > ,
331332 /// The current categories
332333 categories : RuleCategories ,
334+ /// Whether we have already encountered a token that can't precede top level suppressions
335+ deny_top_level_suppressions : bool ,
333336}
334337
335338impl < L , Matcher , Break , Diag > PhaseRunner < ' _ , ' _ , L , Matcher , Break , Diag >
@@ -409,7 +412,7 @@ where
409412 /// whose position is less than the end of the token within the file
410413 fn handle_token ( & mut self , token : SyntaxToken < L > ) -> ControlFlow < Break > {
411414 // Process the content of the token for comments and newline
412- for ( index , piece) in token. leading_trivia ( ) . pieces ( ) . enumerate ( ) {
415+ for piece in token. leading_trivia ( ) . pieces ( ) {
413416 if matches ! (
414417 piece. kind( ) ,
415418 TriviaPieceKind :: Newline
@@ -420,13 +423,16 @@ where
420423 }
421424
422425 if let Some ( comment) = piece. as_comments ( ) {
423- self . handle_comment ( & token , true , index , comment. text ( ) , piece. text_range ( ) ) ?;
426+ self . handle_comment ( comment. text ( ) , piece. text_range ( ) ) ?;
424427 }
425428 }
426429
427430 self . bump_line_index ( token. text_trimmed ( ) , token. text_trimmed_range ( ) ) ;
431+ if !self . deny_top_level_suppressions {
432+ self . deny_top_level_suppressions = !token. kind ( ) . is_allowed_before_suppressions ( ) ;
433+ }
428434
429- for ( index , piece) in token. trailing_trivia ( ) . pieces ( ) . enumerate ( ) {
435+ for piece in token. trailing_trivia ( ) . pieces ( ) {
430436 if matches ! (
431437 piece. kind( ) ,
432438 TriviaPieceKind :: Newline
@@ -437,7 +443,7 @@ where
437443 }
438444
439445 if let Some ( comment) = piece. as_comments ( ) {
440- self . handle_comment ( & token , false , index , comment. text ( ) , piece. text_range ( ) ) ?;
446+ self . handle_comment ( comment. text ( ) , piece. text_range ( ) ) ?;
441447 }
442448 }
443449
@@ -522,14 +528,7 @@ where
522528
523529 /// Parse the text content of a comment trivia piece for suppression
524530 /// comments, and create line suppression entries accordingly
525- fn handle_comment (
526- & mut self ,
527- token : & SyntaxToken < L > ,
528- _is_leading : bool ,
529- _index : usize ,
530- text : & str ,
531- range : TextRange ,
532- ) -> ControlFlow < Break > {
531+ fn handle_comment ( & mut self , text : & str , range : TextRange ) -> ControlFlow < Break > {
533532 for result in ( self . parse_suppression_comment ) ( text, range) {
534533 let suppression: AnalyzerSuppression = match result {
535534 Ok ( kind) => kind,
@@ -570,10 +569,11 @@ where
570569 continue ;
571570 }
572571
573- if let Err ( diagnostic) =
574- self . suppressions
575- . push_suppression ( & suppression, range, token. text_range ( ) )
576- {
572+ if let Err ( diagnostic) = self . suppressions . push_suppression (
573+ & suppression,
574+ range,
575+ !self . deny_top_level_suppressions ,
576+ ) {
577577 let signal = DiagnosticSignal :: new ( || diagnostic. clone ( ) ) ;
578578 ( self . emit_signal ) ( & signal) ?;
579579 continue ;
0 commit comments