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

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 14, 2025

Generator functions (function*) were incorrectly triggering the TS80002 diagnostic "This constructor function may be converted to a class declaration". This is incorrect because generator functions have special semantics that are incompatible with class constructors.

Problem:

function* gen() {}
gen.prototype.next = gen.prototype.next;
gen.prototype.return = gen.prototype.return;

The above code would incorrectly trigger the convert-to-class suggestion, even though generator functions cannot be meaningfully converted to classes.

Solution:
Added checks in the canBeConvertedToClass function to exclude generator functions by testing for FunctionFlags.Generator in both function declaration and function expression paths.

Changes:

  • Modified src/services/suggestionDiagnostics.ts to exclude generator functions
  • Added comprehensive test coverage for generator declarations, generator expressions, and mixed scenarios
  • Verified that regular constructor functions continue to work correctly

Testing:

  • All existing tests pass
  • New tests verify generator functions don't trigger the diagnostic
  • Regular constructor functions still correctly trigger the diagnostic when appropriate

Fixes #62056.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@typescript-bot typescript-bot added the For Milestone Bug PRs that fix a bug with a specific milestone label Jul 14, 2025
Copy link
Member

@RyanCavanaugh RyanCavanaugh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Run npx hereby format and commit that

@Copilot Copilot AI changed the title [WIP] Generator functions trigger TS80002 Fix: Exclude generator functions from convert-to-class suggestions Jul 14, 2025
@Copilot Copilot AI requested a review from RyanCavanaugh July 14, 2025 17:31
Copilot finished work on behalf of RyanCavanaugh July 14, 2025 17:31
Copy link
Contributor Author

Copilot AI commented Jul 14, 2025

Run npx hereby format and commit that

Ran npx hereby format - no formatting changes were needed as the code is already properly formatted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generator functions trigger TS80002
3 participants