From 6bd26cd2e5c424963d4b8c03240986184f155f56 Mon Sep 17 00:00:00 2001 From: BigAru Date: Fri, 19 Oct 2018 12:43:12 +0200 Subject: [PATCH] extract creation of funcDeclaration --- ...onvertArrowFunctionOrFunctionExpression.ts | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/services/refactors/convertArrowFunctionOrFunctionExpression.ts b/src/services/refactors/convertArrowFunctionOrFunctionExpression.ts index 37a89454a3..ae70320b1b 100644 --- a/src/services/refactors/convertArrowFunctionOrFunctionExpression.ts +++ b/src/services/refactors/convertArrowFunctionOrFunctionExpression.ts @@ -82,29 +82,24 @@ namespace ts.refactor.convertArrowFunctionOrFunctionExpression { const variableDeclaration = func.parent; if (!isVariableDeclaration(variableDeclaration) || !isVariableDeclarationInVariableStatement(variableDeclaration) || !isIdentifier(variableDeclaration.name)) return undefined; - const varDeclList = findAncestor(variableDeclaration, n => n.kind === SyntaxKind.VariableDeclarationList)!; + const varDeclList = findAncestor(variableDeclaration, n => isVariableDeclarationList(n))!; if (!isVariableDeclarationList(varDeclList)) return undefined; - const statement = findAncestor(variableDeclaration, n => n.kind === SyntaxKind.VariableStatement)!; + const statement = findAncestor(variableDeclaration, n => isVariableStatement(n))!; if (!isVariableStatement(statement)) return undefined; - if (varDeclList.declarations.length === 0) return undefined; - if (varDeclList.declarations.length === 1) { + newNode = makeFuncDecl(func, statement, variableDeclaration.name, body); - const newNode = createFunctionDeclaration(func.decorators, statement.modifiers, func.asteriskToken, variableDeclaration.name, func.typeParameters, func.parameters, func.type, body); - const edits = textChanges.ChangeTracker.with(context, t => t.replaceNode(file, statement, newNode)); - return { renameFilename: undefined, renameLocation: undefined, edits }; + if (varDeclList.declarations.length === 1) { + edits = textChanges.ChangeTracker.with(context, t => t.replaceNode(file, statement, newNode)); } else { - const newNode = createFunctionDeclaration(func.decorators, statement.modifiers, func.asteriskToken, variableDeclaration.name, func.typeParameters, func.parameters, func.type, body); - - const edits = textChanges.ChangeTracker.with(context, t => { + edits = textChanges.ChangeTracker.with(context, t => { t.delete(file, variableDeclaration); t.insertNodeAfter(file, statement, newNode); }); - return { renameFilename: undefined, renameLocation: undefined, edits }; } - + break; case toArrowFunctionActionName: @@ -132,8 +127,6 @@ namespace ts.refactor.convertArrowFunctionOrFunctionExpression { } return { renameFilename: undefined, renameLocation: undefined, edits }; - - } function getInfo(file: SourceFile, startPosition: number): Info | undefined { @@ -167,4 +160,16 @@ namespace ts.refactor.convertArrowFunctionOrFunctionExpression { return func.body; } } + + function makeFuncDecl(func: FunctionExpression | ArrowFunction, statement: VariableStatement, name: Identifier, body: Block) { + return createFunctionDeclaration( + func.decorators, + statement.modifiers, + func.asteriskToken, + name, + func.typeParameters, + func.parameters, + func.type, + body); + } }