From 06d2461cd0013b1a046c84c577698547a3693398 Mon Sep 17 00:00:00 2001 From: Jesse Trinity Date: Fri, 29 May 2020 11:11:09 -0700 Subject: [PATCH] convert import trigger reason only --- src/services/refactors/convertImport.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/services/refactors/convertImport.ts b/src/services/refactors/convertImport.ts index 43120591e6b..88c501925c1 100644 --- a/src/services/refactors/convertImport.ts +++ b/src/services/refactors/convertImport.ts @@ -5,7 +5,7 @@ namespace ts.refactor { const actionNameNamedToNamespace = "Convert named imports to namespace import"; registerRefactor(refactorName, { getAvailableActions(context): readonly ApplicableRefactorInfo[] { - const i = getImportToConvert(context); + const i = getImportToConvert(context, context.triggerReason === "invoked"); if (!i) return emptyArray; const description = i.kind === SyntaxKind.NamespaceImport ? Diagnostics.Convert_namespace_import_to_named_imports.message : Diagnostics.Convert_named_imports_to_namespace_import.message; const actionName = i.kind === SyntaxKind.NamespaceImport ? actionNameNamespaceToNamed : actionNameNamedToNamespace; @@ -19,11 +19,12 @@ namespace ts.refactor { }); // Can convert imports of the form `import * as m from "m";` or `import d, { x, y } from "m";`. - function getImportToConvert(context: RefactorContext): NamedImportBindings | undefined { + function getImportToConvert(context: RefactorContext, userRequested = true): NamedImportBindings | undefined { const { file } = context; const span = getRefactorContextSpan(context); const token = getTokenAtPosition(file, span.start); - const importDecl = findAncestor(token, isImportDeclaration); + const cursorRequest = userRequested && span.length === 0; + const importDecl = cursorRequest ? findAncestor(token, isImportDeclaration) : getParentNodeInSpan(token, file, span); if (!importDecl || !isImportDeclaration(importDecl) || (importDecl.getEnd() < span.start + span.length)) return undefined; const { importClause } = importDecl; return importClause && importClause.namedBindings;