From d7ccd0ddefbe6589d60f908c90efe453e7604c50 Mon Sep 17 00:00:00 2001 From: Dan Wang Date: Thu, 22 Feb 2018 08:55:58 -0800 Subject: [PATCH] fix: (type-import-style) Support import aliases when fixing (#321) --- src/rules/typeImportStyle.js | 8 +++++++- tests/rules/assertions/typeImportStyle.js | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/rules/typeImportStyle.js b/src/rules/typeImportStyle.js index 1d14eaa4..4202429e 100644 --- a/src/rules/typeImportStyle.js +++ b/src/rules/typeImportStyle.js @@ -29,7 +29,13 @@ const create = (context) => { context.report({ fix (fixer) { const imports = node.specifiers.map((specifier) => { - return 'type ' + specifier.local.name; + 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; diff --git a/tests/rules/assertions/typeImportStyle.js b/tests/rules/assertions/typeImportStyle.js index 46d5e85a..1117b2da 100644 --- a/tests/rules/assertions/typeImportStyle.js +++ b/tests/rules/assertions/typeImportStyle.js @@ -11,6 +11,18 @@ export default { options: ['identifier'], output: 'import {type A, type B} from \'a\';' }, + { + code: 'import type {A, B as C} from \'a\';', + errors: [{message: 'Unexpected "import type"'}], + options: ['identifier'], + output: 'import {type A, type B as C} from \'a\';' + }, + { + code: 'import type A from \'a\';', + errors: [{message: 'Unexpected "import type"'}], + options: ['identifier'], + output: 'import {type default as A} from \'a\';' + }, { code: 'import {type A, type B} from \'a\';', errors: [