Skip to content

Commit 6f48deb

Browse files
manuthljharb
authored andcommitted
[New] order: allow validating named imports
1 parent a9018a8 commit 6f48deb

File tree

5 files changed

+882
-44
lines changed

5 files changed

+882
-44
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
66

77
## [Unreleased]
88

9+
### Added
10+
- [`order`]: allow validating named imports ([#3043], thanks [@manuth])
11+
912
### Fixed
1013
- `ExportMap` / flat config: include `languageOptions` in context ([#3052], thanks [@michaelfaith])
1114

@@ -1133,6 +1136,7 @@ for info on changes for earlier releases.
11331136
[`memo-parser`]: ./memo-parser/README.md
11341137

11351138
[#3052]: https://github.com/import-js/eslint-plugin-import/pull/3052
1139+
[#3043]: https://github.com/import-js/eslint-plugin-import/pull/3043
11361140
[#3036]: https://github.com/import-js/eslint-plugin-import/pull/3036
11371141
[#3033]: https://github.com/import-js/eslint-plugin-import/pull/3033
11381142
[#3018]: https://github.com/import-js/eslint-plugin-import/pull/3018

docs/rules/order.md

+72
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,78 @@ import index from './';
285285
import sibling from './foo';
286286
```
287287

288+
### `named: true|false|{ enabled: true|false, import: true|false, export: true|false, require: true|false, cjsExports: true|false, types: mixed|types-first|types-last }`
289+
290+
Enforce ordering of names within imports and exports:
291+
292+
- If set to `true`, named imports must be ordered according to the `alphabetize` options
293+
- If set to `false`, named imports can occur in any order
294+
295+
`enabled` enables the named ordering for all expressions by default.
296+
Use `import`, `export` and `require` and `cjsExports` to override the enablement for the following kind of expressions:
297+
298+
- `import`:
299+
300+
```ts
301+
import { Readline } from "readline";
302+
```
303+
304+
- `export`:
305+
306+
```ts
307+
export { Readline };
308+
// and
309+
export { Readline } from "readline";
310+
```
311+
312+
- `require`
313+
314+
```ts
315+
const { Readline } = require("readline");
316+
```
317+
318+
- `cjsExports`
319+
320+
```ts
321+
module.exports.Readline = Readline;
322+
// and
323+
module.exports = { Readline };
324+
```
325+
326+
The `types` option allows you to specify the order of `import`s and `export`s of `type` specifiers.
327+
Following values are possible:
328+
329+
- `types-first`: forces `type` specifiers to occur first
330+
- `types-last`: forces value specifiers to occur first
331+
- `mixed`: sorts all specifiers in alphabetical order
332+
333+
The default value is `false`.
334+
335+
Example setting:
336+
337+
```ts
338+
{
339+
named: true,
340+
alphabetize: {
341+
order: 'asc'
342+
}
343+
}
344+
```
345+
346+
This will fail the rule check:
347+
348+
```ts
349+
/* eslint import/order: ["error", {"named": true, "alphabetize": {"order": "asc"}}] */
350+
import { compose, apply } from 'xcompose';
351+
```
352+
353+
While this will pass:
354+
355+
```ts
356+
/* eslint import/order: ["error", {"named": true, "alphabetize": {"order": "asc"}}] */
357+
import { apply, compose } from 'xcompose';
358+
```
359+
288360
### `alphabetize: {order: asc|desc|ignore, orderImportKind: asc|desc|ignore, caseInsensitive: true|false}`
289361

290362
Sort the order within each group in alphabetical manner based on **import path**:

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
"object.groupby": "^1.0.3",
127127
"object.values": "^1.2.0",
128128
"semver": "^6.3.1",
129+
"string.prototype.trimend": "^1.0.8",
129130
"tsconfig-paths": "^3.15.0"
130131
}
131132
}

0 commit comments

Comments
 (0)