From 367f47e75501c616626facff002fcc4aa2c274aa Mon Sep 17 00:00:00 2001 From: BigAru Date: Thu, 18 Oct 2018 08:45:19 +0200 Subject: [PATCH] fix multi decl bug --- .../convertArrowFunctionOrFunction.ts | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/services/refactors/convertArrowFunctionOrFunction.ts b/src/services/refactors/convertArrowFunctionOrFunction.ts index cae45c18880..4d05dbaac1e 100644 --- a/src/services/refactors/convertArrowFunctionOrFunction.ts +++ b/src/services/refactors/convertArrowFunctionOrFunction.ts @@ -102,11 +102,29 @@ namespace ts.refactor.convertArrowFunctionOrFunction { const variableDeclaration = func.parent; if (isVariableDeclaration(variableDeclaration) && isVariableDeclarationInVariableStatement(variableDeclaration) && isIdentifier(variableDeclaration.name)) { - const statement = findAncestor(variableDeclaration, n => n.kind === SyntaxKind.VariableStatement); - const newNode1 = createFunctionDeclaration(func.decorators, func.modifiers, func.asteriskToken, variableDeclaration.name, func.typeParameters, func.parameters, func.type, body2); - const edits1 = textChanges.ChangeTracker.with(context, t => t.replaceNode(file, statement!, newNode1)); - return { renameFilename: undefined, renameLocation: undefined, edits: edits1 }; + const varDeclList = findAncestor(variableDeclaration, n => n.kind === SyntaxKind.VariableDeclarationList)!; + if (!isVariableDeclarationList(varDeclList)) return undefined; + + if (varDeclList.declarations.length === 0) return undefined; + if (varDeclList.declarations.length === 1) { + const statement = findAncestor(variableDeclaration, n => n.kind === SyntaxKind.VariableStatement); + const newNode1 = createFunctionDeclaration(func.decorators, func.modifiers, func.asteriskToken, variableDeclaration.name, func.typeParameters, func.parameters, func.type, body2); + const edits1 = textChanges.ChangeTracker.with(context, t => t.replaceNode(file, statement!, newNode1)); + return { renameFilename: undefined, renameLocation: undefined, edits: edits1 }; + } + else { + const statement = findAncestor(variableDeclaration, n => n.kind === SyntaxKind.VariableStatement); + const newNode1 = createFunctionDeclaration(func.decorators, func.modifiers, func.asteriskToken, variableDeclaration.name, func.typeParameters, func.parameters, func.type, body2); + + const edits1 = textChanges.ChangeTracker.with(context, t => { + t.delete(file, variableDeclaration); + t.insertNodeAfter(file, statement!, newNode1); + }); + return { renameFilename: undefined, renameLocation: undefined, edits: edits1 }; + } + + } return undefined;