From 6922f6c4cae774cdb9f3b5446600ca53be50fb95 Mon Sep 17 00:00:00 2001 From: BigAru Date: Fri, 19 Oct 2018 10:50:33 +0200 Subject: [PATCH] refactor getInfo getAvailableActions --- ...onvertArrowFunctionOrFunctionExpression.ts | 61 +++++++------------ 1 file changed, 23 insertions(+), 38 deletions(-) diff --git a/src/services/refactors/convertArrowFunctionOrFunctionExpression.ts b/src/services/refactors/convertArrowFunctionOrFunctionExpression.ts index 29b205a6358..5b3e00fa478 100644 --- a/src/services/refactors/convertArrowFunctionOrFunctionExpression.ts +++ b/src/services/refactors/convertArrowFunctionOrFunctionExpression.ts @@ -14,7 +14,7 @@ namespace ts.refactor.convertArrowFunctionOrFunctionExpression { registerRefactor(refactorName, { getEditsForAction, getAvailableActions }); interface Info { - token: Node; + fromVarDecl: boolean; func: FunctionExpression | ArrowFunction; } @@ -24,35 +24,24 @@ namespace ts.refactor.convertArrowFunctionOrFunctionExpression { const info = getInfo(file, startPosition); if (!info) return undefined; - const { token, func } = info; - + const { fromVarDecl, func } = info; const possibleActions: RefactorActionInfo[] = []; - const parent = token.parent; - - if (isVariableDeclaration(parent) || (isVariableDeclarationList(parent) && parent.declarations.length === 1)) { - const variableDeclaration = isVariableDeclaration(parent) ? parent : parent.declarations[0]; - if (isArrowFunction(variableDeclaration.initializer!)) { - possibleActions.push({ - name: toNamedFunctionActionName, - description: toNamedFunctionActionDescription - }); - } + if (fromVarDecl || (isArrowFunction(func) && isVariableDeclaration(func.parent))) { + possibleActions.push({ + name: toNamedFunctionActionName, + description: toNamedFunctionActionDescription + }); } - else if (isArrowFunction(func)) { - if (isVariableDeclaration(func.parent)) { - possibleActions.push({ - name: toNamedFunctionActionName, - description: toNamedFunctionActionDescription - }); - } + if (isArrowFunction(func) && !fromVarDecl) { possibleActions.push({ name: toAnonymousFunctionActionName, description: toAnonymousFunctionActionDescription }); } - else { + + if (isFunctionExpression(func)) { possibleActions.push({ name: toArrowFunctionActionName, description: toArrowFunctionActionDescription @@ -163,27 +152,23 @@ namespace ts.refactor.convertArrowFunctionOrFunctionExpression { function getInfo(file: SourceFile, startPosition: number): Info | undefined { const token = getTokenAtPosition(file, startPosition); - let func: FunctionExpression | ArrowFunction; - const parent = token.parent; - if (isVariableDeclaration(parent) || (isVariableDeclarationList(parent) && parent.declarations.length === 1)) { - const variableDeclaration = isVariableDeclaration(parent) ? parent : parent.declarations[0]; + const declFunc = extractArrowFnFromDecl(token.parent); + if (!!declFunc) return { fromVarDecl: true, func: declFunc }; - if (!variableDeclaration.initializer) return undefined; - const initializer = variableDeclaration.initializer; - - if (!isArrowFunction(initializer)) return undefined; - func = initializer; - } - else { - const tmpFunc = getContainingFunction(token); - if (!tmpFunc || !(isFunctionExpression(tmpFunc) || isArrowFunction(tmpFunc)) || rangeContainsRange(tmpFunc.body, token)) return undefined; - func = tmpFunc; + const maybeFunc = getContainingFunction(token); + if (!!maybeFunc && (isFunctionExpression(maybeFunc) || isArrowFunction(maybeFunc)) && !rangeContainsRange(maybeFunc.body, token)) { + return { fromVarDecl: false, func: maybeFunc }; } - - - return { token, func }; + return undefined; } + function extractArrowFnFromDecl(parent: Node): ArrowFunction | undefined { + if (!(isVariableDeclaration(parent) || (isVariableDeclarationList(parent) && parent.declarations.length === 1))) return undefined; + const varDecl = isVariableDeclaration(parent) ? parent : parent.declarations[0]; + + if (!varDecl.initializer || !isArrowFunction(varDecl.initializer)) return undefined; + return varDecl.initializer; + } }