Skip to content

Commit de75f14

Browse files
committed
fix void return statement
1 parent b6669c9 commit de75f14

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

src/services/refactors/addOrRemoveBracesToArrowFunction.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
1010

1111
interface Info {
1212
func: ArrowFunction;
13-
expression: Expression;
13+
expression: Expression | undefined;
14+
returnStatement?: ReturnStatement;
1415
addBraces: boolean;
1516
}
1617

@@ -40,23 +41,23 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
4041
const info = getConvertibleArrowFunctionAtPosition(file, startPosition);
4142
if (!info) return undefined;
4243

43-
const { expression, func } = info;
44+
const { expression, returnStatement, func } = info;
4445

4546
let body: ConciseBody;
4647
if (actionName === addBracesActionName) {
4748
const returnStatement = createReturn(expression);
4849
body = createBlock([returnStatement], /* multiLine */ true);
4950
suppressLeadingAndTrailingTrivia(body);
50-
copyComments(expression, returnStatement, file, SyntaxKind.MultiLineCommentTrivia, true, true);
51+
copyComments(expression!, returnStatement, file, SyntaxKind.MultiLineCommentTrivia, /* explicitHtnl */ true);
5152
}
52-
else if (actionName === removeBracesActionName) {
53-
const returnStatement = <ReturnStatement>expression.parent;
54-
body = needsParentheses(expression) ? createParen(expression) : expression;
53+
else if (actionName === removeBracesActionName && returnStatement) {
54+
const actualExpression = expression || createVoidZero();
55+
body = needsParentheses(actualExpression) ? createParen(actualExpression) : actualExpression;
5556
suppressLeadingAndTrailingTrivia(body);
56-
copyComments(returnStatement, body, file, SyntaxKind.MultiLineCommentTrivia, false);
57+
copyComments(returnStatement, body, file, SyntaxKind.MultiLineCommentTrivia, /* explicitHtnl */ false);
5758
}
5859
else {
59-
Debug.fail('invalid action');
60+
Debug.fail("invalid action");
6061
}
6162

6263
const edits = textChanges.ChangeTracker.with(context, t => updateBody(t, file, func, body));
@@ -89,7 +90,8 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
8990
return {
9091
func,
9192
addBraces: false,
92-
expression: firstStatement.expression
93+
expression: firstStatement.expression,
94+
returnStatement: firstStatement
9395
};
9496
}
9597
}

src/services/utilities.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1643,7 +1643,7 @@ namespace ts {
16431643
return lastPos;
16441644
}
16451645

1646-
export function copyComments(sourceNode: Node, targetNode: Node, sourceFile: SourceFile, explicitKind?: CommentKind, explicitHtnl?: boolean, inline?: boolean) {
1646+
export function copyComments(sourceNode: Node, targetNode: Node, sourceFile: SourceFile, explicitKind?: CommentKind, explicitHtnl?: boolean) {
16471647
forEachLeadingCommentRange(sourceFile.text, sourceNode.pos, (pos, end, kind, htnl) => {
16481648
if (kind === SyntaxKind.MultiLineCommentTrivia) {
16491649
// Remove leading /*
@@ -1656,6 +1656,6 @@ namespace ts {
16561656
pos += 2;
16571657
}
16581658
addSyntheticLeadingComment(targetNode, explicitKind || kind, sourceFile.text.slice(pos, end), explicitHtnl !== undefined ? explicitHtnl : htnl);
1659-
})
1659+
});
16601660
}
16611661
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const foo = /*a*/a/*b*/ => {
4+
//// return;
5+
//// };
6+
7+
goTo.select("a", "b");
8+
edit.applyRefactor({
9+
refactorName: "Add or remove braces in an arrow function",
10+
actionName: "Remove braces from arrow function",
11+
actionDescription: "Remove braces from arrow function",
12+
newContent: `const foo = a => void 0`,
13+
});

0 commit comments

Comments
 (0)