add tests and fix
This commit is contained in:
parent
5a69d9c255
commit
32be0c7099
|
@ -4281,7 +4281,7 @@
|
|||
"category": "Message",
|
||||
"code": 95054
|
||||
},
|
||||
"Convert arrow function": {
|
||||
"Add or remove braces in an arrow function": {
|
||||
"category": "Message",
|
||||
"code": 95055
|
||||
},
|
||||
|
|
|
@ -121,7 +121,7 @@
|
|||
"../services/refactors/extractSymbol.ts",
|
||||
"../services/refactors/generateGetAccessorAndSetAccessor.ts",
|
||||
"../services/refactors/moveToNewFile.ts",
|
||||
"../services/refactors/convertArrowFunction.ts",
|
||||
"../services/refactors/addOrRemoveBracesToArrowFunction.ts",
|
||||
"../services/sourcemaps.ts",
|
||||
"../services/services.ts",
|
||||
"../services/breakpoints.ts",
|
||||
|
|
|
@ -117,7 +117,7 @@
|
|||
"../services/refactors/extractSymbol.ts",
|
||||
"../services/refactors/generateGetAccessorAndSetAccessor.ts",
|
||||
"../services/refactors/moveToNewFile.ts",
|
||||
"../services/refactors/convertArrowFunction.ts",
|
||||
"../services/refactors/addOrRemoveBracesToArrowFunction.ts",
|
||||
"../services/sourcemaps.ts",
|
||||
"../services/services.ts",
|
||||
"../services/breakpoints.ts",
|
||||
|
|
|
@ -123,7 +123,7 @@
|
|||
"../services/refactors/extractSymbol.ts",
|
||||
"../services/refactors/generateGetAccessorAndSetAccessor.ts",
|
||||
"../services/refactors/moveToNewFile.ts",
|
||||
"../services/refactors/convertArrowFunction.ts",
|
||||
"../services/refactors/addOrRemoveBracesToArrowFunction.ts",
|
||||
"../services/sourcemaps.ts",
|
||||
"../services/services.ts",
|
||||
"../services/breakpoints.ts",
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/* @internal */
|
||||
namespace ts.refactor.convertArrowFunction {
|
||||
const refactorName = "Convert arrow function";
|
||||
const refactorDescription = Diagnostics.Convert_arrow_function.message;
|
||||
namespace ts.refactor.addOrRemoveBracesToArrowFunction {
|
||||
const refactorName = "Add or remove braces in an arrow function";
|
||||
const refactorDescription = Diagnostics.Add_or_remove_braces_in_an_arrow_function.message;
|
||||
const addBracesActionName = "Add braces to arrow function";
|
||||
const removeBracesActionName = "Remove braces from arrow function";
|
||||
const addBracesActionDescription = Diagnostics.Add_braces_to_arrow_function.message;
|
||||
|
@ -19,7 +19,10 @@ namespace ts.refactor.convertArrowFunction {
|
|||
const info = getConvertibleArrowFunctionAtPosition(file, startPosition);
|
||||
if (!info) return undefined;
|
||||
|
||||
const actions: RefactorActionInfo[] = [
|
||||
return [{
|
||||
name: refactorName,
|
||||
description: refactorDescription,
|
||||
actions: [
|
||||
info.addBraces ?
|
||||
{
|
||||
name: addBracesActionName,
|
||||
|
@ -28,12 +31,7 @@ namespace ts.refactor.convertArrowFunction {
|
|||
name: removeBracesActionName,
|
||||
description: removeBracesActionDescription
|
||||
}
|
||||
];
|
||||
|
||||
return [{
|
||||
name: refactorName,
|
||||
description: refactorDescription,
|
||||
actions
|
||||
]
|
||||
}];
|
||||
}
|
||||
|
||||
|
@ -42,9 +40,18 @@ namespace ts.refactor.convertArrowFunction {
|
|||
const info = getConvertibleArrowFunctionAtPosition(file, startPosition);
|
||||
if (!info) return undefined;
|
||||
|
||||
const { addBraces, expression, container } = info;
|
||||
const { expression, container } = info;
|
||||
const changeTracker = textChanges.ChangeTracker.fromContext(context);
|
||||
updateBraces(changeTracker, file, container, expression, addBraces);
|
||||
|
||||
if (_actionName === addBracesActionName) {
|
||||
addBraces(changeTracker, file, container, expression);
|
||||
}
|
||||
else if (_actionName === removeBracesActionName) {
|
||||
removeBraces(changeTracker, file, container, expression);
|
||||
}
|
||||
else {
|
||||
Debug.fail("invalid action");
|
||||
}
|
||||
|
||||
return {
|
||||
renameFilename: undefined,
|
||||
|
@ -53,9 +60,18 @@ namespace ts.refactor.convertArrowFunction {
|
|||
};
|
||||
}
|
||||
|
||||
function updateBraces(changeTracker: textChanges.ChangeTracker, file: SourceFile, container: ArrowFunction, expression: Expression, addBraces: boolean) {
|
||||
const body = addBraces ? createBlock([createReturn(expression)]) : expression;
|
||||
function addBraces(changeTracker: textChanges.ChangeTracker, file: SourceFile, container: ArrowFunction, expression: Expression) {
|
||||
updateBraces(changeTracker, file, container, createBlock([createReturn(expression)]));
|
||||
}
|
||||
|
||||
function removeBraces(changeTracker: textChanges.ChangeTracker, file: SourceFile, container: ArrowFunction, expression: Expression) {
|
||||
if (!isLiteralExpression(expression) && !isIdentifier(expression) && !isParenthesizedExpression(expression) && expression.kind !== SyntaxKind.NullKeyword) {
|
||||
expression = createParen(expression);
|
||||
}
|
||||
updateBraces(changeTracker, file, container, expression);
|
||||
}
|
||||
|
||||
function updateBraces(changeTracker: textChanges.ChangeTracker, file: SourceFile, container: ArrowFunction, body: ConciseBody) {
|
||||
const arrowFunction = updateArrowFunction(
|
||||
container,
|
||||
container.modifiers,
|
||||
|
@ -78,13 +94,16 @@ namespace ts.refactor.convertArrowFunction {
|
|||
expression: container.body
|
||||
};
|
||||
}
|
||||
else if (container.body.statements.length === 1 && isReturnStatement(first(container.body.statements))) {
|
||||
else if (container.body.statements.length === 1) {
|
||||
const firstStatement = first(container.body.statements);
|
||||
if (isReturnStatement(firstStatement)) {
|
||||
return {
|
||||
container,
|
||||
addBraces: false,
|
||||
expression: (<ReturnStatement>first(container.body.statements)).expression
|
||||
expression: firstStatement.expression
|
||||
};
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
}
|
|
@ -114,7 +114,7 @@
|
|||
"refactors/extractSymbol.ts",
|
||||
"refactors/generateGetAccessorAndSetAccessor.ts",
|
||||
"refactors/moveToNewFile.ts",
|
||||
"refactors/convertArrowFunction.ts",
|
||||
"refactors/addOrRemoveBracesToArrowFunction.ts",
|
||||
"sourcemaps.ts",
|
||||
"services.ts",
|
||||
"breakpoints.ts",
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
/// <reference path='fourslash.ts' />
|
||||
|
||||
//// const foo = /*a*/a/*b*/ => { };
|
||||
|
||||
goTo.select("a", "b");
|
||||
verify.not.refactorAvailable("Convert arrow function");
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
goTo.select("a", "b");
|
||||
edit.applyRefactor({
|
||||
refactorName: "Convert arrow function",
|
||||
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; };`,
|
|
@ -0,0 +1,11 @@
|
|||
/// <reference path='fourslash.ts' />
|
||||
|
||||
//// const foo = /*a*/a/*b*/ => { return (1, 2, 3); };
|
||||
|
||||
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 => (1, 2, 3);`,
|
||||
});
|
|
@ -0,0 +1,11 @@
|
|||
/// <reference path='fourslash.ts' />
|
||||
|
||||
//// const foo = /*a*/a/*b*/ => { return 1, 2, 3; };
|
||||
|
||||
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 => (1, 2, 3);`,
|
||||
});
|
|
@ -0,0 +1,11 @@
|
|||
/// <reference path='fourslash.ts' />
|
||||
|
||||
//// const foo = /*a*/a/*b*/ => { return "foo"; };
|
||||
|
||||
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 => "foo";`,
|
||||
});
|
|
@ -0,0 +1,11 @@
|
|||
/// <reference path='fourslash.ts' />
|
||||
|
||||
//// const foo = /*a*/a/*b*/ => { return null; };
|
||||
|
||||
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 => null;`,
|
||||
});
|
|
@ -0,0 +1,11 @@
|
|||
/// <reference path='fourslash.ts' />
|
||||
|
||||
//// const foo = /*a*/a/*b*/ => { return undefined; };
|
||||
|
||||
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 => undefined;`,
|
||||
});
|
|
@ -0,0 +1,11 @@
|
|||
/// <reference path='fourslash.ts' />
|
||||
|
||||
//// const foo = /*a*/a/*b*/ => { return void 0; };
|
||||
|
||||
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);`,
|
||||
});
|
|
@ -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 => ({});`,
|
||||
});
|
|
@ -0,0 +1,11 @@
|
|||
/// <reference path='fourslash.ts' />
|
||||
|
||||
//// const foo = /*a*/a/*b*/ => { return `abc{a}`; };
|
||||
|
||||
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 => \`abc{a}\`;`,
|
||||
});
|
|
@ -0,0 +1,11 @@
|
|||
/// <reference path='fourslash.ts' />
|
||||
|
||||
//// const foo = /*a*/a/*b*/ => { return `abc`; };
|
||||
|
||||
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 => \`abc\`;`,
|
||||
});
|
|
@ -0,0 +1,11 @@
|
|||
/// <reference path='fourslash.ts' />
|
||||
|
||||
//// const foo = /*a*/a/*b*/ => { return a; };
|
||||
|
||||
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 => a;`,
|
||||
});
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
goTo.select("a", "b");
|
||||
edit.applyRefactor({
|
||||
refactorName: "Convert arrow function",
|
||||
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 }); };`,
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
goTo.select("a", "b");
|
||||
edit.applyRefactor({
|
||||
refactorName: "Convert arrow function",
|
||||
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; };`,
|
|
@ -4,8 +4,8 @@
|
|||
|
||||
goTo.select("a", "b");
|
||||
edit.applyRefactor({
|
||||
refactorName: "Convert arrow function",
|
||||
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 => a + 1;`,
|
||||
newContent: `const foo = a => (a + 1);`,
|
||||
});
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
goTo.select("a", "b");
|
||||
edit.applyRefactor({
|
||||
refactorName: "Convert arrow function",
|
||||
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 => ({ a: 1 });`,
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
goTo.select("a", "b");
|
||||
edit.applyRefactor({
|
||||
refactorName: "Convert arrow function",
|
||||
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 => 1;`,
|
|
@ -6,4 +6,4 @@
|
|||
//// };
|
||||
|
||||
goTo.select("a", "b");
|
||||
verify.not.refactorAvailable("Convert arrow function");
|
||||
verify.not.refactorAvailable("Add or remove braces in an arrow function");
|
|
@ -0,0 +1,11 @@
|
|||
/// <reference path='fourslash.ts' />
|
||||
|
||||
//// const foo = /*a*/a/*b*/ => (1, 2, 3);
|
||||
|
||||
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 => { return (1, 2, 3); };`,
|
||||
});
|
Loading…
Reference in a new issue