Skip to content

Commit 8bc7f43

Browse files
D0nGiovannibigaru
authored andcommitted
Make conditions more expressive
1 parent 1c74d0e commit 8bc7f43

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/services/refactors/convertArrowFunctionOrFunctionExpression.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,18 @@ namespace ts.refactor.convertArrowFunctionOrFunctionExpression {
9595

9696
maybeFunc = getContainingFunction(token);
9797
if (!!maybeFunc && (isFunctionExpression(maybeFunc) || isArrowFunction(maybeFunc)) && !rangeContainsRange(maybeFunc.body, token)) {
98-
return { selectedVariableDeclaration: false, func: maybeFunc };
98+
return { selectedVariableDeclaration: false, func: maybeFunc };
9999
}
100100

101101
return undefined;
102102
}
103103

104104
function isSingleVariableDeclaration(parent: Node): parent is VariableDeclarationList {
105-
return isVariableDeclarationList(parent) && parent.declarations.length === 1;
105+
return isVariableDeclaration(parent) || (isVariableDeclarationList(parent) && parent.declarations.length === 1);
106106
}
107107

108108
function getArrowFunctionFromVariableDeclaration(parent: Node): ArrowFunction | undefined {
109-
if (!(isVariableDeclaration(parent) || isSingleVariableDeclaration(parent))) return undefined;
109+
if (!isSingleVariableDeclaration(parent)) return undefined;
110110
const variableDeclaration = isVariableDeclaration(parent) ? parent : parent.declarations[0];
111111

112112
const initializer = variableDeclaration.initializer;
@@ -172,7 +172,7 @@ namespace ts.refactor.convertArrowFunctionOrFunctionExpression {
172172
const head = statements[0];
173173
let body: ConciseBody;
174174

175-
if (func.body.statements.length === 1 && ((isReturnStatement(head) && !!head.expression) || isExpressionStatement(head))) {
175+
if (canBeConvertedToExpression(func.body, head)) {
176176
body = head.expression!;
177177
suppressLeadingAndTrailingTrivia(body);
178178
copyComments(head, body, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ false);
@@ -185,4 +185,8 @@ namespace ts.refactor.convertArrowFunctionOrFunctionExpression {
185185
const edits = textChanges.ChangeTracker.with(context, t => t.replaceNode(file, func, newNode));
186186
return { renameFilename: undefined, renameLocation: undefined, edits };
187187
}
188+
189+
function canBeConvertedToExpression(body: Block, head: Statement): head is ReturnStatement | ExpressionStatement {
190+
return body.statements.length === 1 && ((isReturnStatement(head) && !!head.expression) || isExpressionStatement(head));
191+
}
188192
}

0 commit comments

Comments
 (0)