Skip to content
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

[New] import/order: add support for named imports #3043

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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