Skip to content

Commit 1c42fd4

Browse files
authored
fix(33600): disallow convertFunctionToEs6Class Quick Fix for IIFE (microsoft#36580)
1 parent ba4f59f commit 1c42fd4

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

src/services/suggestionDiagnostics.ts

+20-17
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,8 @@ namespace ts {
3737

3838
function check(node: Node) {
3939
if (isJsFile) {
40-
switch (node.kind) {
41-
case SyntaxKind.FunctionExpression:
42-
const decl = getDeclarationOfExpando(node);
43-
if (decl) {
44-
const symbol = decl.symbol;
45-
if (symbol && (symbol.exports && symbol.exports.size || symbol.members && symbol.members.size)) {
46-
diags.push(createDiagnosticForNode(isVariableDeclaration(node.parent) ? node.parent.name : node, Diagnostics.This_constructor_function_may_be_converted_to_a_class_declaration));
47-
break;
48-
}
49-
}
50-
// falls through if no diagnostic was created
51-
case SyntaxKind.FunctionDeclaration:
52-
const symbol = node.symbol;
53-
if (symbol.members && (symbol.members.size > 0)) {
54-
diags.push(createDiagnosticForNode(isVariableDeclaration(node.parent) ? node.parent.name : node, Diagnostics.This_constructor_function_may_be_converted_to_a_class_declaration));
55-
}
56-
break;
40+
if (canBeConvertedToClass(node)) {
41+
diags.push(createDiagnosticForNode(isVariableDeclaration(node.parent) ? node.parent.name : node, Diagnostics.This_constructor_function_may_be_converted_to_a_class_declaration));
5742
}
5843
}
5944
else {
@@ -193,4 +178,22 @@ namespace ts {
193178
function getKeyFromNode(exp: FunctionLikeDeclaration) {
194179
return `${exp.pos.toString()}:${exp.end.toString()}`;
195180
}
181+
182+
function canBeConvertedToClass(node: Node): boolean {
183+
if (node.kind === SyntaxKind.FunctionExpression) {
184+
if (isVariableDeclaration(node.parent) && node.symbol.members?.size) {
185+
return true;
186+
}
187+
188+
const decl = getDeclarationOfExpando(node);
189+
const symbol = decl?.symbol;
190+
return !!(symbol && (symbol.exports?.size || symbol.members?.size));
191+
}
192+
193+
if (node.kind === SyntaxKind.FunctionDeclaration) {
194+
return !!node.symbol.members?.size;
195+
}
196+
197+
return false;
198+
}
196199
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @allowJs: true
4+
5+
// @Filename: /a.js
6+
////(/*1*/function () {
7+
//// const foo = () => {
8+
//// this.x = 10;
9+
//// };
10+
//// foo;
11+
////})();
12+
13+
goTo.marker("1");
14+
verify.not.codeFixAvailable()

0 commit comments

Comments
 (0)