Skip to content

[New] order: add orderByFullPathString option #3196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
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 `orderByFullPathString` 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, orderByFullPathString?: boolean }` \
Default: `{ order: "ignore", orderImportKind: "ignore", caseInsensitive: false, orderByFullPathString: false }`

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

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

Choose a reason for hiding this comment

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

is there a better way to phrase this?

like, perhaps one way sorts the way certain operating systems and IDEs do, and the other sorts the way other ones do? (with an example list of each)


#### 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 @@ function getSorter(alphabetizeOptions) {
const importB = getNormalizedValue(nodeB, alphabetizeOptions.caseInsensitive);
let result = 0;

if (!includes(importA, '/') && !includes(importB, '/')) {
if (
alphabetizeOptions.orderByFullPathString === true
|| !includes(importA, '/') && !includes(importB, '/')
) {
result = compareString(importA, importB);
} else {
const A = importA.split('/');
Expand Down Expand Up @@ -831,8 +834,9 @@ function getAlphabetizeConfig(options) {
const order = alphabetize.order || 'ignore';
const orderImportKind = alphabetize.orderImportKind || 'ignore';
const caseInsensitive = alphabetize.caseInsensitive || false;
const orderByFullPathString = alphabetize.orderByFullPathString || false;

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

// TODO, semver-major: Change the default of "distinctGroup" from true to false
Expand Down Expand Up @@ -962,6 +966,10 @@ module.exports = {
enum: ['ignore', 'asc', 'desc'],
default: 'ignore',
},
orderByFullPathString: {
type: 'boolean',
default: false,
},
},
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 orderByFullPathString: true`
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' }, orderByFullPathString: true }],
}),
// Option alphabetize: {order: 'asc'} with orderByFullPathString: true
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' }, orderByFullPathString: true }],
}),
// Option alphabetize: {order: 'desc'} with orderByFullPathString: true
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' }, orderByFullPathString: true }],
}),
// Option alphabetize: {order: 'desc'} with orderByFullPathString: true 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' },
orderByFullPathString: true,
}],
}),
// 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 orderByFullPathString: true
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' },
orderByFullPathString: true,
}],
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