From 1baae421490015946c5397cb428d47ef2be3f681 Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Fri, 9 Feb 2018 11:15:13 -0800 Subject: [PATCH] Handle variable declaration without initializer in Convert to ES6 Module Fixes #21786 --- src/services/refactors/convertToEs6Module.ts | 9 ++++++--- ...factorConvertToEs6Module_triggers_noInitializer.ts | 11 +++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 tests/cases/fourslash/refactorConvertToEs6Module_triggers_noInitializer.ts diff --git a/src/services/refactors/convertToEs6Module.ts b/src/services/refactors/convertToEs6Module.ts index 65f9f58ccc4..f1f3c16b6bb 100644 --- a/src/services/refactors/convertToEs6Module.ts +++ b/src/services/refactors/convertToEs6Module.ts @@ -33,14 +33,17 @@ namespace ts.refactor { return isExportsOrModuleExportsOrAlias(sourceFile, node as PropertyAccessExpression) || isExportsOrModuleExportsOrAlias(sourceFile, (node as PropertyAccessExpression).expression); case SyntaxKind.VariableDeclarationList: - const decl = firstOrUndefined((node as VariableDeclarationList).declarations); - return !!decl && isExportsOrModuleExportsOrAlias(sourceFile, decl.initializer); + return isVariableDeclarationTriggerLocation(firstOrUndefined((node as VariableDeclarationList).declarations)); case SyntaxKind.VariableDeclaration: - return isExportsOrModuleExportsOrAlias(sourceFile, (node as VariableDeclaration).initializer); + return isVariableDeclarationTriggerLocation(node as VariableDeclaration); default: return isExpression(node) && isExportsOrModuleExportsOrAlias(sourceFile, node) || !onSecondTry && isAtTriggerLocation(sourceFile, node.parent, /*onSecondTry*/ true); } + + function isVariableDeclarationTriggerLocation(decl: VariableDeclaration | undefined) { + return !!decl && !!decl.initializer && isExportsOrModuleExportsOrAlias(sourceFile, decl.initializer); + } } function isAtTopLevelRequire(call: CallExpression): boolean { diff --git a/tests/cases/fourslash/refactorConvertToEs6Module_triggers_noInitializer.ts b/tests/cases/fourslash/refactorConvertToEs6Module_triggers_noInitializer.ts new file mode 100644 index 00000000000..23cbdd12aee --- /dev/null +++ b/tests/cases/fourslash/refactorConvertToEs6Module_triggers_noInitializer.ts @@ -0,0 +1,11 @@ +/// + +// @allowJs: true + +// @Filename: /a.js +/////*a*/const/*b*/ alias; +////require("x"); + +goTo.select("a", "b"); +verify.not.refactorAvailable("Convert to ES6 module"); +