Skip to content
Permalink
Browse files
fix(50427): allow convert function expressions (#50430)
  • Loading branch information
a-tarasyuk committed Aug 26, 2022
1 parent cbc0b17 commit 4579245f36d9c3deccb097da9d5545f9c5c1ab26
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
@@ -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);
@@ -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));
@@ -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() { }
};
`
});

0 comments on commit 4579245

Please sign in to comment.