Skip to content

Handle variable declaration without initializer in Convert to ES6 Module #21835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/services/refactors/convertToEs6Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path='fourslash.ts' />

// @allowJs: true

// @Filename: /a.js
/////*a*/const/*b*/ alias;
////require("x");

goTo.select("a", "b");
verify.not.refactorAvailable("Convert to ES6 module");