diff --git a/src/services/suggestionDiagnostics.ts b/src/services/suggestionDiagnostics.ts index e95406a463592..5767bad6a13f6 100644 --- a/src/services/suggestionDiagnostics.ts +++ b/src/services/suggestionDiagnostics.ts @@ -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; + } } diff --git a/tests/cases/fourslash/convertFunctionToEs6Class_noQuickInfoForIIFE.ts b/tests/cases/fourslash/convertFunctionToEs6Class_noQuickInfoForIIFE.ts new file mode 100644 index 0000000000000..9211fecd911c3 --- /dev/null +++ b/tests/cases/fourslash/convertFunctionToEs6Class_noQuickInfoForIIFE.ts @@ -0,0 +1,14 @@ +/// + +// @allowJs: true + +// @Filename: /a.js +////(/*1*/function () { +//// const foo = () => { +//// this.x = 10; +//// }; +//// foo; +////})(); + +goTo.marker("1"); +verify.not.codeFixAvailable()