add more test and fix others

This commit is contained in:
王文璐 2018-05-25 10:25:52 +08:00
parent 3d9a6ab068
commit 590476bf06
6 changed files with 63 additions and 11 deletions

View File

@ -727,7 +727,7 @@ 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, /* initial */ undefined);
return iterateCommentRanges(/*reduce*/ false, text, pos, /*trailing*/ false, cb, state);
}
export function forEachTrailingCommentRange<U>(text: string, pos: number, cb: (pos: number, end: number, kind: CommentKind, hasTrailingNewLine: boolean) => U): U | undefined;

View File

@ -48,19 +48,19 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
const returnStatement = createReturn(expression);
body = createBlock([returnStatement], /* multiLine */ true);
suppressLeadingAndTrailingTrivia(body);
copyComments(expression!, returnStatement, file, SyntaxKind.MultiLineCommentTrivia, /* explicitHtnl */ true);
copyComments(expression!, returnStatement, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ true);
}
else if (actionName === removeBracesActionName && returnStatement) {
const actualExpression = expression || createVoidZero();
body = needsParentheses(actualExpression) ? createParen(actualExpression) : actualExpression;
suppressLeadingAndTrailingTrivia(body);
copyComments(returnStatement, body, file, SyntaxKind.MultiLineCommentTrivia, /* explicitHtnl */ false);
copyComments(returnStatement, body, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ false);
}
else {
Debug.fail("invalid action");
}
const edits = textChanges.ChangeTracker.with(context, t => updateBody(t, file, func, body));
const edits = textChanges.ChangeTracker.with(context, t => t.replaceNode(file, func.body, body));
return { renameFilename: undefined, renameLocation: undefined, edits };
}
@ -68,14 +68,10 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
return isBinaryExpression(expression) && expression.operatorToken.kind === SyntaxKind.CommaToken || isObjectLiteralExpression(expression);
}
function updateBody(changeTracker: textChanges.ChangeTracker, file: SourceFile, container: ArrowFunction, body: ConciseBody) {
changeTracker.replaceNode(file, container.body, body);
}
function getConvertibleArrowFunctionAtPosition(file: SourceFile, startPosition: number): Info | undefined {
const node = getTokenAtPosition(file, startPosition, /*includeJsDocComment*/ false);
const func = getContainingFunction(node);
if (!func || !isArrowFunction(func)) return undefined;
if (!func || !isArrowFunction(func) || (!rangeContainsRange(func, node) || rangeContainsRange(func.body, node))) return undefined;
if (isExpression(func.body)) {
return {

View File

@ -1650,7 +1650,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, commentKind?: CommentKind, hasTrailingNewLine?: boolean) {
forEachLeadingCommentRange(sourceFile.text, sourceNode.pos, (pos, end, kind, htnl) => {
if (kind === SyntaxKind.MultiLineCommentTrivia) {
// Remove leading /*
@ -1662,7 +1662,7 @@ namespace ts {
// Remove leading //
pos += 2;
}
addSyntheticLeadingComment(targetNode, explicitKind || kind, sourceFile.text.slice(pos, end), explicitHtnl !== undefined ? explicitHtnl : htnl);
addSyntheticLeadingComment(targetNode, commentKind || kind, sourceFile.text.slice(pos, end), hasTrailingNewLine !== undefined ? hasTrailingNewLine : htnl);
});
}
}

View File

@ -0,0 +1,11 @@
/// <reference path='fourslash.ts' />
//// const foo = /*a*/a/*b*/ => { return; };
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 => void 0;`,
});

View File

@ -0,0 +1,27 @@
/// <reference path='fourslash.ts' />
//// const /*a*/foo/*b*/ = /*c*/(/*d*//*e*/aa/*f*/aa, /*g*/b/*h*/) /*i*//*j*/ /*k*/=>/*l*/ /*m*/{/*n*/ /*o*/return/*p*/ 1; };
goTo.select("a", "b");
verify.not.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function")
goTo.select("c", "d");
verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function")
goTo.select("e", "f");
verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function")
goTo.select("g", "h");
verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function")
goTo.select("i", "j");
verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function")
goTo.select("k", "l");
verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function")
goTo.select("m", "n");
verify.not.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function")
goTo.select("o", "p");
verify.not.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function")

View File

@ -0,0 +1,18 @@
/// <reference path='fourslash.ts' />
//// const /*a*/foo/*b*/ = /*c*/()/*d*/ /*e*//*f*/ /*g*/=>/*h*/ /*i*/1/*j*/;
goTo.select("a", "b");
verify.not.refactorAvailable("Add or remove braces in an arrow function", "Add braces to arrow function")
goTo.select("c", "d");
verify.refactorAvailable("Add or remove braces in an arrow function", "Add braces to arrow function")
goTo.select("e", "f");
verify.refactorAvailable("Add or remove braces in an arrow function", "Add braces to arrow function")
goTo.select("g", "h");
verify.refactorAvailable("Add or remove braces in an arrow function", "Add braces to arrow function")
goTo.select("i", "j");
verify.not.refactorAvailable("Add or remove braces in an arrow function", "Add braces to arrow function")