-
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add type-import-style rule (#320)
- Loading branch information
Showing
6 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
### `type-import-style` | ||
|
||
_The `--fix` option on the command line automatically fixes problems reported by this rule._ | ||
|
||
Enforces a particular style for type imports: | ||
|
||
``` | ||
// 'identifier' style | ||
import {type T, type U, type V} from '...'; | ||
// 'declaration' style | ||
import type {T, U, V} from '...'; | ||
``` | ||
|
||
The rule has a string option: | ||
|
||
* `"identifier"` (default): Enforces that type imports are all in the | ||
'identifier' style. | ||
* `"declaration"`: Enforces that type imports are all in the 'declaration' | ||
style. | ||
|
||
<!-- assertions typeImportStyle --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const schema = [ | ||
{ | ||
enum: ['declaration', 'identifier'], | ||
type: 'string' | ||
} | ||
]; | ||
|
||
const create = (context) => { | ||
if (context.options[0] === 'declaration') { | ||
return { | ||
ImportDeclaration (node) { | ||
if (node.importKind !== 'type') { | ||
node.specifiers.forEach((specifier) => { | ||
if (specifier.importKind === 'type') { | ||
context.report({ | ||
message: 'Unexpected type import', | ||
node | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
}; | ||
} else { | ||
// Default to 'identifier' | ||
return { | ||
ImportDeclaration (node) { | ||
if (node.importKind === 'type') { | ||
context.report({ | ||
fix (fixer) { | ||
const imports = node.specifiers.map((specifier) => { | ||
return 'type ' + specifier.local.name; | ||
}); | ||
const source = node.source.value; | ||
|
||
return fixer.replaceText(node, | ||
'import {' + imports.join(', ') + '} from \'' + source + '\';' | ||
); | ||
}, | ||
message: 'Unexpected "import type"', | ||
node | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
}; | ||
|
||
export default { | ||
create, | ||
schema | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
export default { | ||
invalid: [ | ||
{ | ||
code: 'import type {A, B} from \'a\';', | ||
errors: [{message: 'Unexpected "import type"'}], | ||
output: 'import {type A, type B} from \'a\';' | ||
}, | ||
{ | ||
code: 'import type {A, B} from \'a\';', | ||
errors: [{message: 'Unexpected "import type"'}], | ||
options: ['identifier'], | ||
output: 'import {type A, type B} from \'a\';' | ||
}, | ||
{ | ||
code: 'import {type A, type B} from \'a\';', | ||
errors: [ | ||
{message: 'Unexpected type import'}, | ||
{message: 'Unexpected type import'} | ||
], | ||
options: ['declaration'] | ||
} | ||
], | ||
valid: [ | ||
{ | ||
code: 'import {type A, type B} from \'a\';' | ||
}, | ||
{ | ||
code: 'import {type A, type B} from \'a\';', | ||
options: ['identifier'] | ||
}, | ||
{ | ||
code: 'import type {A, B} from \'a\';', | ||
options: ['declaration'] | ||
} | ||
] | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters