Skip to content
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

fix(50427): Convert to class fails #50430

Merged
merged 1 commit into from Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/services/codefixes/convertFunctionToEs6Class.ts
Expand Up @@ -22,8 +22,8 @@ namespace ts.codefix {
}

const ctorDeclaration = ctorSymbol.valueDeclaration;
if (isFunctionDeclaration(ctorDeclaration)) {
changes.replaceNode(sourceFile, ctorDeclaration, createClassFromFunctionDeclaration(ctorDeclaration));
if (isFunctionDeclaration(ctorDeclaration) || isFunctionExpression(ctorDeclaration)) {
changes.replaceNode(sourceFile, ctorDeclaration, createClassFromFunction(ctorDeclaration));
}
else if (isVariableDeclaration(ctorDeclaration)) {
const classDeclaration = createClassFromVariableDeclaration(ctorDeclaration);
Expand Down Expand Up @@ -233,7 +233,7 @@ namespace ts.codefix {
return cls;
}

function createClassFromFunctionDeclaration(node: FunctionDeclaration): ClassDeclaration {
function createClassFromFunction(node: FunctionDeclaration | FunctionExpression): ClassDeclaration {
const memberElements = createClassElementsFromSymbol(ctorSymbol);
if (node.body) {
memberElements.unshift(factory.createConstructorDeclaration(/*modifiers*/ undefined, node.parameters, node.body));
Expand Down
18 changes: 18 additions & 0 deletions tests/cases/fourslash/convertFunctionToEs6Class14.ts
@@ -0,0 +1,18 @@
/// <reference path="fourslash.ts" />

// @allowNonTsExtensions: true
// @filename: foo.js
////const NS = {};
////NS.Foo = function () {};
////NS.Foo.prototype.m = function () {};

verify.codeFix({
description: "Convert function to an ES2015 class",
newFileContent:
`const NS = {};
NS.Foo = class {
constructor() { }
m() { }
};
`
});