Skip to content

Commit

Permalink
[New] order: allow validating named imports
Browse files Browse the repository at this point in the history
  • Loading branch information
manuth authored and sparten11740 committed Nov 21, 2024
1 parent 4468692 commit 020b595
Show file tree
Hide file tree
Showing 5 changed files with 880 additions and 44 deletions.
4 changes: 4 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`]: allow validating named imports ([#3043], thanks [@manuth])

### Fixed
- `ExportMap` / flat config: include `languageOptions` in context ([#3052], thanks [@michaelfaith])

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

[#3052]: https://github.com/import-js/eslint-plugin-import/pull/3052
[#3043]: https://github.com/import-js/eslint-plugin-import/pull/3043
[#3036]: https://github.com/import-js/eslint-plugin-import/pull/3036
[#3033]: https://github.com/import-js/eslint-plugin-import/pull/3033
[#3018]: https://github.com/import-js/eslint-plugin-import/pull/3018
Expand Down
72 changes: 72 additions & 0 deletions docs/rules/order.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,78 @@ import index from './';
import sibling from './foo';
```

### `named: true|false|{ enabled: true|false, import: true|false, export: true|false, require: true|false, cjsExports: true|false, types: mixed|types-first|types-last }`

Enforce ordering of names within imports and exports:

- If set to `true`, named imports must be ordered according to the `alphabetize` options
- If set to `false`, named imports can occur in any order

`enabled` enables the named ordering for all expressions by default.
Use `import`, `export` and `require` and `cjsExports` to override the enablement for the following kind of expressions:

- `import`:

```ts
import { Readline } from "readline";
```

- `export`:

```ts
export { Readline };
// and
export { Readline } from "readline";
```

- `require`

```ts
const { Readline } = require("readline");
```

- `cjsExports`

```ts
module.exports.Readline = Readline;
// and
module.exports = { Readline };
```

The `types` option allows you to specify the order of `import`s and `export`s of `type` specifiers.
Following values are possible:

- `types-first`: forces `type` specifiers to occur first
- `types-last`: forces value specifiers to occur first
- `mixed`: sorts all specifiers in alphabetical order

The default value is `false`.

Example setting:

```ts
{
named: true,
alphabetize: {
order: 'asc'
}
}
```

This will fail the rule check:

```ts
/* eslint import/order: ["error", {"named": true, "alphabetize": {"order": "asc"}}] */
import { compose, apply } from 'xcompose';
```

While this will pass:

```ts
/* eslint import/order: ["error", {"named": true, "alphabetize": {"order": "asc"}}] */
import { apply, compose } from 'xcompose';
```

### `alphabetize: {order: asc|desc|ignore, orderImportKind: asc|desc|ignore, caseInsensitive: true|false}`

Sort the order within each group in alphabetical manner based on **import path**:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
"object.groupby": "^1.0.3",
"object.values": "^1.2.0",
"semver": "^6.3.1",
"string.prototype.trimend": "^1.0.8",
"tsconfig-paths": "^3.15.0"
}
}
Loading

0 comments on commit 020b595

Please sign in to comment.