Skip to content

Commit caad188

Browse files
committed
fix(linter): fix eslint/sort-imports allowSeparatedGroups not working with single empty line
This fix #15990
1 parent 024b48a commit caad188

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

crates/oxc_linter/src/rules/eslint/sort_imports.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -458,22 +458,21 @@ fn get_first_local_member_name<'a>(decl: &ImportDeclaration<'a>) -> Option<Cow<'
458458

459459
// Calculates number of lines between two nodes. It is assumed that the given `left` span appears before
460460
// the given `right` span in the source code. Lines are counted from the end of the `left` span till the
461-
// start of the `right` span. If the given span are on the same line, or `right` span is appears before `left` span,
462-
// it returns `0`.
461+
// start of the `right` span. If the given span are on the same or consecutive lines, or `right` span is
462+
// appears before `left` span, it returns `0`.
463463
fn get_number_of_lines_between(left: Span, right: Span, ctx: &LintContext) -> usize {
464464
if left.end >= right.start {
465465
return 0;
466466
}
467467
let between_span = Span::new(left.end, right.start);
468-
let count = ctx.source_range(between_span).lines().count();
468+
let count = ctx.source_range(between_span).chars().filter(|c| *c == '\n').count();
469469

470-
// In same line
471-
if count < 2 {
470+
if count < 1 {
472471
return 0;
473472
}
474473

475474
// In different lines, need to subtract 2 because the count includes the first and last line.
476-
count - 2
475+
count - 1
477476
}
478477

479478
#[test]
@@ -581,7 +580,7 @@ fn test() {
581580
(
582581
"import b from 'b';
583582
584-
import a from 'a';",
583+
import a from 'a';",
585584
Some(serde_json::json!([{ "allowSeparatedGroups": true }])),
586585
),
587586
(

0 commit comments

Comments
 (0)