containsTopLevelCommonjs:handle uninitialised vars (#27642)

Previously it assumed that all variable declarations had an initialiser,
which is not correct.
This commit is contained in:
Nathan Shively-Sanders
2018-10-09 13:17:54 -07:00
committed by GitHub
parent 74392a0e51
commit 53906f222f
2 changed files with 25 additions and 1 deletions

View File

@@ -82,7 +82,7 @@ namespace ts {
switch (statement.kind) {
case SyntaxKind.VariableStatement:
return (statement as VariableStatement).declarationList.declarations.some(decl =>
isRequireCall(propertyAccessLeftHandSide(decl.initializer!), /*checkArgumentIsStringLiteralLike*/ true)); // TODO: GH#18217
!!decl.initializer && isRequireCall(propertyAccessLeftHandSide(decl.initializer), /*checkArgumentIsStringLiteralLike*/ true));
case SyntaxKind.ExpressionStatement: {
const { expression } = statement as ExpressionStatement;
if (!isBinaryExpression(expression)) return isRequireCall(expression, /*checkArgumentIsStringLiteralLike*/ true);

View File

@@ -0,0 +1,24 @@
/// <reference path='fourslash.ts' />
// @allowJs: true
// @target: esnext
// @Filename: /a.js
////var privateUnrelated;
////[|exports.f|] = function() {};
////privateUnrelated = 1;
////console.log(privateUnrelated);
verify.getSuggestionDiagnostics([{
message: "File is a CommonJS module; it may be converted to an ES6 module.",
code: 80001,
}]);
verify.codeFix({
description: "Convert to ES6 module",
newFileContent:
`var privateUnrelated;
export function f() {}
privateUnrelated = 1;
console.log(privateUnrelated);`,
});