Skip to content

Commit 4579245

Browse files
authored
fix(50427): allow convert function expressions (microsoft#50430)
1 parent cbc0b17 commit 4579245

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/services/codefixes/convertFunctionToEs6Class.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ namespace ts.codefix {
2222
}
2323

2424
const ctorDeclaration = ctorSymbol.valueDeclaration;
25-
if (isFunctionDeclaration(ctorDeclaration)) {
26-
changes.replaceNode(sourceFile, ctorDeclaration, createClassFromFunctionDeclaration(ctorDeclaration));
25+
if (isFunctionDeclaration(ctorDeclaration) || isFunctionExpression(ctorDeclaration)) {
26+
changes.replaceNode(sourceFile, ctorDeclaration, createClassFromFunction(ctorDeclaration));
2727
}
2828
else if (isVariableDeclaration(ctorDeclaration)) {
2929
const classDeclaration = createClassFromVariableDeclaration(ctorDeclaration);
@@ -233,7 +233,7 @@ namespace ts.codefix {
233233
return cls;
234234
}
235235

236-
function createClassFromFunctionDeclaration(node: FunctionDeclaration): ClassDeclaration {
236+
function createClassFromFunction(node: FunctionDeclaration | FunctionExpression): ClassDeclaration {
237237
const memberElements = createClassElementsFromSymbol(ctorSymbol);
238238
if (node.body) {
239239
memberElements.unshift(factory.createConstructorDeclaration(/*modifiers*/ undefined, node.parameters, node.body));
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @allowNonTsExtensions: true
4+
// @filename: foo.js
5+
////const NS = {};
6+
////NS.Foo = function () {};
7+
////NS.Foo.prototype.m = function () {};
8+
9+
verify.codeFix({
10+
description: "Convert function to an ES2015 class",
11+
newFileContent:
12+
`const NS = {};
13+
NS.Foo = class {
14+
constructor() { }
15+
m() { }
16+
};
17+
`
18+
});

0 commit comments

Comments
 (0)