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 typescript flow rule #2614

Draft
wants to merge 38 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
1830d7a
add debug command for new rule
azyzz228 Dec 5, 2022
5684be8
draft files for the rule, test, docs.
azyzz228 Dec 5, 2022
7d1ebbd
add 2 test cases for each option
azyzz228 Dec 5, 2022
6141499
add typescript-flow alpha version
azyzz228 Dec 5, 2022
daaf8aa
add test cases for single option none
azyzz228 Dec 7, 2022
50ce08e
add blank docs for new rule
azyzz228 Dec 7, 2022
3321565
change export map to include word 'type' as a key for namespace when …
azyzz228 Dec 7, 2022
8bf8556
add new schema, comments, and handle single config option when it is …
azyzz228 Dec 7, 2022
bb172d6
delete none option and its code
azyzz228 Dec 8, 2022
803dde0
refactor and finish fixer for strict separate optiop
azyzz228 Dec 11, 2022
74ba6d3
add namespace test cases to invalid strict separate
azyzz228 Dec 11, 2022
06705cc
add default type export
azyzz228 Dec 11, 2022
bf6063a
add valid cases for strict separate
azyzz228 Dec 11, 2022
4a6a9bc
finish strict separate option
azyzz228 Dec 11, 2022
08d9832
add strict inline with only import
azyzz228 Dec 11, 2022
4b5e577
add test cases for strict inline with only one import
azyzz228 Dec 11, 2022
8f7239d
add invalid default import test cases for inline option
azyzz228 Dec 13, 2022
7e5388c
add fixer for inline option with ImportDefault specifier
azyzz228 Dec 13, 2022
8c7ff3f
fix one failing test case in default rule
azyzz228 Dec 14, 2022
250a022
add more named import test cases for strict and inline options
azyzz228 Dec 14, 2022
a630161
remove unnecessary comments
azyzz228 Dec 14, 2022
366ac51
added comment check if inline import is supported
azyzz228 Dec 14, 2022
5f79ac6
add check for TS version support for inline type import
azyzz228 Dec 14, 2022
150af6f
add invalid test case with repeated import with namespace to inline o…
azyzz228 Dec 14, 2022
64757e8
add support for namespace in repeated source for inline option
azyzz228 Dec 14, 2022
e05f4c2
add new typescript flow to src/index
azyzz228 Dec 15, 2022
fe60ea1
fix default type import with inline option
azyzz228 Dec 31, 2022
944d967
add TSX file and export/import type cases for typescript flow
azyzz228 Dec 31, 2022
a8d9ba1
add error messages
azyzz228 Jan 8, 2023
051efd2
delete unnecessary comments
azyzz228 Jan 8, 2023
b42c4d5
add 2 default import type test cases
azyzz228 Jan 8, 2023
b4cc95a
add docs
azyzz228 Jan 8, 2023
97d2074
remove custom script
azyzz228 Jan 8, 2023
fc0c163
restore ExportMap to the version from main branch
azyzz228 Jan 8, 2023
c088081
refactor specifier cache loop to function for config = separate
azyzz228 Feb 1, 2023
accfa3a
refactor by adding function for every piece of logic
azyzz228 Feb 2, 2023
a6246b2
remove redundant comments
azyzz228 Feb 14, 2023
05c62b8
Merge branch 'import-js:main' into new-typescript-flow-rule
azyzz228 Sep 29, 2024
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
164 changes: 164 additions & 0 deletions docs/rules/typescript-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@

This rules helps to define type import patterns for the typescript.

## Rule details
This rule allows you to specify how type imports should be written in your TypeScript code. There are two options available: inline type imports and separate type import statements. Rule can also autofix your type imports to the preferred style.

The rule has 2 modes: strict and preference.

Strict option enforces either inline type imports with type modifiers, or separate type imports.

If you select the preference mode, the rule will check for the availability of your preferred import style and use that if possible. If not, it will fall back to your second preferred option.

For example, if you specify the preference as ["inline", "separate"], the rule will try to use inline type imports first. If that is not supported by your TypeScript version, it will use separate type import statements instead.


#### Rule schema

```javascript
// strict mode
"import/typescript-flow": [ 0 | 1 | 2, "inline" | "separate" ]

// preference mode
"import/typescript-flow": [ 0 | 1 | 2, [ "inline", "separate" ] | [ "separate", "inline" ] ]

```

### Strict mode

#### separate

**Definition**: Separate type import statements are written using the import type syntax. They were introduced in TypeScript 3.8

The following patterns are *not* considered warnings:

```javascript
// good1.js

// Separate import type statement
import type { MyType } from "./typescript.ts"
```

```javascript
// good2.js

// Separate import type statement
import type { MyType as Persona, Bar as Foo } from "./typescript.ts"
```
```javascript
// good3.js

// Default type imports are ignored
import type DefaultImport from "./typescript.ts"
```

```javascript
// good4.js

// Default imports are ignored
import DefaultImport from "./typescript.ts"
```

The following patterns are considered warnings:

```javascript
// bad1.js

// type imports must be imported in separate import type statement
import {type MyType,Bar} from "./typescript.ts"

// gets fixed to the following:
import {Bar} from "./typescript.ts"
import type { MyType } from "./typescript.ts"
```

```javascript
// bad2.js

// type imports must be imported in separate import type statement
import {type MyType,type Foo} from "./typescript.ts"

// gets fixed to the following:
import type { MyType, Foo} from "./typescript.ts"
```

```javascript
// bad3.js

// type imports must be imported in separate import type statement
import {type MyType as Persona,Bar,type Foo as Baz} from "./typescript.ts"

// gets fixed to the following:
import {Bar} from "./typescript.ts"
import type { MyType as Persona, Foo as Baz } from "./typescript.ts"
```

#### inline

**Definition**: Inline type imports are written as type modifiers within the curly braces of a regular import statement. They were introduced in TypeScript 4.5.

Patterns that do not raise the warning:

```javascript
// good1.js

// no separate import type statement. inline type import exists
import { type MyType } from "./typescript.ts"
```

```javascript
// good2.js

// no separate import type statement. inline type import exists
import { type MyType, Bar, type Foo as Baz } from "./typescript.ts"
```

Patterns are considered warnings and fixed:

```javascript
// bad1.js

// type imports must be imported inline
import type {MyType} from "./typescript.ts"

// gets fixed to the following:
import {type MyType} from "./typescript.ts"
```

```javascript
// bad1.js

// type imports must be imported inline
import type {MyType, Bar} from "./typescript.ts"

// gets fixed to the following:
import {type MyType, type Bar} from "./typescript.ts"
```

```javascript
// bad3.js

// type imports must be imported inline
import type {MyType, Bar} from "./typescript.ts"
import {Value} from "./typescript.ts"

// Rule can check if there are any other imports from the same source. If yes, then adds inline type imports there. gets fixed to the following:
import {Value, type MyType, type Bar} from "./typescript.ts"
```

### Preference mode

```javascript
// preference mode where inline comes first
"import/typescript-flow": [ 0 | 1 | 2, [ "inline", "separate" ] ]
```
Rule checks if `inline` type modifiers are supported by TypeScript version (must be >=4.5). If supported, then rule operates with `inline` option, if not, then it falls back to `separate` option.

```javascript
// preference mode where strict comes first
"import/typescript-flow": [ 0 | 1 | 2, [ "separate", "inline" ] ]
```

If `separate` comes first in preferred order, then rule will act as if user chose `separate`.

Since `separate` type imports are supported in TypeScript version 3.8 and above, and `inline` type imports are supported in version 4.5 and above, the `separate` option is guaranteed to be supported if the `inline` option is supported. Therefore, there is no need to consider the case where the `separate` option is not supported and the rule falls back to the `inline` option.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,4 @@
"string.prototype.trimend": "^1.0.8",
"tsconfig-paths": "^3.15.0"
}
}
}
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ export const rules = {

// deprecated aliases to rules
'imports-first': require('./rules/imports-first'),

// new typescript flow rule
'typescript-flow': require('./rules/typescript-flow'),
};

export const configs = {
Expand Down
Loading
Loading