Fix: Exclude generator functions from convert-to-class suggestions

Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-07-14 17:13:53 +00:00
parent 277ae43411
commit 6f002ef7f9
4 changed files with 62 additions and 15 deletions

View File

@@ -289,21 +289,31 @@ function getKeyFromNode(exp: FunctionLikeDeclaration) {
return `${exp.pos.toString()}:${exp.end.toString()}`;
}
function canBeConvertedToClass(node: Node, checker: TypeChecker): boolean {
if (isFunctionExpression(node)) {
if (isVariableDeclaration(node.parent) && node.symbol.members?.size) {
return true;
}
const symbol = checker.getSymbolOfExpando(node, /*allowDeclaration*/ false);
return !!(symbol && (symbol.exports?.size || symbol.members?.size));
}
if (isFunctionDeclaration(node)) {
return !!node.symbol.members?.size;
}
return false;
function canBeConvertedToClass(node: Node, checker: TypeChecker): boolean {
if (isFunctionExpression(node)) {
// Generator functions cannot be converted to classes
if (getFunctionFlags(node) & FunctionFlags.Generator) {
return false;
}
if (isVariableDeclaration(node.parent) && node.symbol.members?.size) {
return true;
}
const symbol = checker.getSymbolOfExpando(node, /*allowDeclaration*/ false);
return !!(symbol && (symbol.exports?.size || symbol.members?.size));
}
if (isFunctionDeclaration(node)) {
// Generator functions cannot be converted to classes
if (getFunctionFlags(node) & FunctionFlags.Generator) {
return false;
}
return !!node.symbol.members?.size;
}
return false;
}
/** @internal */

View File

@@ -0,0 +1,10 @@
/// <reference path='fourslash.ts' />
// @allowJs: true
// @Filename: /a.js
////const gen = function*() {};
////gen.prototype.next = gen.prototype.next;
////gen.prototype.return = gen.prototype.return;
// Generator function expressions should not trigger the convert-to-class suggestion
verify.getSuggestionDiagnostics([]);

View File

@@ -0,0 +1,10 @@
/// <reference path='fourslash.ts' />
// @allowJs: true
// @Filename: /a.js
////function* gen() {}
////gen.prototype.next = gen.prototype.next;
////gen.prototype.return = gen.prototype.return;
// Generator functions should not trigger the convert-to-class suggestion
verify.getSuggestionDiagnostics([]);

View File

@@ -0,0 +1,17 @@
/// <reference path='fourslash.ts' />
// @allowJs: true
// @Filename: /a.js
////function [|regular|]() {}
////regular.prototype.method = function() { this.x = 1; };
////
////function* gen() {}
////gen.prototype.next = gen.prototype.next;
////gen.prototype.return = gen.prototype.return;
// Regular constructor functions should still trigger the convert-to-class suggestion
// but generator functions should not
verify.getSuggestionDiagnostics([{
message: "This constructor function may be converted to a class declaration.",
code: 80002,
}]);