Fix bug: VariableDeclaration initiazer may be undefined (#23439)

This commit is contained in:
Andy
2018-04-17 08:59:07 -07:00
committed by GitHub
parent bc285aa9a1
commit 55f9a6ffc2
2 changed files with 30 additions and 16 deletions
+16 -16
View File
@@ -130,23 +130,23 @@ namespace ts.codefix {
let foundImport = false;
const newNodes = flatMap(declarationList.declarations, decl => {
const { name, initializer } = decl;
if (isExportsOrModuleExportsOrAlias(sourceFile, initializer)) {
// `const alias = module.exports;` can be removed.
foundImport = true;
return [];
}
if (isRequireCall(initializer, /*checkArgumentIsStringLiteralLike*/ true)) {
foundImport = true;
return convertSingleImport(sourceFile, name, initializer.arguments[0], changes, checker, identifiers, target);
}
else if (isPropertyAccessExpression(initializer) && isRequireCall(initializer.expression, /*checkArgumentIsStringLiteralLike*/ true)) {
foundImport = true;
return convertPropertyAccessImport(name, initializer.name.text, initializer.expression.arguments[0], identifiers);
}
else {
// Move it out to its own variable statement. (This will not be used if `!foundImport`)
return createVariableStatement(/*modifiers*/ undefined, createVariableDeclarationList([decl], declarationList.flags));
if (initializer) {
if (isExportsOrModuleExportsOrAlias(sourceFile, initializer)) {
// `const alias = module.exports;` can be removed.
foundImport = true;
return [];
}
else if (isRequireCall(initializer, /*checkArgumentIsStringLiteralLike*/ true)) {
foundImport = true;
return convertSingleImport(sourceFile, name, initializer.arguments[0], changes, checker, identifiers, target);
}
else if (isPropertyAccessExpression(initializer) && isRequireCall(initializer.expression, /*checkArgumentIsStringLiteralLike*/ true)) {
foundImport = true;
return convertPropertyAccessImport(name, initializer.name.text, initializer.expression.arguments[0], identifiers);
}
}
// Move it out to its own variable statement. (This will not be used if `!foundImport`)
return createVariableStatement(/*modifiers*/ undefined, createVariableDeclarationList([decl], declarationList.flags));
});
if (foundImport) {
// useNonAdjustedEndPosition to ensure we don't eat the newline after the statement.
@@ -0,0 +1,14 @@
/// <reference path='fourslash.ts' />
// @allowJs: true
// @Filename: /a.js
////require("m");
////let x; x;
verify.codeFix({
description: "Convert to ES6 module",
newFileContent:
`import "m";
let x; x;`
});