Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

## [Unreleased]

### Added
- [`order`]: add `orderBySplitPaths` option

## [2.32.0] - 2025-06-20

### Added
Expand Down
6 changes: 4 additions & 2 deletions docs/rules/order.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ import index from './';

### `alphabetize`

Valid values: `{ order?: "asc" | "desc" | "ignore", orderImportKind?: "asc" | "desc" | "ignore", caseInsensitive?: boolean }` \
Default: `{ order: "ignore", orderImportKind: "ignore", caseInsensitive: false }`
Valid values: `{ order?: "asc" | "desc" | "ignore", orderImportKind?: "asc" | "desc" | "ignore", caseInsensitive?: boolean, orderBySplitPaths?: boolean }` \
Default: `{ order: "ignore", orderImportKind: "ignore", caseInsensitive: false, orderBySplitPaths: true }`

Determine the sort order of imports within each [predefined group][18] or [`PathGroup`][8] alphabetically based on specifier.

Expand All @@ -385,6 +385,8 @@ Valid properties and their values include:

- **`caseInsensitive`**: use `true` to ignore case and `false` to consider case when sorting

- **`orderBySplitPaths`**: use `true` to split by paths and sort by each one and `false` to sort by the full un-split path string

#### Example

Given the following settings:
Expand Down
12 changes: 10 additions & 2 deletions src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,10 @@
const importB = getNormalizedValue(nodeB, alphabetizeOptions.caseInsensitive);
let result = 0;

if (!includes(importA, '/') && !includes(importB, '/')) {
if (
alphabetizeOptions.orderBySplitPaths === false
|| !includes(importA, '/') && !includes(importB, '/')

Check warning on line 438 in src/rules/order.js

View check run for this annotation

Codecov / codecov/patch

src/rules/order.js#L436-L438

Added lines #L436 - L438 were not covered by tests
) {
result = compareString(importA, importB);
} else {
const A = importA.split('/');
Expand Down Expand Up @@ -831,8 +834,9 @@
const order = alphabetize.order || 'ignore';
const orderImportKind = alphabetize.orderImportKind || 'ignore';
const caseInsensitive = alphabetize.caseInsensitive || false;
const orderBySplitPaths = alphabetize.orderBySplitPaths || true;

Check warning on line 837 in src/rules/order.js

View check run for this annotation

Codecov / codecov/patch

src/rules/order.js#L837

Added line #L837 was not covered by tests

return { order, orderImportKind, caseInsensitive };
return { order, orderImportKind, caseInsensitive, orderBySplitPaths };

Check warning on line 839 in src/rules/order.js

View check run for this annotation

Codecov / codecov/patch

src/rules/order.js#L839

Added line #L839 was not covered by tests
}

// TODO, semver-major: Change the default of "distinctGroup" from true to false
Expand Down Expand Up @@ -962,6 +966,10 @@
enum: ['ignore', 'asc', 'desc'],
default: 'ignore',
},
orderBySplitPaths: {
type: 'boolean',
default: true,
},
},
additionalProperties: false,
},
Expand Down
64 changes: 64 additions & 0 deletions tests/src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,48 @@ ruleTester.run('order', rule, {
alphabetize: { order: 'desc' },
}],
}),
// Option alphabetize: {order: 'asc'} with orderBySplitPaths: false`
test({
code: `
import a from "foo";
import b from "foo-bar";
import c from "foo/bar";
import d from "foo/barfoo";
`,
options: [{ alphabetize: { order: 'asc' }, orderBySplitPaths: false }],
}),
// Option alphabetize: {order: 'asc'} with orderBySplitPaths: false
test({
code: `
import a from "foo";
import b from "foo-bar";
import c from "foo/foobar/bar";
import d from "foo/foobar/barfoo";
`,
options: [{ alphabetize: { order: 'asc' }, orderBySplitPaths: false }],
}),
// Option alphabetize: {order: 'desc'} with orderBySplitPaths: false
test({
code: `
import d from "foo/barfoo";
import c from "foo/bar";
import b from "foo-bar";
import a from "foo";
`,
options: [{ alphabetize: { order: 'desc' }, orderBySplitPaths: false }],
}),
// Option alphabetize: {order: 'desc'} with orderBySplitPaths: false and file names having non-alphanumeric characters.
test({
code: `
import d from "foo/barfoo";
import b from "foo-bar";
import c from "foo,bar";
import a from "foo";`,
options: [{
alphabetize: { order: 'desc' },
orderBySplitPaths: false,
}],
}),
// Option alphabetize with newlines-between: {order: 'asc', newlines-between: 'always'}
test({
code: `
Expand Down Expand Up @@ -2646,6 +2688,28 @@ ruleTester.run('order', rule, {
message: '`foo-bar` import should occur after import of `foo/barfoo`',
}],
}),
// Option alphabetize: {order: 'asc'} with orderBySplitPaths: false
test({
code: `
import a from "foo";
import c from "foo/bar";
import d from "foo/barfoo";
import b from "foo-bar";
`,
options: [{
alphabetize: { order: 'asc' },
orderBySplitPaths: false,
}],
output: `
import a from "foo";
import b from "foo-bar";
import c from "foo/bar";
import d from "foo/barfoo";
`,
errors: [{
message: '`foo/barfoo` import should occur after import of `foo-bar`',
}],
}),
// Option alphabetize {order: 'asc': caseInsensitive: true}
test({
code: `
Expand Down
Loading