From 0c06126d60b95ee69abda1eb6b3aed3a45815593 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=96=87=E7=92=90?= Date: Wed, 25 Apr 2018 18:25:47 +0800 Subject: [PATCH] fix converter --- src/compiler/scanner.ts | 10 +++++----- .../addOrRemoveBracesToArrowFunction.ts | 7 ++++--- src/services/utilities.ts | 6 +++--- .../refactorAddOrRemoveBracesToArrowFunction1.ts | 4 +++- .../refactorAddOrRemoveBracesToArrowFunction2.ts | 4 +++- ...refactorAddOrRemoveBracesToArrowFunction20.ts | 2 +- ...refactorAddOrRemoveBracesToArrowFunction21.ts | 9 ++++++--- ...refactorAddOrRemoveBracesToArrowFunction22.ts | 16 ++++++++++++++++ .../refactorAddOrRemoveBracesToArrowFunction3.ts | 4 +++- .../refactorAddOrRemoveBracesToArrowFunction7.ts | 6 ++++++ .../refactorAddOrRemoveBracesToArrowFunction9.ts | 4 +++- 11 files changed, 53 insertions(+), 19 deletions(-) create mode 100644 tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction22.ts create mode 100644 tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction7.ts diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts index 8963285dc7a..2bcbed12835 100644 --- a/src/compiler/scanner.ts +++ b/src/compiler/scanner.ts @@ -626,13 +626,13 @@ namespace ts { * @returns If "reduce" is true, the accumulated value. If "reduce" is false, the first truthy * return value of the callback. */ - function iterateCommentRanges(reduce: boolean, text: string, pos: number, trailing: boolean, cb: (pos: number, end: number, kind: CommentKind, hasTrailingNewLine: boolean, state: T, memo: U | undefined) => U, state: T, initial?: U): U | undefined { + function iterateCommentRanges(reduce: boolean, text: string, pos: number, trailing: boolean, cb: (pos: number, end: number, kind: CommentKind, hasTrailingNewLine: boolean, state: T, memo: U | undefined) => U, state: T, initial?: U, inline?: boolean): U | undefined { let pendingPos!: number; let pendingEnd!: number; let pendingKind!: CommentKind; let pendingHasTrailingNewLine!: boolean; let hasPendingCommentRange = false; - let collecting = trailing || pos === 0; + let collecting = inline || trailing || pos === 0; let accumulator = initial; scan: while (pos >= 0 && pos < text.length) { const ch = text.charCodeAt(pos); @@ -725,9 +725,9 @@ namespace ts { } export function forEachLeadingCommentRange(text: string, pos: number, cb: (pos: number, end: number, kind: CommentKind, hasTrailingNewLine: boolean) => U): U | undefined; - export function forEachLeadingCommentRange(text: string, pos: number, cb: (pos: number, end: number, kind: CommentKind, hasTrailingNewLine: boolean, state: T) => U, state: T): U | undefined; - export function forEachLeadingCommentRange(text: string, pos: number, cb: (pos: number, end: number, kind: CommentKind, hasTrailingNewLine: boolean, state: T) => U, state?: T): U | undefined { - return iterateCommentRanges(/*reduce*/ false, text, pos, /*trailing*/ false, cb, state); + export function forEachLeadingCommentRange(text: string, pos: number, cb: (pos: number, end: number, kind: CommentKind, hasTrailingNewLine: boolean, state: T) => U, state: T, inline?: boolean): U | undefined; + export function forEachLeadingCommentRange(text: string, pos: number, cb: (pos: number, end: number, kind: CommentKind, hasTrailingNewLine: boolean, state: T) => U, state?: T, inline?: boolean): U | undefined { + return iterateCommentRanges(/*reduce*/ false, text, pos, /*trailing*/ false, cb, state, /* initial */ undefined, inline); } export function forEachTrailingCommentRange(text: string, pos: number, cb: (pos: number, end: number, kind: CommentKind, hasTrailingNewLine: boolean) => U): U | undefined; diff --git a/src/services/refactors/addOrRemoveBracesToArrowFunction.ts b/src/services/refactors/addOrRemoveBracesToArrowFunction.ts index e6fed3ebaf7..647bbbc5db6 100644 --- a/src/services/refactors/addOrRemoveBracesToArrowFunction.ts +++ b/src/services/refactors/addOrRemoveBracesToArrowFunction.ts @@ -46,12 +46,14 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction { let body: ConciseBody; if (actionName === addBracesActionName) { const returnStatement = createReturn(expression); - body = createBlock([returnStatement]); - copyComments(expression, returnStatement, file, SyntaxKind.SingleLineCommentTrivia, true); + body = createBlock([returnStatement], /* multiLine */ true); + suppressLeadingAndTrailingTrivia(expression); + copyComments(expression, returnStatement, file, SyntaxKind.MultiLineCommentTrivia, true, true); } else if (actionName === removeBracesActionName) { const returnStatement = expression.parent; body = needsParentheses(expression) ? createParen(expression) : expression; + suppressLeadingAndTrailingTrivia(returnStatement); copyComments(returnStatement, body, file, SyntaxKind.MultiLineCommentTrivia, false); } else { @@ -99,7 +101,6 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction { else if (func.body.statements.length === 1) { const firstStatement = first(func.body.statements); if (isReturnStatement(firstStatement)) { - return { func, addBraces: false, diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 987398f36f4..acb995bf5bf 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -1643,7 +1643,7 @@ namespace ts { return lastPos; } - export function copyComments(sourceNode: Node, targetNode: Node, sourceFile: SourceFile, explicitKind?: CommentKind, explicitHtnl?: boolean) { + export function copyComments(sourceNode: Node, targetNode: Node, sourceFile: SourceFile, explicitKind?: CommentKind, explicitHtnl?: boolean, inline?: boolean) { forEachLeadingCommentRange(sourceFile.text, sourceNode.pos, (pos, end, kind, htnl) => { if (kind === SyntaxKind.MultiLineCommentTrivia) { // Remove leading /* @@ -1655,7 +1655,7 @@ namespace ts { // Remove leading // pos += 2; } - addSyntheticLeadingComment(targetNode, explicitKind || kind, sourceFile.text.slice(pos, end), explicitHtnl !== undefined ? explicitHtnl : htnl); - }); + addSyntheticLeadingComment(targetNode, explicitKind || kind, sourceFile.text.slice(pos, end), explicitHtnl !== undefined ? explicitHtnl : htnl); + }, undefined, inline) } } diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction1.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction1.ts index 1d8d52e201f..9671981dfcf 100644 --- a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction1.ts +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction1.ts @@ -7,5 +7,7 @@ edit.applyRefactor({ refactorName: "Add or remove braces in an arrow function", actionName: "Add braces to arrow function", actionDescription: "Add braces to arrow function", - newContent: `const foo = a => { return a + 1; };`, + newContent: `const foo = a => { + return a + 1; +};`, }); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction2.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction2.ts index d875018c397..2cce0dc5998 100644 --- a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction2.ts +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction2.ts @@ -7,5 +7,7 @@ edit.applyRefactor({ refactorName: "Add or remove braces in an arrow function", actionName: "Add braces to arrow function", actionDescription: "Add braces to arrow function", - newContent: `const foo = a => { return ({ a: 1 }); };`, + newContent: `const foo = a => { + return ({ a: 1 }); +};`, }); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction20.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction20.ts index 9dde36a8f26..f84e66898fe 100644 --- a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction20.ts +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction20.ts @@ -10,5 +10,5 @@ edit.applyRefactor({ refactorName: "Add or remove braces in an arrow function", actionName: "Remove braces from arrow function", actionDescription: "Remove braces from arrow function", - newContent: `const foo = a => /* return comment */ a;`, + newContent: `const foo = a => /* return comment*/ a;`, }); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction21.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction21.ts index 97ca16bcb28..4ba578ded89 100644 --- a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction21.ts +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction21.ts @@ -5,7 +5,10 @@ goTo.select("a", "b"); edit.applyRefactor({ refactorName: "Add or remove braces in an arrow function", - actionName: "Remove braces from arrow function", - actionDescription: "Remove braces from arrow function", - newContent: `const foo = a => { /* expression comment */ return a + 1; }`, + actionName: "Add braces to arrow function", + actionDescription: "Add braces to arrow function", + newContent: `const foo = a => { + /* expression comment */ + return a + 1; +}`, }); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction22.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction22.ts new file mode 100644 index 00000000000..9d179bcb935 --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction22.ts @@ -0,0 +1,16 @@ +/// + +//// const foo = /*a*/a/*b*/ => +//// /* expression comment */ +//// a + 1 + +goTo.select("a", "b"); +edit.applyRefactor({ + refactorName: "Add or remove braces in an arrow function", + actionName: "Add braces to arrow function", + actionDescription: "Add braces to arrow function", + newContent: `const foo = a => { + /* expression comment */ + return a + 1; +}`, +}); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction3.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction3.ts index ead5867a175..23710deb00b 100644 --- a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction3.ts +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction3.ts @@ -7,5 +7,7 @@ edit.applyRefactor({ refactorName: "Add or remove braces in an arrow function", actionName: "Add braces to arrow function", actionDescription: "Add braces to arrow function", - newContent: `const foo = a => { return 1; };`, + newContent: `const foo = a => { + return 1; +};`, }); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction7.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction7.ts new file mode 100644 index 00000000000..7a8690be4eb --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction7.ts @@ -0,0 +1,6 @@ +/// + +//// const foo = /*a*/a/*b*/ => { }; + +goTo.select("a", "b"); +verify.not.refactorAvailable("Add or remove braces in an arrow function"); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction9.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction9.ts index 610e19cff45..a7ba12a5996 100644 --- a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction9.ts +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction9.ts @@ -7,5 +7,7 @@ edit.applyRefactor({ refactorName: "Add or remove braces in an arrow function", actionName: "Add braces to arrow function", actionDescription: "Add braces to arrow function", - newContent: `const foo = a => { return (1, 2, 3); };`, + newContent: `const foo = a => { + return (1, 2, 3); +};`, });