Skip to content

Commit 55f9a6f

Browse files
author
Andy
authored
Fix bug: VariableDeclaration initiazer may be undefined (#23439)
1 parent bc285aa commit 55f9a6f

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

src/services/codefixes/convertToEs6Module.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,23 +130,23 @@ namespace ts.codefix {
130130
let foundImport = false;
131131
const newNodes = flatMap(declarationList.declarations, decl => {
132132
const { name, initializer } = decl;
133-
if (isExportsOrModuleExportsOrAlias(sourceFile, initializer)) {
134-
// `const alias = module.exports;` can be removed.
135-
foundImport = true;
136-
return [];
137-
}
138-
if (isRequireCall(initializer, /*checkArgumentIsStringLiteralLike*/ true)) {
139-
foundImport = true;
140-
return convertSingleImport(sourceFile, name, initializer.arguments[0], changes, checker, identifiers, target);
141-
}
142-
else if (isPropertyAccessExpression(initializer) && isRequireCall(initializer.expression, /*checkArgumentIsStringLiteralLike*/ true)) {
143-
foundImport = true;
144-
return convertPropertyAccessImport(name, initializer.name.text, initializer.expression.arguments[0], identifiers);
145-
}
146-
else {
147-
// Move it out to its own variable statement. (This will not be used if `!foundImport`)
148-
return createVariableStatement(/*modifiers*/ undefined, createVariableDeclarationList([decl], declarationList.flags));
133+
if (initializer) {
134+
if (isExportsOrModuleExportsOrAlias(sourceFile, initializer)) {
135+
// `const alias = module.exports;` can be removed.
136+
foundImport = true;
137+
return [];
138+
}
139+
else if (isRequireCall(initializer, /*checkArgumentIsStringLiteralLike*/ true)) {
140+
foundImport = true;
141+
return convertSingleImport(sourceFile, name, initializer.arguments[0], changes, checker, identifiers, target);
142+
}
143+
else if (isPropertyAccessExpression(initializer) && isRequireCall(initializer.expression, /*checkArgumentIsStringLiteralLike*/ true)) {
144+
foundImport = true;
145+
return convertPropertyAccessImport(name, initializer.name.text, initializer.expression.arguments[0], identifiers);
146+
}
149147
}
148+
// Move it out to its own variable statement. (This will not be used if `!foundImport`)
149+
return createVariableStatement(/*modifiers*/ undefined, createVariableDeclarationList([decl], declarationList.flags));
150150
});
151151
if (foundImport) {
152152
// useNonAdjustedEndPosition to ensure we don't eat the newline after the statement.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @allowJs: true
4+
5+
// @Filename: /a.js
6+
////require("m");
7+
////let x; x;
8+
9+
verify.codeFix({
10+
description: "Convert to ES6 module",
11+
newFileContent:
12+
`import "m";
13+
let x; x;`
14+
});

0 commit comments

Comments
 (0)