1
1
/* @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 ;
5
5
const addBracesActionName = "Add braces to arrow function" ;
6
6
const removeBracesActionName = "Remove braces from arrow function" ;
7
7
const addBracesActionDescription = Diagnostics . Add_braces_to_arrow_function . message ;
@@ -19,21 +19,19 @@ namespace ts.refactor.convertArrowFunction {
19
19
const info = getConvertibleArrowFunctionAtPosition ( file , startPosition ) ;
20
20
if ( ! info ) return undefined ;
21
21
22
- const actions : RefactorActionInfo [ ] = [
23
- info . addBraces ?
24
- {
25
- name : addBracesActionName ,
26
- description : addBracesActionDescription
27
- } : {
28
- name : removeBracesActionName ,
29
- description : removeBracesActionDescription
30
- }
31
- ] ;
32
-
33
22
return [ {
34
23
name : refactorName ,
35
24
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
+ ]
37
35
} ] ;
38
36
}
39
37
@@ -42,9 +40,18 @@ namespace ts.refactor.convertArrowFunction {
42
40
const info = getConvertibleArrowFunctionAtPosition ( file , startPosition ) ;
43
41
if ( ! info ) return undefined ;
44
42
45
- const { addBraces , expression, container } = info ;
43
+ const { expression, container } = info ;
46
44
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
+ }
48
55
49
56
return {
50
57
renameFilename : undefined ,
@@ -53,9 +60,18 @@ namespace ts.refactor.convertArrowFunction {
53
60
} ;
54
61
}
55
62
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
+ }
58
73
74
+ function updateBraces ( changeTracker : textChanges . ChangeTracker , file : SourceFile , container : ArrowFunction , body : ConciseBody ) {
59
75
const arrowFunction = updateArrowFunction (
60
76
container ,
61
77
container . modifiers ,
@@ -78,12 +94,15 @@ namespace ts.refactor.convertArrowFunction {
78
94
expression : container . body
79
95
} ;
80
96
}
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
+ }
87
106
}
88
107
return undefined ;
89
108
}
0 commit comments