Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions crates/oxc_linter/src/rules/eslint/sort_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,22 +458,22 @@ fn get_first_local_member_name<'a>(decl: &ImportDeclaration<'a>) -> Option<Cow<'

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

// In same line
if count < 2 {
if count < 1 {
return 0;
}

// In different lines, need to subtract 2 because the count includes the first and last line.
count - 2
// In different lines, need to subtract 1, because we need new line 2 time to have 1 line
// between node
count - 1
}

#[test]
Expand Down Expand Up @@ -584,6 +584,13 @@ fn test() {
import a from 'a';",
Some(serde_json::json!([{ "allowSeparatedGroups": true }])),
),
// No leading whitespaces - issue #15990
(
"import b from 'b';

import a from 'a';",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the change in this test is to fail the previous implementation

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, can we include both still?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I added a new test case instead of modifying the existing one.

Some(serde_json::json!([{ "allowSeparatedGroups": true }])),
),
(
"import a from 'a';

Expand Down
Loading