mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-04-17 01:49:41 -05:00
Fix: Exclude generator functions from convert-to-class suggestions
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
This commit is contained in:
@@ -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 */
|
||||
|
||||
@@ -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([]);
|
||||
@@ -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([]);
|
||||
@@ -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,
|
||||
}]);
|
||||
Reference in New Issue
Block a user