mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-09 16:39:46 -05:00
containsTopLevelCommonjs:handle uninitialised vars (#27642)
Previously it assumed that all variable declarations had an initialiser, which is not correct.
This commit is contained in:
committed by
GitHub
parent
74392a0e51
commit
53906f222f
@@ -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);
|
||||
|
||||
@@ -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);`,
|
||||
});
|
||||
Reference in New Issue
Block a user