Conversation
Contributor
Author
|
@ljharb TL;DR for the help-wanted #3181: adds a |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3272 +/- ##
==========================================
+ Coverage 79.58% 79.61% +0.02%
==========================================
Files 98 98
Lines 4527 4533 +6
Branches 1529 1534 +5
==========================================
+ Hits 3603 3609 +6
Misses 924 924 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…s by their alias
When `named` ordering is enabled, specifiers are sorted by their
original name, so `import { b, a as c }` is reported even though the
local bindings `b` and `c` are already alphabetized. `named.sortBy`
accepts 'name' (default, current behavior) or 'alias', which sorts
aliased specifiers by the name to the right of `as` (the local name of
an import, or the exported name of an export) across imports, exports,
destructured requires, and CJS exports.
Fixes import-js#3181.
KAMRONBEK
force-pushed
the
new/3181-order-named-alias
branch
from
July 17, 2026 15:57
0526aa5 to
3b0ac8e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
With
named: trueandalphabetize: { order: 'asc' },import { b, a as c } from 'lib';is reported because named ordering always sorts by the original (imported/exported) name — herea— with no way to order by the local aliasc. #3181 asks for an option to sort by the alias instead; ljharb: "That seems like a reasonable option to add." (labels: help wanted, semver-minor).Implementation
New
named.sortByoption following the existingnamedoption-object shape (likenamed.types):'name'(default — current behavior): sort aliased specifiers by their original name (the name to the left ofas).'alias': sort aliased specifiers by their alias (the name to the right ofas— the local name of an aliased import / destructured require, or the exported name of an aliased export / CJS export), falling back to the name when not aliased.Changes in
src/rules/order.js:sortBy: { type: 'string', enum: ['name', 'alias'] }on thenamedobject form;create():sortBy: 'name'default merged into the normalizednamedoptions;makeNamedOrderReport(): in alias mode the composite sort key becomes`${alias || name}:${name}`(mirroring the existing`${name}:${alias}`scheme, so duplicate keys stay distinct), and the reportdisplayNamebecomesname as aliasso messages are unambiguous, e.g.'`b` import should occur before import of `c as z`'.The autofix needs no changes — it already swaps whole specifier texts, so reordering follows the new ranks.
Docs
docs/rules/order.mdnamedsection: updated the Valid values line and added anamed.sortByvalue list with a pass/fail example, matching the existingnamed.typesdoc format.Tests
7 new cases in
tests/src/rules/order.js, following the surroundingnamedtest style:import { b, a as c }withsortBy: 'alias'); a combined aliasedexport { Z as A, B }+ destructuredrequire+module.exports = { b: B, a: Z }case; explicitsortBy: 'name'keeping current behavior;outputand messages): aliased imports, aliased exports + destructured require in one test,module.exportsobject exports, and explicitsortBy: 'name'on the issue's example.All 326 tests in the file pass; the 7 new tests fail without the implementation (verified by temporarily reverting
src/rules/order.js). Default behavior is unchanged (the full pre-existing suite passes untouched), so this is semver-minor.Fixes #3181.