diff --git a/src/services/suggestionDiagnostics.ts b/src/services/suggestionDiagnostics.ts index 80fe528e791f1..c38257994c901 100644 --- a/src/services/suggestionDiagnostics.ts +++ b/src/services/suggestionDiagnostics.ts @@ -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; } @@ -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; } diff --git a/tests/cases/fourslash/convertFunctionToEs6Class_generatorExpression_falsePositive.ts b/tests/cases/fourslash/convertFunctionToEs6Class_generatorExpression_falsePositive.ts new file mode 100644 index 0000000000000..87c82f1789551 --- /dev/null +++ b/tests/cases/fourslash/convertFunctionToEs6Class_generatorExpression_falsePositive.ts @@ -0,0 +1,10 @@ +/// + +// @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([]); \ No newline at end of file diff --git a/tests/cases/fourslash/convertFunctionToEs6Class_generator_falsePositive.ts b/tests/cases/fourslash/convertFunctionToEs6Class_generator_falsePositive.ts new file mode 100644 index 0000000000000..77390d67162eb --- /dev/null +++ b/tests/cases/fourslash/convertFunctionToEs6Class_generator_falsePositive.ts @@ -0,0 +1,10 @@ +/// + +// @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([]); \ No newline at end of file diff --git a/tests/cases/fourslash/convertFunctionToEs6Class_regularVsGenerator.ts b/tests/cases/fourslash/convertFunctionToEs6Class_regularVsGenerator.ts new file mode 100644 index 0000000000000..0d0c9653e61f1 --- /dev/null +++ b/tests/cases/fourslash/convertFunctionToEs6Class_regularVsGenerator.ts @@ -0,0 +1,17 @@ +/// + +// @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, +}]); \ No newline at end of file