Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
40 changes: 25 additions & 15 deletions src/services/suggestionDiagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
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,
}]);