Skip to content

Commit

Permalink
feat: add "ignoreTypeDefault" option to "type-import-style" (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
zertosh authored and gajus committed Apr 3, 2019
1 parent 5adb546 commit 6748dc5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
6 changes: 6 additions & 0 deletions .README/rules/type-import-style.md
Expand Up @@ -12,11 +12,17 @@ import {type T, type U, type V} from '...';
import type {T, U, V} from '...';
```

#### Options

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.

This rule has an object option:

* `ignoreTypeDefault` - if `true`, when in "identifier" mode, default type imports will be ignored. Default is `false`.

<!-- assertions typeImportStyle -->
58 changes: 40 additions & 18 deletions src/rules/typeImportStyle.js
Expand Up @@ -2,6 +2,15 @@ const schema = [
{
enum: ['declaration', 'identifier'],
type: 'string'
},
{
additionalProperties: false,
properties: {
ignoreTypeDefault: {
type: 'boolean'
}
},
type: 'object'
}
];

Expand All @@ -23,28 +32,41 @@ const create = (context) => {
};
} else {
// Default to 'identifier'
const ignoreTypeDefault = context.options[1] &&
context.options[1].ignoreTypeDefault;

return {
ImportDeclaration (node) {
if (node.importKind === 'type') {
context.report({
fix (fixer) {
const imports = node.specifiers.map((specifier) => {
if (specifier.type === 'ImportDefaultSpecifier') {
return 'type default as ' + specifier.local.name;
} else if (specifier.imported.name === specifier.local.name) {
return 'type ' + specifier.local.name;
} else {
return 'type ' + specifier.imported.name + ' as ' + specifier.local.name;
}
});
const source = node.source.value;
if (node.importKind !== 'type') {
return;
}

return fixer.replaceText(node, 'import {' + imports.join(', ') + '} from \'' + source + '\';');
},
message: 'Unexpected "import type"',
node
});
if (
ignoreTypeDefault &&
node.specifiers[0] &&
node.specifiers[0].type === 'ImportDefaultSpecifier'
) {
return;
}

context.report({
fix (fixer) {
const imports = node.specifiers.map((specifier) => {
if (specifier.type === 'ImportDefaultSpecifier') {
return 'type default as ' + specifier.local.name;
} else if (specifier.imported.name === specifier.local.name) {
return 'type ' + specifier.local.name;
} else {
return 'type ' + specifier.imported.name + ' as ' + specifier.local.name;
}
});
const source = node.source.value;

return fixer.replaceText(node, 'import {' + imports.join(', ') + '} from \'' + source + '\';');
},
message: 'Unexpected "import type"',
node
});
}
};
}
Expand Down
8 changes: 8 additions & 0 deletions tests/rules/assertions/typeImportStyle.js
Expand Up @@ -43,6 +43,14 @@ export default {
{
code: 'import type {A, B} from \'a\';',
options: ['declaration']
},
{
code: 'import typeof * as A from \'a\';',
options: ['identifier']
},
{
code: 'import type A from \'a\';',
options: ['identifier', {ignoreTypeDefault: true}]
}
]
};
Expand Down

0 comments on commit 6748dc5

Please sign in to comment.