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 3 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
17 changes: 14 additions & 3 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,9 @@ Valid properties and their values include:

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

- **`orderByFullPathString`**: use `true` to sort by the full un-split path string and `false` to split by paths and sort by each one
- enabling this flag may better align with the sort algorithm used by certain operating systems and IDE's, e.g. the Organize Imports action in JetBrains IDE's
Copy link
Member

Choose a reason for hiding this comment

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

sorry to ask for more work, but i'd love some research on lists for both of the options :-)

Copy link
Author

@tw3 tw3 Jul 1, 2025

Choose a reason for hiding this comment

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

personally I'm only aware of the impact to Organize Imports in WebStorm b/c that is what has affected me and my team

not sure how i would find other impacted examples

i know that other people have taken note of this difference: #2722

apparently this has had an effect on Microsoft's accessibility-insights-web tool

microsoft/accessibility-insights-web#6846

should i add that to the markup?

Copy link
Member

Choose a reason for hiding this comment

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

That'd be great!

How does your local OS sort these things?

Copy link
Author

@tw3 tw3 Jul 1, 2025

Choose a reason for hiding this comment

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

Here's a video demo of the Organize Imports behavior in WebStorm IDE:

org-imports-demo-video.mp4

Ideally Jetbrains would make the sort algorithm for Organize Imports configurable from their end, but unfortunately that isn't currently possible.

So without a change in this plugin my team will either need to:

  • Stay on version 2.26 of this plugin, or
  • Always sort according to Eslint and no longer use the action in WebStorm, or
  • Choose a different/simpler import plugin that "vibes" with WebStorm

Copy link
Member

Choose a reason for hiding this comment

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

Sure, that makes sense :-)

I meant, in Mac and Windows, say, if you name a bunch of empty files as the specifier names, how does it sort them?

Copy link
Author

@tw3 tw3 Jul 1, 2025

Choose a reason for hiding this comment

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

This is what I see on Windows

image

This does indeed list files according to the new split-by-path-then-order algorithm

Or were you looking for something different?

Copy link
Member

Choose a reason for hiding this comment

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

Nope, that's perfect! I believe Mac works the same way.


#### Example

Given the following settings:
Expand All @@ -394,7 +397,8 @@ Given the following settings:
"import/order": ["error", {
"alphabetize": {
"order": "asc",
"caseInsensitive": true
"caseInsensitive": true,
"orderByFullPathString": true,
}
}]
}
Expand All @@ -408,6 +412,8 @@ import aTypes from 'prop-types';
import { compose, apply } from 'xcompose';
import * as classnames from 'classnames';
import blist from 'BList';
import timeUtils1 from '../utils/time'
import timeUtils2 from '../utils-time'
```

While this will pass:
Expand All @@ -418,6 +424,11 @@ import * as classnames from 'classnames';
import aTypes from 'prop-types';
import React, { PureComponent } from 'react';
import { compose, apply } from 'xcompose';
// Below the '../utils-time' and '../utils/time' path are not split before ordering ("orderByFullPathString": true).
// Instead, during comparison the "-" in "../utils-time" occurs lexicographically before the
// second "/" in "../utils/time"
import timeUtils2 from '../utils-time'
import timeUtils1 from '../utils/time'
```

### `named`
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