mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 16:38:05 -06:00
fix(33600): disallow convertFunctionToEs6Class Quick Fix for IIFE (#36580)
This commit is contained in:
parent
ba4f59fc1a
commit
1c42fd4bdf
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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()
|
||||
Loading…
x
Reference in New Issue
Block a user