1
1
use solar_ast:: { Item , SourceUnit , visit:: Visit } ;
2
+ use solar_interface:: SourceMap ;
2
3
use solar_parse:: ast:: Span ;
3
4
use std:: { collections:: HashMap , fmt, marker:: PhantomData , ops:: ControlFlow } ;
4
5
@@ -110,10 +111,10 @@ impl InlineConfig {
110
111
pub fn new < ' ast > (
111
112
items : impl IntoIterator < Item = ( Span , InlineConfigItem ) > ,
112
113
ast : & ' ast SourceUnit < ' ast > ,
113
- src : & str ,
114
+ source_map : & SourceMap ,
114
115
) -> Self {
115
116
let mut disabled_ranges: HashMap < String , Vec < DisabledRange > > = HashMap :: new ( ) ;
116
- let mut disabled_blocks: HashMap < String , ( usize , usize ) > = HashMap :: new ( ) ;
117
+ let mut disabled_blocks: HashMap < String , ( usize , usize , usize ) > = HashMap :: new ( ) ;
117
118
118
119
let mut prev_sp = Span :: DUMMY ;
119
120
for ( sp, item) in items {
@@ -122,11 +123,11 @@ impl InlineConfig {
122
123
prev_sp = sp;
123
124
}
124
125
126
+ let Ok ( ( file, comment_range) ) = source_map. span_to_source ( sp) else { continue } ;
127
+ let src = file. src . as_str ( ) ;
125
128
match item {
126
129
InlineConfigItem :: DisableNextItem ( lints) => {
127
- let comment_end = sp. hi ( ) . to_usize ( ) ;
128
-
129
- if let Some ( next_item) = NextItemFinder :: new ( comment_end) . find ( ast) {
130
+ if let Some ( next_item) = NextItemFinder :: new ( sp. hi ( ) . to_usize ( ) ) . find ( ast) {
130
131
for lint in lints {
131
132
disabled_ranges. entry ( lint) . or_default ( ) . push ( DisabledRange {
132
133
start : next_item. lo ( ) . to_usize ( ) ,
@@ -137,55 +138,50 @@ impl InlineConfig {
137
138
} ;
138
139
}
139
140
InlineConfigItem :: DisableLine ( lints) => {
140
- let mut prev_newline = src[ ..sp. lo ( ) . to_usize ( ) ]
141
- . char_indices ( )
142
- . rev ( )
143
- . skip_while ( |( _, ch) | * ch != '\n' ) ;
144
- let start = prev_newline. next ( ) . map ( |( idx, _) | idx) . unwrap_or_default ( ) ;
141
+ let start = src[ ..comment_range. start ] . rfind ( '\n' ) . map_or ( 0 , |i| i) ;
142
+ let end = src[ comment_range. end ..]
143
+ . find ( '\n' )
144
+ . map_or ( src. len ( ) , |i| comment_range. end + i) ;
145
145
146
- let end_offset = sp. hi ( ) . to_usize ( ) ;
147
- let mut next_newline =
148
- src[ end_offset..] . char_indices ( ) . skip_while ( |( _, ch) | * ch != '\n' ) ;
149
- let end =
150
- end_offset + next_newline. next ( ) . map ( |( idx, _) | idx) . unwrap_or_default ( ) ;
151
146
for lint in lints {
152
147
disabled_ranges. entry ( lint) . or_default ( ) . push ( DisabledRange {
153
- start,
154
- end,
148
+ start : start + file . start_pos . to_usize ( ) ,
149
+ end : end + file . start_pos . to_usize ( ) ,
155
150
loose : false ,
156
151
} )
157
152
}
158
153
}
159
154
InlineConfigItem :: DisableNextLine ( lints) => {
160
- let offset = sp. hi ( ) . to_usize ( ) ;
161
- let mut char_indices =
162
- src[ offset..] . char_indices ( ) . skip_while ( |( _, ch) | * ch != '\n' ) . skip ( 1 ) ;
163
- if let Some ( ( mut start, _) ) = char_indices. next ( ) {
164
- start += offset;
165
- let end = char_indices
166
- . find ( |( _, ch) | * ch == '\n' )
167
- . map ( |( idx, _) | offset + idx + 1 )
168
- . unwrap_or ( src. len ( ) ) ;
169
- for lint in lints {
170
- disabled_ranges. entry ( lint) . or_default ( ) . push ( DisabledRange {
171
- start,
172
- end,
173
- loose : false ,
174
- } )
155
+ if let Some ( offset) = src[ comment_range. end ..] . find ( '\n' ) {
156
+ let start = comment_range. end + offset + 1 ;
157
+ if start < src. len ( ) {
158
+ let end = src[ start..] . find ( '\n' ) . map_or ( src. len ( ) , |i| start + i) ;
159
+ for lint in lints {
160
+ disabled_ranges. entry ( lint) . or_default ( ) . push ( DisabledRange {
161
+ start : start + file. start_pos . to_usize ( ) ,
162
+ end : end + file. start_pos . to_usize ( ) ,
163
+ loose : false ,
164
+ } )
165
+ }
175
166
}
176
167
}
177
168
}
178
169
InlineConfigItem :: DisableStart ( lints) => {
179
170
for lint in lints {
180
171
disabled_blocks
181
172
. entry ( lint)
182
- . and_modify ( |( _, depth) | * depth += 1 )
183
- . or_insert ( ( sp. hi ( ) . to_usize ( ) , 1 ) ) ;
173
+ . and_modify ( |( _, depth, _) | * depth += 1 )
174
+ . or_insert ( (
175
+ sp. hi ( ) . to_usize ( ) ,
176
+ 1 ,
177
+ // Use file end as fallback for unclosed blocks
178
+ file. start_pos . to_usize ( ) + src. len ( ) ,
179
+ ) ) ;
184
180
}
185
181
}
186
182
InlineConfigItem :: DisableEnd ( lints) => {
187
183
for lint in lints {
188
- if let Some ( ( start, depth) ) = disabled_blocks. get_mut ( & lint) {
184
+ if let Some ( ( start, depth, _ ) ) = disabled_blocks. get_mut ( & lint) {
189
185
* depth = depth. saturating_sub ( 1 ) ;
190
186
191
187
if * depth == 0 {
@@ -204,10 +200,10 @@ impl InlineConfig {
204
200
}
205
201
}
206
202
207
- for ( lint, ( start, _) ) in disabled_blocks {
203
+ for ( lint, ( start, _, file_end ) ) in disabled_blocks {
208
204
disabled_ranges. entry ( lint) . or_default ( ) . push ( DisabledRange {
209
205
start,
210
- end : src . len ( ) ,
206
+ end : file_end ,
211
207
loose : false ,
212
208
} ) ;
213
209
}
0 commit comments