mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-10 18:04:18 -05:00
fix(50427): allow convert function expressions (#50430)
This commit is contained in:
@@ -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));
|
||||
|
||||
18
tests/cases/fourslash/convertFunctionToEs6Class14.ts
Normal file
18
tests/cases/fourslash/convertFunctionToEs6Class14.ts
Normal file
@@ -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() { }
|
||||
};
|
||||
`
|
||||
});
|
||||
Reference in New Issue
Block a user