Skip to content

Commit

Permalink
cleanup with prefer-inline and better docs
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer committed Nov 8, 2022
1 parent 5d5a2d8 commit 0a2f060
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
12 changes: 9 additions & 3 deletions docs/rules/no-duplicates.md
Expand Up @@ -69,25 +69,31 @@ import * from './mod.js?minify'

### Inline Type imports

TypeScript 4.5 introduced a new [feature](https://devblogs.microsoft.com/typescript/announcing-typescript-4-5/#type-on-import-names) that allows mixing of named value and type imports. In order to support fixing to an inline type import when duplicate imports are detected, `preferInline` can be set to true.
TypeScript 4.5 introduced a new [feature](https://devblogs.microsoft.com/typescript/announcing-typescript-4-5/#type-on-import-names) that allows mixing of named value and type imports. In order to support fixing to an inline type import when duplicate imports are detected, `prefer-inline` can be set to true.

Config:

```json
"import/no-duplicates": ["error", {"preferInline": true}]
"import/no-duplicates": ["error", {"prefer-inline": true}]
```

<!--tabs-->

❌ Invalid

```js
import { AValue, type AType } from './mama-mia'
import type { BType } from './mama-mia'
```

will fix to
✅ Valid with `["error", "prefer-inline"]`

```js
import { AValue, type AType, type BType } from './mama-mia'
```

<!--tabs-->

## When Not To Use It

If the core ESLint version is good enough (i.e. you're _not_ using Flow and you _are_ using [`import/extensions`](./extensions.md)), keep it and don't use this.
Expand Down
6 changes: 3 additions & 3 deletions src/rules/no-duplicates.js
Expand Up @@ -109,8 +109,8 @@ function getFix(first, rest, sourceCode, context) {
const [specifiersText] = specifiers.reduce(
([result, needsComma], specifier) => {
const isTypeSpecifier = specifier.importNode.importKind === 'type';
const preferInline = context.options[0] && context.options[0].preferInline;
const insertText = `${preferInline && isTypeSpecifier ? 'type ' : ''}${specifier.text}`;
const preferInline = context.options[0] && context.options[0]['prefer-inline'];
const insertText = `${prefer-inline && isTypeSpecifier ? 'type ' : ''}${specifier.text}`;
return [
needsComma && !specifier.isEmpty
? `${result},${insertText}`
Expand Down Expand Up @@ -260,7 +260,7 @@ module.exports = {
considerQueryString: {
type: 'boolean',
},
preferInline: {
'prefer-inline': {
type: 'boolean',
},
},
Expand Down
4 changes: 2 additions & 2 deletions tests/src/rules/no-duplicates.js
Expand Up @@ -538,7 +538,7 @@ context('TypeScript', function () {
test({
code: "import {type x} from './foo'; import type {y} from './foo'",
...parserConfig,
options: [{ 'preferInline': false }],
options: [{ 'prefer-inline': false }],
output: `import {type x,y} from './foo'; `,
errors: [
{
Expand All @@ -556,7 +556,7 @@ context('TypeScript', function () {
test({
code: "import {type x} from 'foo'; import type {y} from 'foo'",
...parserConfig,
options: [{ 'preferInline': true }],
options: [{ 'prefer-inline': true }],
output: `import {type x,type y} from 'foo'; `,
errors: [
{
Expand Down

0 comments on commit 0a2f060

Please sign in to comment.