[New] add `named-order` rule to sort specifiers of import, export, require
Examples
✅ Valid with named-order
import { a, b } from './foo';
const { a, b } = require('./foo');
export { a, b };
❌ Invalid with named-order
import { b, a } from './foo';
const { b, a } = require('./foo');
export { b, a };
Why?
I experienced a significant decrease in readability as the number of specifiers increased in the named import. So, I made this rule. Tell me if there's anything weird in this rule 😄
Please discuss things in issues before sending a PR.
What’s your use case where you have that many named imports? Things should be deep-imported from granular modules.
Please discuss things in issues before sending a PR.
What’s your use case where you have that many named imports? Things should be deep-imported from granular modules.
Oh I see, I will write an issue about this. So Is it ok to change to draft, or should be closed this PR?
What’s your use case where you have that many named imports? Things should be deep-imported from granular modules.
It is a good to have feature when using something like rxjs IMO:
import {
BehaviorSubject,
combineLatest,
distinctUntilChanged,
map,
switchMap,
tap,
} from 'rxjs';
It will reduce conflicts when collaborating with others.
I wrote an issue for discussion about this 😄 https://github.com/import-js/eslint-plugin-import/issues/2553
@ronparkdev hi, if we have an import like import fetch, { RequestInit, Response } from 'node-fetch' then your rule gives such an error. 🥲
TypeError: Cannot read properties of undefined (reading 'name')
Occurred while linting /project/sandbox/src/server.ts:2
at /project/sandbox/node_modules/eslint-plugin-eslint-custom-rules/rules/named-order.js:53:49
at Array.reduce (<anonymous>)
at getNormalizedValue (/project/sandbox/node_modules/eslint-plugin-eslint-custom-rules/rules/named-order.js:53:21)
at sorter (/project/sandbox/node_modules/eslint-plugin-eslint-custom-rules/rules/named-order.js:70:24)
at Array.sort (<anonymous>)
at handleImports (/project/sandbox/node_modules/eslint-plugin-eslint-custom-rules/rules/named-order.js:130:50)
at /project/sandbox/node_modules/eslint/lib/linter/safe-emitter.js:45:58
at Array.forEach (<anonymous>)
at Object.emit (/project/sandbox/node_modules/eslint/lib/linter/safe-emitter.js:45:38)
at NodeEventGenerator.applySelector (/project/sandbox/node_modules/eslint/lib/linter/node-event-generator.js:293:26)
error Command failed with exit code 2.