Skip to content

Commit 590476b

Browse files
committed
add more test and fix others
1 parent 3d9a6ab commit 590476b

6 files changed

Lines changed: 63 additions & 11 deletions

File tree

src/compiler/scanner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ namespace ts {
727727
export function forEachLeadingCommentRange<U>(text: string, pos: number, cb: (pos: number, end: number, kind: CommentKind, hasTrailingNewLine: boolean) => U): U | undefined;
728728
export function forEachLeadingCommentRange<T, U>(text: string, pos: number, cb: (pos: number, end: number, kind: CommentKind, hasTrailingNewLine: boolean, state: T) => U, state: T): U | undefined;
729729
export function forEachLeadingCommentRange<T, U>(text: string, pos: number, cb: (pos: number, end: number, kind: CommentKind, hasTrailingNewLine: boolean, state: T) => U, state?: T): U | undefined {
730-
return iterateCommentRanges(/*reduce*/ false, text, pos, /*trailing*/ false, cb, state, /* initial */ undefined);
730+
return iterateCommentRanges(/*reduce*/ false, text, pos, /*trailing*/ false, cb, state);
731731
}
732732

733733
export function forEachTrailingCommentRange<U>(text: string, pos: number, cb: (pos: number, end: number, kind: CommentKind, hasTrailingNewLine: boolean) => U): U | undefined;

src/services/refactors/addOrRemoveBracesToArrowFunction.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,34 +48,30 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
4848
const returnStatement = createReturn(expression);
4949
body = createBlock([returnStatement], /* multiLine */ true);
5050
suppressLeadingAndTrailingTrivia(body);
51-
copyComments(expression!, returnStatement, file, SyntaxKind.MultiLineCommentTrivia, /* explicitHtnl */ true);
51+
copyComments(expression!, returnStatement, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ true);
5252
}
5353
else if (actionName === removeBracesActionName && returnStatement) {
5454
const actualExpression = expression || createVoidZero();
5555
body = needsParentheses(actualExpression) ? createParen(actualExpression) : actualExpression;
5656
suppressLeadingAndTrailingTrivia(body);
57-
copyComments(returnStatement, body, file, SyntaxKind.MultiLineCommentTrivia, /* explicitHtnl */ false);
57+
copyComments(returnStatement, body, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ false);
5858
}
5959
else {
6060
Debug.fail("invalid action");
6161
}
6262

63-
const edits = textChanges.ChangeTracker.with(context, t => updateBody(t, file, func, body));
63+
const edits = textChanges.ChangeTracker.with(context, t => t.replaceNode(file, func.body, body));
6464
return { renameFilename: undefined, renameLocation: undefined, edits };
6565
}
6666

6767
function needsParentheses(expression: Expression) {
6868
return isBinaryExpression(expression) && expression.operatorToken.kind === SyntaxKind.CommaToken || isObjectLiteralExpression(expression);
6969
}
7070

71-
function updateBody(changeTracker: textChanges.ChangeTracker, file: SourceFile, container: ArrowFunction, body: ConciseBody) {
72-
changeTracker.replaceNode(file, container.body, body);
73-
}
74-
7571
function getConvertibleArrowFunctionAtPosition(file: SourceFile, startPosition: number): Info | undefined {
7672
const node = getTokenAtPosition(file, startPosition, /*includeJsDocComment*/ false);
7773
const func = getContainingFunction(node);
78-
if (!func || !isArrowFunction(func)) return undefined;
74+
if (!func || !isArrowFunction(func) || (!rangeContainsRange(func, node) || rangeContainsRange(func.body, node))) return undefined;
7975

8076
if (isExpression(func.body)) {
8177
return {

src/services/utilities.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,7 +1650,7 @@ namespace ts {
16501650
return lastPos;
16511651
}
16521652

1653-
export function copyComments(sourceNode: Node, targetNode: Node, sourceFile: SourceFile, explicitKind?: CommentKind, explicitHtnl?: boolean) {
1653+
export function copyComments(sourceNode: Node, targetNode: Node, sourceFile: SourceFile, commentKind?: CommentKind, hasTrailingNewLine?: boolean) {
16541654
forEachLeadingCommentRange(sourceFile.text, sourceNode.pos, (pos, end, kind, htnl) => {
16551655
if (kind === SyntaxKind.MultiLineCommentTrivia) {
16561656
// Remove leading /*
@@ -1662,7 +1662,7 @@ namespace ts {
16621662
// Remove leading //
16631663
pos += 2;
16641664
}
1665-
addSyntheticLeadingComment(targetNode, explicitKind || kind, sourceFile.text.slice(pos, end), explicitHtnl !== undefined ? explicitHtnl : htnl);
1665+
addSyntheticLeadingComment(targetNode, commentKind || kind, sourceFile.text.slice(pos, end), hasTrailingNewLine !== undefined ? hasTrailingNewLine : htnl);
16661666
});
16671667
}
16681668
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const foo = /*a*/a/*b*/ => { return; };
4+
5+
goTo.select("a", "b");
6+
edit.applyRefactor({
7+
refactorName: "Add or remove braces in an arrow function",
8+
actionName: "Remove braces from arrow function",
9+
actionDescription: "Remove braces from arrow function",
10+
newContent: `const foo = a => void 0;`,
11+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const /*a*/foo/*b*/ = /*c*/(/*d*//*e*/aa/*f*/aa, /*g*/b/*h*/) /*i*//*j*/ /*k*/=>/*l*/ /*m*/{/*n*/ /*o*/return/*p*/ 1; };
4+
5+
goTo.select("a", "b");
6+
verify.not.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function")
7+
8+
goTo.select("c", "d");
9+
verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function")
10+
11+
goTo.select("e", "f");
12+
verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function")
13+
14+
goTo.select("g", "h");
15+
verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function")
16+
17+
goTo.select("i", "j");
18+
verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function")
19+
20+
goTo.select("k", "l");
21+
verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function")
22+
23+
goTo.select("m", "n");
24+
verify.not.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function")
25+
26+
goTo.select("o", "p");
27+
verify.not.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function")
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const /*a*/foo/*b*/ = /*c*/()/*d*/ /*e*//*f*/ /*g*/=>/*h*/ /*i*/1/*j*/;
4+
5+
goTo.select("a", "b");
6+
verify.not.refactorAvailable("Add or remove braces in an arrow function", "Add braces to arrow function")
7+
8+
goTo.select("c", "d");
9+
verify.refactorAvailable("Add or remove braces in an arrow function", "Add braces to arrow function")
10+
11+
goTo.select("e", "f");
12+
verify.refactorAvailable("Add or remove braces in an arrow function", "Add braces to arrow function")
13+
14+
goTo.select("g", "h");
15+
verify.refactorAvailable("Add or remove braces in an arrow function", "Add braces to arrow function")
16+
17+
goTo.select("i", "j");
18+
verify.not.refactorAvailable("Add or remove braces in an arrow function", "Add braces to arrow function")

0 commit comments

Comments
 (0)