fix(33600): disallow convertFunctionToEs6Class Quick Fix for IIFE (#36580)

This commit is contained in:
Alexander T 2020-02-03 23:52:08 +02:00 committed by GitHub
parent ba4f59fc1a
commit 1c42fd4bdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 17 deletions

View File

@ -37,23 +37,8 @@ namespace ts {
function check(node: Node) {
if (isJsFile) {
switch (node.kind) {
case SyntaxKind.FunctionExpression:
const decl = getDeclarationOfExpando(node);
if (decl) {
const symbol = decl.symbol;
if (symbol && (symbol.exports && symbol.exports.size || symbol.members && symbol.members.size)) {
diags.push(createDiagnosticForNode(isVariableDeclaration(node.parent) ? node.parent.name : node, Diagnostics.This_constructor_function_may_be_converted_to_a_class_declaration));
break;
}
}
// falls through if no diagnostic was created
case SyntaxKind.FunctionDeclaration:
const symbol = node.symbol;
if (symbol.members && (symbol.members.size > 0)) {
diags.push(createDiagnosticForNode(isVariableDeclaration(node.parent) ? node.parent.name : node, Diagnostics.This_constructor_function_may_be_converted_to_a_class_declaration));
}
break;
if (canBeConvertedToClass(node)) {
diags.push(createDiagnosticForNode(isVariableDeclaration(node.parent) ? node.parent.name : node, Diagnostics.This_constructor_function_may_be_converted_to_a_class_declaration));
}
}
else {
@ -193,4 +178,22 @@ namespace ts {
function getKeyFromNode(exp: FunctionLikeDeclaration) {
return `${exp.pos.toString()}:${exp.end.toString()}`;
}
function canBeConvertedToClass(node: Node): boolean {
if (node.kind === SyntaxKind.FunctionExpression) {
if (isVariableDeclaration(node.parent) && node.symbol.members?.size) {
return true;
}
const decl = getDeclarationOfExpando(node);
const symbol = decl?.symbol;
return !!(symbol && (symbol.exports?.size || symbol.members?.size));
}
if (node.kind === SyntaxKind.FunctionDeclaration) {
return !!node.symbol.members?.size;
}
return false;
}
}

View File

@ -0,0 +1,14 @@
/// <reference path='fourslash.ts' />
// @allowJs: true
// @Filename: /a.js
////(/*1*/function () {
//// const foo = () => {
//// this.x = 10;
//// };
//// foo;
////})();
goTo.marker("1");
verify.not.codeFixAvailable()