Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 32be0c7

Browse filesBrowse files
committed
add tests and fix
1 parent 5a69d9c commit 32be0c7
Copy full SHA for 32be0c7

25 files changed

+178-44Lines changed: 178 additions & 44 deletions
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/compiler/diagnosticMessages.json‎

Copy file name to clipboardExpand all lines: src/compiler/diagnosticMessages.json
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4281,7 +4281,7 @@
42814281
"category": "Message",
42824282
"code": 95054
42834283
},
4284-
"Convert arrow function": {
4284+
"Add or remove braces in an arrow function": {
42854285
"category": "Message",
42864286
"code": 95055
42874287
},
Collapse file

‎src/harness/tsconfig.json‎

Copy file name to clipboardExpand all lines: src/harness/tsconfig.json
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
"../services/refactors/extractSymbol.ts",
122122
"../services/refactors/generateGetAccessorAndSetAccessor.ts",
123123
"../services/refactors/moveToNewFile.ts",
124-
"../services/refactors/convertArrowFunction.ts",
124+
"../services/refactors/addOrRemoveBracesToArrowFunction.ts",
125125
"../services/sourcemaps.ts",
126126
"../services/services.ts",
127127
"../services/breakpoints.ts",
Collapse file

‎src/server/tsconfig.json‎

Copy file name to clipboardExpand all lines: src/server/tsconfig.json
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
"../services/refactors/extractSymbol.ts",
118118
"../services/refactors/generateGetAccessorAndSetAccessor.ts",
119119
"../services/refactors/moveToNewFile.ts",
120-
"../services/refactors/convertArrowFunction.ts",
120+
"../services/refactors/addOrRemoveBracesToArrowFunction.ts",
121121
"../services/sourcemaps.ts",
122122
"../services/services.ts",
123123
"../services/breakpoints.ts",
Collapse file

‎src/server/tsconfig.library.json‎

Copy file name to clipboardExpand all lines: src/server/tsconfig.library.json
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
"../services/refactors/extractSymbol.ts",
124124
"../services/refactors/generateGetAccessorAndSetAccessor.ts",
125125
"../services/refactors/moveToNewFile.ts",
126-
"../services/refactors/convertArrowFunction.ts",
126+
"../services/refactors/addOrRemoveBracesToArrowFunction.ts",
127127
"../services/sourcemaps.ts",
128128
"../services/services.ts",
129129
"../services/breakpoints.ts",
Collapse file

‎…rvices/refactors/convertArrowFunction.ts‎ ‎…tors/addOrRemoveBracesToArrowFunction.ts‎src/services/refactors/convertArrowFunction.ts renamed to src/services/refactors/addOrRemoveBracesToArrowFunction.ts src/services/refactors/convertArrowFunction.ts renamed to src/services/refactors/addOrRemoveBracesToArrowFunction.ts

Copy file name to clipboardExpand all lines: src/services/refactors/addOrRemoveBracesToArrowFunction.ts
+44-25Lines changed: 44 additions & 25 deletions
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
}
Collapse file

‎src/services/tsconfig.json‎

Copy file name to clipboardExpand all lines: src/services/tsconfig.json
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
"refactors/extractSymbol.ts",
115115
"refactors/generateGetAccessorAndSetAccessor.ts",
116116
"refactors/moveToNewFile.ts",
117-
"refactors/convertArrowFunction.ts",
117+
"refactors/addOrRemoveBracesToArrowFunction.ts",
118118
"sourcemaps.ts",
119119
"services.ts",
120120
"breakpoints.ts",
Collapse file

‎tests/cases/fourslash/refactorAddBracesToArrowFunction7.ts‎

Copy file name to clipboardExpand all lines: tests/cases/fourslash/refactorAddBracesToArrowFunction7.ts
-6Lines changed: 0 additions & 6 deletions
This file was deleted.
Collapse file

‎…ash/refactorAddBracesToArrowFunction1.ts‎ ‎…ctorAddOrRemoveBracesToArrowFunction1.ts‎tests/cases/fourslash/refactorAddBracesToArrowFunction1.ts renamed to tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction1.ts tests/cases/fourslash/refactorAddBracesToArrowFunction1.ts renamed to tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction1.ts

Copy file name to clipboardExpand all lines: tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction1.ts
+1-1Lines changed: 1 addition & 1 deletion
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; };`,
Collapse file
+11Lines changed: 11 additions & 0 deletions
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+
});
Collapse file
+11Lines changed: 11 additions & 0 deletions
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+
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.