Skip to content

Commit 4e7b31b

Browse files
committed
add tests and fix
1 parent 29545d9 commit 4e7b31b

25 files changed

+179
-39
lines changed

src/compiler/diagnosticMessages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4166,7 +4166,7 @@
41664166
"category": "Message",
41674167
"code": 95046
41684168
},
4169-
"Convert arrow function": {
4169+
"Add or remove braces in an arrow function": {
41704170
"category": "Message",
41714171
"code": 95047
41724172
},

src/harness/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
"../services/codefixes/fixes.ts",
116116
"../services/refactors/extractSymbol.ts",
117117
"../services/refactors/generateGetAccessorAndSetAccessor.ts",
118-
"../services/refactors/convertArrowFunction.ts",
118+
"../services/refactors/addOrRemoveBracesToArrowFunction.ts",
119119
"../services/refactors/refactors.ts",
120120
"../services/sourcemaps.ts",
121121
"../services/services.ts",

src/server/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
"../services/codefixes/fixes.ts",
112112
"../services/refactors/extractSymbol.ts",
113113
"../services/refactors/generateGetAccessorAndSetAccessor.ts",
114-
"../services/refactors/convertArrowFunction.ts",
114+
"../services/refactors/addOrRemoveBracesToArrowFunction.ts",
115115
"../services/refactors/refactors.ts",
116116
"../services/sourcemaps.ts",
117117
"../services/services.ts",

src/server/tsconfig.library.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
"../services/codefixes/fixes.ts",
118118
"../services/refactors/extractSymbol.ts",
119119
"../services/refactors/generateGetAccessorAndSetAccessor.ts",
120-
"../services/refactors/convertArrowFunction.ts",
120+
"../services/refactors/addOrRemoveBracesToArrowFunction.ts",
121121
"../services/refactors/refactors.ts",
122122
"../services/sourcemaps.ts",
123123
"../services/services.ts",

src/services/refactors/convertArrowFunction.ts renamed to src/services/refactors/addOrRemoveBracesToArrowFunction.ts

+44-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @internal */
2-
namespace ts.refactor.convertArrowFunction {
3-
const refactorName = "Convert arrow function";
4-
const refactorDescription = Diagnostics.Convert_arrow_function.message;
2+
namespace ts.refactor.addOrRemoveBracesToArrowFunction {
3+
const refactorName = "Add or remove braces in an arrow function";
4+
const refactorDescription = Diagnostics.Add_or_remove_braces_in_an_arrow_function.message;
55
const addBracesActionName = "Add braces to arrow function";
66
const removeBracesActionName = "Remove braces from arrow function";
77
const addBracesActionDescription = Diagnostics.Add_braces_to_arrow_function.message;
@@ -19,21 +19,19 @@ namespace ts.refactor.convertArrowFunction {
1919
const info = getConvertibleArrowFunctionAtPosition(file, startPosition);
2020
if (!info) return undefined;
2121

22-
const actions: RefactorActionInfo[] = [
23-
info.addBraces ?
24-
{
25-
name: addBracesActionName,
26-
description: addBracesActionDescription
27-
} : {
28-
name: removeBracesActionName,
29-
description: removeBracesActionDescription
30-
}
31-
];
32-
3322
return [{
3423
name: refactorName,
3524
description: refactorDescription,
36-
actions
25+
actions: [
26+
info.addBraces ?
27+
{
28+
name: addBracesActionName,
29+
description: addBracesActionDescription
30+
} : {
31+
name: removeBracesActionName,
32+
description: removeBracesActionDescription
33+
}
34+
]
3735
}];
3836
}
3937

@@ -42,9 +40,18 @@ namespace ts.refactor.convertArrowFunction {
4240
const info = getConvertibleArrowFunctionAtPosition(file, startPosition);
4341
if (!info) return undefined;
4442

45-
const { addBraces, expression, container } = info;
43+
const { expression, container } = info;
4644
const changeTracker = textChanges.ChangeTracker.fromContext(context);
47-
updateBraces(changeTracker, file, container, expression, addBraces);
45+
46+
if (_actionName === addBracesActionName) {
47+
addBraces(changeTracker, file, container, expression);
48+
}
49+
else if (_actionName === removeBracesActionName) {
50+
removeBraces(changeTracker, file, container, expression);
51+
}
52+
else {
53+
Debug.fail("invalid action");
54+
}
4855

4956
return {
5057
renameFilename: undefined,
@@ -53,9 +60,18 @@ namespace ts.refactor.convertArrowFunction {
5360
};
5461
}
5562

56-
function updateBraces(changeTracker: textChanges.ChangeTracker, file: SourceFile, container: ArrowFunction, expression: Expression, addBraces: boolean) {
57-
const body = addBraces ? createBlock([createReturn(expression)]) : expression;
63+
function addBraces(changeTracker: textChanges.ChangeTracker, file: SourceFile, container: ArrowFunction, expression: Expression) {
64+
updateBraces(changeTracker, file, container, createBlock([createReturn(expression)]));
65+
}
66+
67+
function removeBraces(changeTracker: textChanges.ChangeTracker, file: SourceFile, container: ArrowFunction, expression: Expression) {
68+
if (!isLiteralExpression(expression) && !isIdentifier(expression) && !isParenthesizedExpression(expression) && expression.kind !== SyntaxKind.NullKeyword) {
69+
expression = createParen(expression);
70+
}
71+
updateBraces(changeTracker, file, container, expression);
72+
}
5873

74+
function updateBraces(changeTracker: textChanges.ChangeTracker, file: SourceFile, container: ArrowFunction, body: ConciseBody) {
5975
const arrowFunction = updateArrowFunction(
6076
container,
6177
container.modifiers,
@@ -78,12 +94,15 @@ namespace ts.refactor.convertArrowFunction {
7894
expression: container.body
7995
};
8096
}
81-
else if (container.body.statements.length === 1 && isReturnStatement(first(container.body.statements))) {
82-
return {
83-
container,
84-
addBraces: false,
85-
expression: (<ReturnStatement>first(container.body.statements)).expression
86-
};
97+
else if (container.body.statements.length === 1) {
98+
const firstStatement = first(container.body.statements);
99+
if (isReturnStatement(firstStatement)) {
100+
return {
101+
container,
102+
addBraces: false,
103+
expression: firstStatement.expression
104+
};
105+
}
87106
}
88107
return undefined;
89108
}

src/services/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
"codefixes/fixes.ts",
109109
"refactors/extractSymbol.ts",
110110
"refactors/generateGetAccessorAndSetAccessor.ts",
111-
"refactors/convertArrowFunction.ts",
111+
"refactors/addOrRemoveBracesToArrowFunction.ts",
112112
"refactors/refactors.ts",
113113
"sourcemaps.ts",
114114
"services.ts",

tests/cases/fourslash/refactorAddBracesToArrowFunction1.ts renamed to tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction1.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
goTo.select("a", "b");
66
edit.applyRefactor({
7-
refactorName: "Convert arrow function",
7+
refactorName: "Add or remove braces in an arrow function",
88
actionName: "Add braces to arrow function",
99
actionDescription: "Add braces to arrow function",
1010
newContent: `const foo = a => { return a + 1; };`,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const foo = /*a*/a/*b*/ => { return (1, 2, 3); };
4+
5+
goTo.select("a", "b");
6+
edit.applyRefactor({
7+
refactorName: "Add or remove braces in an arrow function",
8+
actionName: "Remove braces from arrow function",
9+
actionDescription: "Remove braces from arrow function",
10+
newContent: `const foo = a => (1, 2, 3);`,
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const foo = /*a*/a/*b*/ => { return 1, 2, 3; };
4+
5+
goTo.select("a", "b");
6+
edit.applyRefactor({
7+
refactorName: "Add or remove braces in an arrow function",
8+
actionName: "Remove braces from arrow function",
9+
actionDescription: "Remove braces from arrow function",
10+
newContent: `const foo = a => (1, 2, 3);`,
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const foo = /*a*/a/*b*/ => { return "foo"; };
4+
5+
goTo.select("a", "b");
6+
edit.applyRefactor({
7+
refactorName: "Add or remove braces in an arrow function",
8+
actionName: "Remove braces from arrow function",
9+
actionDescription: "Remove braces from arrow function",
10+
newContent: `const foo = a => "foo";`,
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const foo = /*a*/a/*b*/ => { return null; };
4+
5+
goTo.select("a", "b");
6+
edit.applyRefactor({
7+
refactorName: "Add or remove braces in an arrow function",
8+
actionName: "Remove braces from arrow function",
9+
actionDescription: "Remove braces from arrow function",
10+
newContent: `const foo = a => null;`,
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const foo = /*a*/a/*b*/ => { return undefined; };
4+
5+
goTo.select("a", "b");
6+
edit.applyRefactor({
7+
refactorName: "Add or remove braces in an arrow function",
8+
actionName: "Remove braces from arrow function",
9+
actionDescription: "Remove braces from arrow function",
10+
newContent: `const foo = a => undefined;`,
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const foo = /*a*/a/*b*/ => { return void 0; };
4+
5+
goTo.select("a", "b");
6+
edit.applyRefactor({
7+
refactorName: "Add or remove braces in an arrow function",
8+
actionName: "Remove braces from arrow function",
9+
actionDescription: "Remove braces from arrow function",
10+
newContent: `const foo = a => (void 0);`,
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const foo = /*a*/a/*b*/ => { return {}; };
4+
5+
goTo.select("a", "b");
6+
edit.applyRefactor({
7+
refactorName: "Add or remove braces in an arrow function",
8+
actionName: "Remove braces from arrow function",
9+
actionDescription: "Remove braces from arrow function",
10+
newContent: `const foo = a => ({});`,
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const foo = /*a*/a/*b*/ => { return `abc{a}`; };
4+
5+
goTo.select("a", "b");
6+
edit.applyRefactor({
7+
refactorName: "Add or remove braces in an arrow function",
8+
actionName: "Remove braces from arrow function",
9+
actionDescription: "Remove braces from arrow function",
10+
newContent: `const foo = a => \`abc{a}\`;`,
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const foo = /*a*/a/*b*/ => { return `abc`; };
4+
5+
goTo.select("a", "b");
6+
edit.applyRefactor({
7+
refactorName: "Add or remove braces in an arrow function",
8+
actionName: "Remove braces from arrow function",
9+
actionDescription: "Remove braces from arrow function",
10+
newContent: `const foo = a => \`abc\`;`,
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const foo = /*a*/a/*b*/ => { return a; };
4+
5+
goTo.select("a", "b");
6+
edit.applyRefactor({
7+
refactorName: "Add or remove braces in an arrow function",
8+
actionName: "Remove braces from arrow function",
9+
actionDescription: "Remove braces from arrow function",
10+
newContent: `const foo = a => a;`,
11+
});

tests/cases/fourslash/refactorAddBracesToArrowFunction2.ts renamed to tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction2.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
goTo.select("a", "b");
66
edit.applyRefactor({
7-
refactorName: "Convert arrow function",
7+
refactorName: "Add or remove braces in an arrow function",
88
actionName: "Add braces to arrow function",
99
actionDescription: "Add braces to arrow function",
1010
newContent: `const foo = a => { return ({ a: 1 }); };`,

tests/cases/fourslash/refactorAddBracesToArrowFunction3.ts renamed to tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction3.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
goTo.select("a", "b");
66
edit.applyRefactor({
7-
refactorName: "Convert arrow function",
7+
refactorName: "Add or remove braces in an arrow function",
88
actionName: "Add braces to arrow function",
99
actionDescription: "Add braces to arrow function",
1010
newContent: `const foo = a => { return 1; };`,

tests/cases/fourslash/refactorAddBracesToArrowFunction4.ts renamed to tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction4.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
goTo.select("a", "b");
66
edit.applyRefactor({
7-
refactorName: "Convert arrow function",
7+
refactorName: "Add or remove braces in an arrow function",
88
actionName: "Remove braces from arrow function",
99
actionDescription: "Remove braces from arrow function",
10-
newContent: `const foo = a => a + 1;`,
10+
newContent: `const foo = a => (a + 1);`,
1111
});

tests/cases/fourslash/refactorAddBracesToArrowFunction5.ts renamed to tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction5.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
goTo.select("a", "b");
66
edit.applyRefactor({
7-
refactorName: "Convert arrow function",
7+
refactorName: "Add or remove braces in an arrow function",
88
actionName: "Remove braces from arrow function",
99
actionDescription: "Remove braces from arrow function",
1010
newContent: `const foo = a => ({ a: 1 });`,

tests/cases/fourslash/refactorAddBracesToArrowFunction6.ts renamed to tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction6.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
goTo.select("a", "b");
66
edit.applyRefactor({
7-
refactorName: "Convert arrow function",
7+
refactorName: "Add or remove braces in an arrow function",
88
actionName: "Remove braces from arrow function",
99
actionDescription: "Remove braces from arrow function",
1010
newContent: `const foo = a => 1;`,

tests/cases/fourslash/refactorAddBracesToArrowFunction7.ts renamed to tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction7.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
//// const foo = /*a*/a/*b*/ => { };
44

55
goTo.select("a", "b");
6-
verify.not.refactorAvailable("Convert arrow function");
6+
verify.not.refactorAvailable("Add or remove braces in an arrow function");

tests/cases/fourslash/refactorAddBracesToArrowFunction8.ts renamed to tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction8.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
//// };
77

88
goTo.select("a", "b");
9-
verify.not.refactorAvailable("Convert arrow function");
9+
verify.not.refactorAvailable("Add or remove braces in an arrow function");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const foo = /*a*/a/*b*/ => (1, 2, 3);
4+
5+
goTo.select("a", "b");
6+
edit.applyRefactor({
7+
refactorName: "Add or remove braces in an arrow function",
8+
actionName: "Add braces to arrow function",
9+
actionDescription: "Add braces to arrow function",
10+
newContent: `const foo = a => { return (1, 2, 3); };`,
11+
});

0 commit comments

Comments
 (0)