@@ -164,10 +164,19 @@ impl<'a> DisableDirectivesBuilder<'a> {
164
164
// `eslint-disable-next-line`
165
165
else if let Some ( text) = text. strip_prefix ( "-next-line" ) {
166
166
// Get the span up to the next new line
167
- let stop = source_text[ comment_span. end as usize ..]
168
- . lines ( )
169
- . take ( 2 )
170
- . fold ( comment_span. end , |acc, line| acc + line. len ( ) as u32 ) ;
167
+ let mut stop = comment_span. end ;
168
+ let mut lines_after_comment_end =
169
+ source_text[ comment_span. end as usize ..] . split_inclusive ( '\n' ) ;
170
+
171
+ if let Some ( rest_of_line) = lines_after_comment_end. next ( ) {
172
+ stop += rest_of_line. len ( ) as u32 ;
173
+ }
174
+
175
+ if let Some ( next_line) = lines_after_comment_end. next ( ) {
176
+ let next_line_trimmed = next_line. trim_end_matches ( [ '\n' , '\r' ] ) ;
177
+ stop += next_line_trimmed. len ( ) as u32 ;
178
+ }
179
+
171
180
if text. trim ( ) . is_empty ( ) {
172
181
self . add_interval (
173
182
comment_span. end ,
@@ -695,7 +704,7 @@ semi*/
695
704
}
696
705
697
706
#[ cfg( test) ]
698
- mod test_unused_directives {
707
+ mod tests {
699
708
use oxc_allocator:: Allocator ;
700
709
use oxc_ast:: Comment ;
701
710
use oxc_parser:: Parser ;
@@ -726,6 +735,17 @@ mod test_unused_directives {
726
735
}
727
736
}
728
737
738
+ fn test_directive_span ( source_text : & str , expected_start : u32 , expected_stop : u32 ) {
739
+ let allocator = Allocator :: default ( ) ;
740
+ let semantic = process_source ( & allocator, source_text) ;
741
+ let directives =
742
+ DisableDirectivesBuilder :: new ( ) . build ( semantic. source_text ( ) , semantic. comments ( ) ) ;
743
+ let interval = & directives. intervals . intervals [ 0 ] ;
744
+
745
+ assert_eq ! ( interval. start, expected_start) ;
746
+ assert_eq ! ( interval. stop, expected_stop) ;
747
+ }
748
+
729
749
#[ test]
730
750
fn unused_enable_all ( ) {
731
751
test_directives (
@@ -890,4 +910,17 @@ mod test_unused_directives {
890
910
} ,
891
911
) ;
892
912
}
913
+
914
+ #[ test]
915
+ fn next_line_span_of_line_comment ( ) {
916
+ test_directive_span ( "// eslint-disable-next-line max-params" , 38 , 38 ) ;
917
+ test_directive_span ( "// eslint-disable-next-line max-params\n " , 38 , 39 ) ;
918
+ test_directive_span ( "// eslint-disable-next-line max-params\r \n " , 38 , 40 ) ;
919
+ test_directive_span ( "// eslint-disable-next-line max-params \n " , 42 , 43 ) ;
920
+ test_directive_span ( "// eslint-disable-next-line max-params \r \n " , 42 , 44 ) ;
921
+ test_directive_span ( "// eslint-disable-next-line max-params \n ABC" , 42 , 47 ) ;
922
+ test_directive_span ( "// eslint-disable-next-line max-params \r \n ABC" , 42 , 48 ) ;
923
+ test_directive_span ( "// eslint-disable-next-line max-params \n ABC \n " , 42 , 48 ) ;
924
+ test_directive_span ( "// eslint-disable-next-line max-params \r \n ABC \r \n " , 42 , 49 ) ;
925
+ }
893
926
}
0 commit comments