Skip to content

Commit aba3654

Browse files
authoredApr 1, 2025··
fix(linter): span disable directive correctly on next line (#10141)
Previous code didn't account for the newline characters (0..N carriage return characters and one line feed character) between the line of the disable directive comment and the next line. Fixes #10115
1 parent 5eb27b5 commit aba3654

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed
 

‎crates/oxc_linter/src/disable_directives.rs

+38-5
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,19 @@ impl<'a> DisableDirectivesBuilder<'a> {
164164
// `eslint-disable-next-line`
165165
else if let Some(text) = text.strip_prefix("-next-line") {
166166
// 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+
171180
if text.trim().is_empty() {
172181
self.add_interval(
173182
comment_span.end,
@@ -695,7 +704,7 @@ semi*/
695704
}
696705

697706
#[cfg(test)]
698-
mod test_unused_directives {
707+
mod tests {
699708
use oxc_allocator::Allocator;
700709
use oxc_ast::Comment;
701710
use oxc_parser::Parser;
@@ -726,6 +735,17 @@ mod test_unused_directives {
726735
}
727736
}
728737

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+
729749
#[test]
730750
fn unused_enable_all() {
731751
test_directives(
@@ -890,4 +910,17 @@ mod test_unused_directives {
890910
},
891911
);
892912
}
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+
}
893926
}

0 commit comments

Comments
 (0)
Please sign in to comment.