@@ -2,17 +2,11 @@ use biome_analyze::{
22 Ast , Rule , RuleDiagnostic , RuleSource , context:: RuleContext , declare_lint_rule,
33} ;
44use biome_console:: markup;
5- use biome_css_syntax:: { AnyCssRule , CssLanguage } ;
5+ use biome_css_syntax:: { AnyCssRule , CssSyntaxNode } ;
66use biome_diagnostics:: Severity ;
7- use biome_rowan:: { AstNode , Direction , SyntaxToken , TextRange } ;
7+ use biome_rowan:: { AstNode , Direction , TextRange } ;
88use biome_rule_options:: no_irregular_whitespace:: NoIrregularWhitespaceOptions ;
99
10- const IRREGULAR_WHITESPACES : & [ char ; 22 ] = & [
11- '\u{c}' , '\u{b}' , '\u{85}' , '\u{feff}' , '\u{a0}' , '\u{1680}' , '\u{180e}' , '\u{2000}' ,
12- '\u{2001}' , '\u{2002}' , '\u{2003}' , '\u{2004}' , '\u{2005}' , '\u{2006}' , '\u{2007}' , '\u{2008}' ,
13- '\u{2009}' , '\u{200a}' , '\u{200b}' , '\u{202f}' , '\u{205f}' , '\u{3000}' ,
14- ] ;
15-
1610declare_lint_rule ! {
1711 /// Disallows the use of irregular whitespace characters.
1812 ///
@@ -53,8 +47,7 @@ impl Rule for NoIrregularWhitespace {
5347 type Options = NoIrregularWhitespaceOptions ;
5448
5549 fn run ( ctx : & RuleContext < Self > ) -> Self :: Signals {
56- let node = ctx. query ( ) ;
57- get_irregular_whitespace ( node) . into_boxed_slice ( )
50+ get_irregular_whitespace ( ctx. query ( ) . syntax ( ) ) . into_boxed_slice ( )
5851 }
5952
6053 fn diagnostic ( _: & RuleContext < Self > , range : & Self :: State ) -> Option < RuleDiagnostic > {
@@ -73,24 +66,31 @@ impl Rule for NoIrregularWhitespace {
7366 }
7467}
7568
76- fn get_irregular_whitespace ( node : & AnyCssRule ) -> Vec < TextRange > {
77- let syntax = node. syntax ( ) ;
78- let mut all_whitespaces_token: Vec < TextRange > = vec ! [ ] ;
79- let matches_irregular_whitespace = |token : & SyntaxToken < CssLanguage > | {
80- !token. has_leading_comments ( )
81- && !token. has_trailing_comments ( )
82- && token. text ( ) . chars ( ) . any ( |char| {
83- IRREGULAR_WHITESPACES
84- . iter ( )
85- . any ( |irregular_whitespace| & char == irregular_whitespace)
86- } )
87- } ;
69+ fn is_irregular_whitespace ( c : char ) -> bool {
70+ matches ! (
71+ c,
72+ '\u{000B}' | '\u{000C}' | '\u{0085}' | '\u{00A0}' | '\u{1680}' | '\u{180E}' | '\u{2000}'
73+ ..='\u{200B}' | '\u{202F}' | '\u{205F}' | '\u{3000}' | '\u{FEFF}'
74+ )
75+ }
8876
77+ fn get_irregular_whitespace ( syntax : & CssSyntaxNode ) -> Vec < TextRange > {
78+ if !syntax
79+ . text_with_trivia ( )
80+ . chars ( )
81+ . any ( is_irregular_whitespace)
82+ {
83+ return vec ! [ ] ;
84+ }
85+
86+ let mut results = vec ! [ ] ;
8987 for token in syntax. descendants_tokens ( Direction :: Next ) {
90- if matches_irregular_whitespace ( & token) {
91- all_whitespaces_token. push ( token. text_range ( ) ) ;
88+ if !token. has_leading_comments ( )
89+ && !token. has_trailing_comments ( )
90+ && token. text ( ) . chars ( ) . any ( is_irregular_whitespace)
91+ {
92+ results. push ( token. text_range ( ) ) ;
9293 }
9394 }
94-
95- all_whitespaces_token
95+ results
9696}
0 commit comments