@@ -10,14 +10,33 @@ use crate::globals::is_node_builtin_module;
10
10
#[ cfg_attr( feature = "schema" , derive( schemars:: JsonSchema ) ) ]
11
11
pub struct ImportGroups ( Box < [ ImportGroup ] > ) ;
12
12
impl ImportGroups {
13
+ /// Returns `true` if no explicit group is set.
14
+ pub fn is_empty ( & self ) -> bool {
15
+ self . 0 . is_empty ( )
16
+ }
17
+
13
18
/// Returns the index of the first group containing `candidate`.
14
19
/// If no group contains `candidate`, then the returned value corresponds to the index of the implicit group.
15
20
/// The index of the implicit group correspond to the number of groups.
16
- pub fn index ( & self , candidate : & ImportSourceCandidate ) -> usize {
21
+ pub fn index ( & self , candidate : & ImportSourceCandidate ) -> u16 {
17
22
self . 0
18
23
. iter ( )
19
24
. position ( |group| group. contains ( candidate) )
20
- . unwrap_or ( self . 0 . len ( ) )
25
+ . unwrap_or ( self . 0 . len ( ) ) as u16
26
+ }
27
+
28
+ /// Returns how many blank lines must separate `first_group` and `second_group`.
29
+ pub fn separated_by_blank_line ( & self , first_group : u16 , second_group : u16 ) -> bool {
30
+ self . 0
31
+ . get ( ( first_group as usize ) ..( second_group as usize ) )
32
+ . is_some_and ( |groups| {
33
+ groups. iter ( ) . any ( |group| {
34
+ matches ! (
35
+ group,
36
+ ImportGroup :: Predefined ( PredefinedImportGroup :: BlankLine )
37
+ )
38
+ } )
39
+ } )
21
40
}
22
41
}
23
42
@@ -62,6 +81,8 @@ impl Deserializable for ImportGroup {
62
81
#[ derive( Clone , Debug , Deserializable , Eq , PartialEq , serde:: Deserialize , serde:: Serialize ) ]
63
82
#[ cfg_attr( feature = "schema" , derive( schemars:: JsonSchema ) ) ]
64
83
pub enum PredefinedImportGroup {
84
+ #[ serde( rename = ":BLANK-LINE:" ) ]
85
+ BlankLine ,
65
86
#[ serde( rename = ":BUN:" ) ]
66
87
Bun ,
67
88
#[ serde( rename = ":NODE:" ) ]
@@ -71,6 +92,7 @@ impl PredefinedImportGroup {
71
92
fn contains ( & self , candidate : & ImportSourceCandidate ) -> bool {
72
93
let import_source = candidate. as_str ( ) ;
73
94
match self {
95
+ Self :: BlankLine => false ,
74
96
Self :: Bun => import_source == "bun" || import_source. starts_with ( "bun:" ) ,
75
97
Self :: Node => {
76
98
import_source. starts_with ( "node:" ) || is_node_builtin_module ( import_source)
0 commit comments