Skip to content

Commit 8dbb7b5

Browse files
committed
fix converter
1 parent 590599c commit 8dbb7b5

10 files changed

+47
-19
lines changed

src/compiler/scanner.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -626,13 +626,13 @@ namespace ts {
626626
* @returns If "reduce" is true, the accumulated value. If "reduce" is false, the first truthy
627627
* return value of the callback.
628628
*/
629-
function iterateCommentRanges<T, U>(reduce: boolean, text: string, pos: number, trailing: boolean, cb: (pos: number, end: number, kind: CommentKind, hasTrailingNewLine: boolean, state: T, memo: U) => U, state: T, initial?: U): U {
629+
function iterateCommentRanges<T, U>(reduce: boolean, text: string, pos: number, trailing: boolean, cb: (pos: number, end: number, kind: CommentKind, hasTrailingNewLine: boolean, state: T, memo: U) => U, state: T, initial?: U, inline?: boolean): U {
630630
let pendingPos: number;
631631
let pendingEnd: number;
632632
let pendingKind: CommentKind;
633633
let pendingHasTrailingNewLine: boolean;
634634
let hasPendingCommentRange = false;
635-
let collecting = trailing || pos === 0;
635+
let collecting = inline || trailing || pos === 0;
636636
let accumulator = initial;
637637
scan: while (pos >= 0 && pos < text.length) {
638638
const ch = text.charCodeAt(pos);
@@ -725,9 +725,9 @@ namespace ts {
725725
}
726726

727727
export function forEachLeadingCommentRange<U>(text: string, pos: number, cb: (pos: number, end: number, kind: CommentKind, hasTrailingNewLine: boolean) => U): U | undefined;
728-
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;
729-
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);
728+
export function forEachLeadingCommentRange<T, U>(text: string, pos: number, cb: (pos: number, end: number, kind: CommentKind, hasTrailingNewLine: boolean, state: T) => U, state: T, inline?: boolean): U | undefined;
729+
export function forEachLeadingCommentRange<T, U>(text: string, pos: number, cb: (pos: number, end: number, kind: CommentKind, hasTrailingNewLine: boolean, state: T) => U, state?: T, inline?: boolean): U | undefined {
730+
return iterateCommentRanges(/*reduce*/ false, text, pos, /*trailing*/ false, cb, state, /* initial */ undefined, inline);
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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
4646
let body: ConciseBody;
4747
if (actionName === addBracesActionName) {
4848
const returnStatement = createReturn(expression);
49-
body = createBlock([returnStatement]);
50-
copyComments(expression, returnStatement, file, SyntaxKind.SingleLineCommentTrivia, true);
49+
body = createBlock([returnStatement], /* multiLine */ true);
50+
suppressLeadingAndTrailingTrivia(expression);
51+
copyComments(expression, returnStatement, file, SyntaxKind.MultiLineCommentTrivia, true, true);
5152
}
5253
else if (actionName === removeBracesActionName) {
5354
const returnStatement = <ReturnStatement>expression.parent;
5455
body = needsParentheses(expression) ? createParen(expression) : expression;
56+
suppressLeadingAndTrailingTrivia(returnStatement);
5557
copyComments(returnStatement, body, file, SyntaxKind.MultiLineCommentTrivia, false);
5658
}
5759
else {
@@ -99,7 +101,6 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
99101
else if (func.body.statements.length === 1) {
100102
const firstStatement = first(func.body.statements);
101103
if (isReturnStatement(firstStatement)) {
102-
103104
return {
104105
func,
105106
addBraces: false,

src/services/utilities.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,7 +1552,7 @@ namespace ts {
15521552
return lastPos;
15531553
}
15541554

1555-
export function copyComments(sourceNode: Node, targetNode: Node, sourceFile: SourceFile, explicitKind?: CommentKind, explicitHtnl?: boolean) {
1555+
export function copyComments(sourceNode: Node, targetNode: Node, sourceFile: SourceFile, explicitKind?: CommentKind, explicitHtnl?: boolean, inline?: boolean) {
15561556
forEachLeadingCommentRange(sourceFile.text, sourceNode.pos, (pos, end, kind, htnl) => {
15571557
if (kind === SyntaxKind.MultiLineCommentTrivia) {
15581558
// Remove leading /*
@@ -1564,7 +1564,7 @@ namespace ts {
15641564
// Remove leading //
15651565
pos += 2;
15661566
}
1567-
addSyntheticLeadingComment(targetNode, explicitKind || kind, sourceFile.text.slice(pos, end), explicitHtnl !== undefined ? explicitHtnl : htnl);
1568-
});
1567+
addSyntheticLeadingComment(targetNode, explicitKind || kind, sourceFile.text.slice(pos, end), explicitHtnl !== undefined ? explicitHtnl : htnl);
1568+
}, undefined, inline)
15691569
}
15701570
}

tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction1.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ edit.applyRefactor({
77
refactorName: "Add or remove braces in an arrow function",
88
actionName: "Add braces to arrow function",
99
actionDescription: "Add braces to arrow function",
10-
newContent: `const foo = a => { return a + 1; };`,
10+
newContent: `const foo = a => {
11+
return a + 1;
12+
};`,
1113
});

tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction2.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ edit.applyRefactor({
77
refactorName: "Add or remove braces in an arrow function",
88
actionName: "Add braces to arrow function",
99
actionDescription: "Add braces to arrow function",
10-
newContent: `const foo = a => { return ({ a: 1 }); };`,
10+
newContent: `const foo = a => {
11+
return ({ a: 1 });
12+
};`,
1113
});

tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction20.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ edit.applyRefactor({
1010
refactorName: "Add or remove braces in an arrow function",
1111
actionName: "Remove braces from arrow function",
1212
actionDescription: "Remove braces from arrow function",
13-
newContent: `const foo = a => /* return comment */ a;`,
13+
newContent: `const foo = a => /* return comment*/ a;`,
1414
});

tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction21.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
goTo.select("a", "b");
66
edit.applyRefactor({
77
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 => { /* expression comment */ return a + 1; }`,
8+
actionName: "Add braces to arrow function",
9+
actionDescription: "Add braces to arrow function",
10+
newContent: `const foo = a => {
11+
/* expression comment */
12+
return a + 1;
13+
}`,
1114
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const foo = /*a*/a/*b*/ =>
4+
//// /* expression comment */
5+
//// a + 1
6+
7+
goTo.select("a", "b");
8+
edit.applyRefactor({
9+
refactorName: "Add or remove braces in an arrow function",
10+
actionName: "Add braces to arrow function",
11+
actionDescription: "Add braces to arrow function",
12+
newContent: `const foo = a => {
13+
/* expression comment */
14+
return a + 1;
15+
}`,
16+
});

tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction3.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ edit.applyRefactor({
77
refactorName: "Add or remove braces in an arrow function",
88
actionName: "Add braces to arrow function",
99
actionDescription: "Add braces to arrow function",
10-
newContent: `const foo = a => { return 1; };`,
10+
newContent: `const foo = a => {
11+
return 1;
12+
};`,
1113
});

tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction9.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ edit.applyRefactor({
77
refactorName: "Add or remove braces in an arrow function",
88
actionName: "Add braces to arrow function",
99
actionDescription: "Add braces to arrow function",
10-
newContent: `const foo = a => { return (1, 2, 3); };`,
10+
newContent: `const foo = a => {
11+
return (1, 2, 3);
12+
};`,
1113
});

0 commit comments

Comments
 (0)