Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(eslint-plugin): [consistent-type-imports] support fixing to inli…
…ne types (#5050)

* feat(eslint-plugin): [consistent-type-imports] support fixing to inline types

* first pass fix to simpler fixer

* cleanup pass 2

* add test for default import

* add some docs

* add moar test cases

* add failing test

* use aImportIsOnlyTypes instead of inlineTypes message

* wip

* fix for default import inline

* fix another case with as

* simplify by using existing typeOverValue func

* add some commentse

* add another test for two imports from same source

* better doc with link to 4.5

* fix lint

* cleanup comments

* Update packages/eslint-plugin/docs/rules/consistent-type-imports.md

Co-authored-by: Josh Goldberg <me@joshuakgoldberg.com>

* Update packages/eslint-plugin/docs/rules/consistent-type-imports.md

Co-authored-by: Josh Goldberg <me@joshuakgoldberg.com>

* Update packages/eslint-plugin/docs/rules/consistent-type-imports.md

Co-authored-by: Josh Goldberg <me@joshuakgoldberg.com>

* Update packages/eslint-plugin/src/rules/consistent-type-imports.ts

Co-authored-by: Josh Goldberg <me@joshuakgoldberg.com>

* Update packages/eslint-plugin/src/rules/consistent-type-imports.ts

Co-authored-by: Josh Goldberg <me@joshuakgoldberg.com>

* fix changelog formatting

* Update packages/eslint-plugin/docs/rules/consistent-type-imports.md

Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>

* try single quotest

* cleanup not valid comment anymore

* Update packages/eslint-plugin/tests/rules/consistent-type-imports.test.ts

Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>

* Update packages/eslint-plugin/tests/rules/consistent-type-imports.test.ts

Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>

* rm comment

* improve documentation with feedback

* add tabs

Co-authored-by: Josh Goldberg <me@joshuakgoldberg.com>
Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>
  • Loading branch information
3 people committed Nov 7, 2022
1 parent c759da1 commit 75dcdf1
Show file tree
Hide file tree
Showing 3 changed files with 536 additions and 32 deletions.
40 changes: 39 additions & 1 deletion packages/eslint-plugin/docs/rules/consistent-type-imports.md
Expand Up @@ -15,7 +15,7 @@ This allows transpilers to drop imports without knowing the types of the depende

This option defines the expected import kind for type-only imports. Valid values for `prefer` are:

- `type-imports` will enforce that you always use `import type Foo from '...'` except referenced by metadata of decorators. It is default.
- `type-imports` will enforce that you always use `import type Foo from '...'` except referenced by metadata of decorators. It is the default.
- `no-type-imports` will enforce that you always use `import Foo from '...'`.

Examples of **correct** code with `{prefer: 'type-imports'}`, and **incorrect** code with `{prefer: 'no-type-imports'}`.
Expand All @@ -36,6 +36,44 @@ type T = Foo;
const x: Bar = 1;
```

### `fixStyle`

This option defines the expected type modifier to be added when an import is detected as used only in the type position. Valid values for `fixStyle` are:

- `separate-type-imports` will add the type keyword after the import keyword `import type { A } from '...'`. It is the default.
- `inline-type-imports` will inline the type keyword `import { type A } from '...'` and is only available in TypeScript 4.5 and onwards. See [documentation here](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-5.html#type-modifiers-on-import-names 'TypeScript 4.5 documentation on type modifiers and import names').

<!--tabs-->

#### ❌ Incorrect

```ts
import { Foo } from 'Foo';
import Bar from 'Bar';
type T = Foo;
const x: Bar = 1;
```

#### ✅ With `separate-type-imports`

```ts
import type { Foo } from 'Foo';
import type Bar from 'Bar';
type T = Foo;
const x: Bar = 1;
```

#### ✅ With `inline-type-imports`

```ts
import { type Foo } from 'Foo';
import type Bar from 'Bar';
type T = Foo;
const x: Bar = 1;
```

<!--tabs-->

### `disallowTypeAnnotations`

If `true`, type imports in type annotations (`import()`) are not allowed.
Expand Down

0 comments on commit 75dcdf1

Please sign in to comment.