fix converter

This commit is contained in:
王文璐
2018-05-23 10:22:17 +08:00
parent bd9a8b5c0c
commit 0c06126d60
11 changed files with 53 additions and 19 deletions
+5 -5
View File
@@ -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<T, U>(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<T, U>(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<U>(text: string, pos: number, cb: (pos: number, end: number, kind: CommentKind, hasTrailingNewLine: boolean) => U): U | undefined;
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;
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 {
return iterateCommentRanges(/*reduce*/ false, text, pos, /*trailing*/ false, cb, state);
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;
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 {
return iterateCommentRanges(/*reduce*/ false, text, pos, /*trailing*/ false, cb, state, /* initial */ undefined, inline);
}
export function forEachTrailingCommentRange<U>(text: string, pos: number, cb: (pos: number, end: number, kind: CommentKind, hasTrailingNewLine: boolean) => U): U | undefined;
@@ -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 = <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,
+3 -3
View File
@@ -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)
}
}
@@ -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;
};`,
});
@@ -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 });
};`,
});
@@ -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;`,
});
@@ -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;
}`,
});
@@ -0,0 +1,16 @@
/// <reference path='fourslash.ts' />
//// 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;
}`,
});
@@ -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;
};`,
});
@@ -0,0 +1,6 @@
/// <reference path='fourslash.ts' />
//// const foo = /*a*/a/*b*/ => { };
goTo.select("a", "b");
verify.not.refactorAvailable("Add or remove braces in an arrow function");
@@ -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);
};`,
});