Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/services/suggestionDiagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ function getKeyFromNode(exp: FunctionLikeDeclaration) {

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;
}
Expand All @@ -300,6 +305,11 @@ function canBeConvertedToClass(node: Node, checker: TypeChecker): boolean {
}

if (isFunctionDeclaration(node)) {
// Generator functions cannot be converted to classes
if (getFunctionFlags(node) & FunctionFlags.Generator) {
return false;
}

return !!node.symbol.members?.size;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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([]);
Original file line number Diff line number Diff line change
@@ -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([]);
Original file line number Diff line number Diff line change
@@ -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,
}]);
Loading