Skip to content

Commit

Permalink
[Docs] extensions, order: improve documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Xunnamius authored and ljharb committed Nov 18, 2024
1 parent a20d843 commit d5f2950
Show file tree
Hide file tree
Showing 4 changed files with 815 additions and 163 deletions.
3 changes: 3 additions & 0 deletions .markdownlint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"line-length": false,
"no-duplicate-heading": {
"siblings_only": true
},
"ul-indent": {
"start_indent": 1,
"start_indented": true
Expand Down
5 changes: 5 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]

### Changed
- [Docs] `extensions`, `order`: improve documentation ([#3106], thanks [@Xunnamius])

## [2.31.0] - 2024-10-03

### Added
Expand Down Expand Up @@ -1152,6 +1155,7 @@ for info on changes for earlier releases.

[`memo-parser`]: ./memo-parser/README.md

[#3106]: https://github.com/import-js/eslint-plugin-import/pull/3106
[#3073]: https://github.com/import-js/eslint-plugin-import/pull/3073
[#3072]: https://github.com/import-js/eslint-plugin-import/pull/3072
[#3071]: https://github.com/import-js/eslint-plugin-import/pull/3071
Expand Down Expand Up @@ -2018,6 +2022,7 @@ for info on changes for earlier releases.
[@wtgtybhertgeghgtwtg]: https://github.com/wtgtybhertgeghgtwtg
[@xM8WVqaG]: https://github.com/xM8WVqaG
[@xpl]: https://github.com/xpl
[@Xunnamius]: https://github.com/Xunnamius
[@yesl-kim]: https://github.com/yesl-kim
[@yndajas]: https://github.com/yndajas
[@yordis]: https://github.com/yordis
Expand Down
55 changes: 51 additions & 4 deletions docs/rules/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ In order to provide a consistent use of file extensions across your code base, t

This rule either takes one string option, one object option, or a string and an object option. If it is the string `"never"` (the default value), then the rule forbids the use for any extension. If it is the string `"always"`, then the rule enforces the use of extensions for all import statements. If it is the string `"ignorePackages"`, then the rule enforces the use of extensions for all import statements except package imports.

```json
```jsonc
"import/extensions": [<severity>, "never" | "always" | "ignorePackages"]
```

By providing an object you can configure each extension separately.

```json
```jsonc
"import/extensions": [<severity>, {
<extension>: "never" | "always" | "ignorePackages"
}]
Expand All @@ -26,7 +26,7 @@ By providing an object you can configure each extension separately.

By providing both a string and an object, the string will set the default setting for all extensions, and the object can be used to set granular overrides for specific extensions.

```json
```jsonc
"import/extensions": [
<severity>,
"never" | "always" | "ignorePackages",
Expand All @@ -40,7 +40,7 @@ For example, `["error", "never", { "svg": "always" }]` would require that all ex

`ignorePackages` can be set as a separate boolean option like this:

```json
```jsonc
"import/extensions": [
<severity>,
"never" | "always" | "ignorePackages",
Expand All @@ -58,6 +58,50 @@ Default value of `ignorePackages` is `false`.

By default, `import type` and `export type` style imports/exports are ignored. If you want to check them as well, you can set the `checkTypeImports` option to `true`.

Unfortunately, in more advanced linting setups, such as when employing custom specifier aliases (e.g. you're using `eslint-import-resolver-alias`, `paths` in `tsconfig.json`, etc), this rule can be too coarse-grained when determining which imports to ignore and on which to enforce the config.
This is especially troublesome if you have import specifiers that [look like externals or builtins](./order.md#how-imports-are-grouped).

Set `pathGroupOverrides` to force this rule to always ignore certain imports and never ignore others.
`pathGroupOverrides` accepts an array of one or more [`PathGroupOverride`](#pathgroupoverride) objects.

For example:

```jsonc
"import/extensions": [
<severity>,
"never" | "always" | "ignorePackages",
{
ignorePackages: true | false,
pattern: {
<extension>: "never" | "always" | "ignorePackages"
},
pathGroupOverrides: [
{
pattern: "package-name-to-ignore",
action: "ignore",
},
{
pattern: "bespoke+alias:{*,*/**}",
action: "enforce",
}
]
}
]
```

> \[!NOTE]
>
> `pathGroupOverrides` is inspired by [`pathGroups` in `'import/order'`](./order.md#pathgroups) and shares a similar interface.
> If you're using `pathGroups` already, you may find `pathGroupOverrides` very useful.
### `PathGroupOverride`

| property | required | type | description |
| :--------------: | :------: | :---------------------: | --------------------------------------------------------------- |
| `pattern` | ☑️ | `string` | [Minimatch pattern][16] for specifier matching |
| `patternOptions` | | `object` | [Minimatch options][17]; default: `{nocomment: true}` |
| `action` | ☑️ | `"enforce" \| "ignore"` | What action to take on imports whose specifiers match `pattern` |

### Exception

When disallowing the use of certain extensions this rule makes an exception and allows the use of extension when the file would not be resolvable without extension.
Expand Down Expand Up @@ -190,3 +234,6 @@ export type { Foo } from './foo';
If you are not concerned about a consistent usage of file extension.

In the future, when this rule supports native node ESM resolution, and the plugin is configured to use native rather than transpiled ESM (a config option that is not yet available) - setting this to `always` will have no effect.

[16]: https://www.npmjs.com/package/minimatch#features
[17]: https://www.npmjs.com/package/minimatch#options
Loading

0 comments on commit d5f2950

Please sign in to comment.