Skip to content

Commit

Permalink
add tests and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Kingwl committed Apr 17, 2018
1 parent e1b1d2b commit 07d1c86
Show file tree
Hide file tree
Showing 25 changed files with 179 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4166,7 +4166,7 @@
"category": "Message",
"code": 95046
},
"Convert arrow function": {
"Add or remove braces in an arrow function": {
"category": "Message",
"code": 95047
},
Expand Down
2 changes: 1 addition & 1 deletion src/harness/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
"../services/codefixes/useDefaultImport.ts",
"../services/refactors/extractSymbol.ts",
"../services/refactors/generateGetAccessorAndSetAccessor.ts",
"../services/refactors/convertArrowFunction.ts",
"../services/refactors/addOrRemoveBracesToArrowFunction.ts",
"../services/sourcemaps.ts",
"../services/services.ts",
"../services/breakpoints.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
"../services/codefixes/useDefaultImport.ts",
"../services/refactors/extractSymbol.ts",
"../services/refactors/generateGetAccessorAndSetAccessor.ts",
"../services/refactors/convertArrowFunction.ts",
"../services/refactors/addOrRemoveBracesToArrowFunction.ts",
"../services/sourcemaps.ts",
"../services/services.ts",
"../services/breakpoints.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/server/tsconfig.library.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
"../services/codefixes/useDefaultImport.ts",
"../services/refactors/extractSymbol.ts",
"../services/refactors/generateGetAccessorAndSetAccessor.ts",
"../services/refactors/convertArrowFunction.ts",
"../services/refactors/addOrRemoveBracesToArrowFunction.ts",
"../services/sourcemaps.ts",
"../services/services.ts",
"../services/breakpoints.ts",
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,21 +19,19 @@ namespace ts.refactor.convertArrowFunction {
const info = getConvertibleArrowFunctionAtPosition(file, startPosition);
if (!info) return undefined;

const actions: RefactorActionInfo[] = [
info.addBraces ?
{
name: addBracesActionName,
description: addBracesActionDescription
} : {
name: removeBracesActionName,
description: removeBracesActionDescription
}
];

return [{
name: refactorName,
description: refactorDescription,
actions
actions: [
info.addBraces ?
{
name: addBracesActionName,
description: addBracesActionDescription
} : {
name: removeBracesActionName,
description: removeBracesActionDescription
}
]
}];
}

Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -78,12 +94,15 @@ namespace ts.refactor.convertArrowFunction {
expression: container.body
};
}
else if (container.body.statements.length === 1 && isReturnStatement(first(container.body.statements))) {
return {
container,
addBraces: false,
expression: (<ReturnStatement>first(container.body.statements)).expression
};
else if (container.body.statements.length === 1) {
const firstStatement = first(container.body.statements);
if (isReturnStatement(firstStatement)) {
return {
container,
addBraces: false,
expression: firstStatement.expression
};
}
}
return undefined;
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
"codefixes/useDefaultImport.ts",
"refactors/extractSymbol.ts",
"refactors/generateGetAccessorAndSetAccessor.ts",
"refactors/convertArrowFunction.ts",
"refactors/addOrRemoveBracesToArrowFunction.ts",
"sourcemaps.ts",
"services.ts",
"breakpoints.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; };`,
Expand Down
Original file line number Diff line number Diff line change
@@ -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);`,
});
Original file line number Diff line number Diff line change
@@ -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);`,
});
Original file line number Diff line number Diff line change
@@ -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";`,
});
Original file line number Diff line number Diff line change
@@ -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;`,
});
Original file line number Diff line number Diff line change
@@ -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;`,
});
Original file line number Diff line number Diff line change
@@ -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);`,
});
Original file line number Diff line number Diff line change
@@ -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 => ({});`,
});
Original file line number Diff line number Diff line change
@@ -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}\`;`,
});
Original file line number Diff line number Diff line change
@@ -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\`;`,
});
Original file line number Diff line number Diff line change
@@ -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;`,
});
Original file line number Diff line number Diff line change
Expand Up @@ -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 }); };`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; };`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);`,
});
Original file line number Diff line number Diff line change
Expand Up @@ -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 });`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
//// const foo = /*a*/a/*b*/ => { };

goTo.select("a", "b");
verify.not.refactorAvailable("Convert arrow function");
verify.not.refactorAvailable("Add or remove braces in an arrow function");
Original file line number Diff line number Diff line change
Expand Up @@ -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");
11 changes: 11 additions & 0 deletions tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction9.ts
Original file line number Diff line number Diff line change
@@ -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); };`,
});

0 comments on commit 07d1c86

Please sign in to comment.