2
2
namespace ts . refactor . addOrRemoveBracesToArrowFunction {
3
3
const refactorName = "Add or remove braces in an arrow function" ;
4
4
const refactorDescription = Diagnostics . Add_or_remove_braces_in_an_arrow_function . message ;
5
- const addBracesActionName = "Add braces to arrow function" ;
6
- const removeBracesActionName = "Remove braces from arrow function" ;
7
- const addBracesActionDescription = Diagnostics . Add_braces_to_arrow_function . message ;
8
- const removeBracesActionDescription = Diagnostics . Remove_braces_from_arrow_function . message ;
9
- registerRefactor ( refactorName , { getEditsForAction, getAvailableActions } ) ;
10
5
11
- interface Info {
6
+ const addBracesAction = {
7
+ name : "Add braces to arrow function" ,
8
+ description : Diagnostics . Add_braces_to_arrow_function . message ,
9
+ kind : "refactor.rewrite.arrow.braces.add" ,
10
+ } ;
11
+ const removeBracesAction = {
12
+ name : "Remove braces from arrow function" ,
13
+ description : Diagnostics . Remove_braces_from_arrow_function . message ,
14
+ kind : "refactor.rewrite.arrow.braces.remove"
15
+ } ;
16
+ registerRefactor ( refactorName , {
17
+ kinds : [ removeBracesAction . kind ] ,
18
+ getEditsForAction,
19
+ getAvailableActions } ) ;
20
+
21
+ interface FunctionBracesInfo {
12
22
func : ArrowFunction ;
13
23
expression : Expression | undefined ;
14
24
returnStatement ?: ReturnStatement ;
15
25
addBraces : boolean ;
16
26
}
17
27
18
- type InfoOrError = {
19
- info : Info ,
20
- error ?: never
21
- } | {
22
- info ?: never ,
23
- error : string
24
- } ;
25
-
26
28
function getAvailableActions ( context : RefactorContext ) : readonly ApplicableRefactorInfo [ ] {
27
29
const { file, startPosition, triggerReason } = context ;
28
30
const info = getConvertibleArrowFunctionAtPosition ( file , startPosition , triggerReason === "invoked" ) ;
29
31
if ( ! info ) return emptyArray ;
30
32
31
- if ( info . error === undefined ) {
33
+ if ( ! isRefactorErrorInfo ( info ) ) {
32
34
return [ {
33
35
name : refactorName ,
34
36
description : refactorDescription ,
35
37
actions : [
36
- info . info . addBraces ?
37
- {
38
- name : addBracesActionName ,
39
- description : addBracesActionDescription
40
- } : {
41
- name : removeBracesActionName ,
42
- description : removeBracesActionDescription
43
- }
38
+ info . addBraces ? addBracesAction : removeBracesAction
44
39
]
45
40
} ] ;
46
41
}
@@ -49,15 +44,10 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
49
44
return [ {
50
45
name : refactorName ,
51
46
description : refactorDescription ,
52
- actions : [ {
53
- name : addBracesActionName ,
54
- description : addBracesActionDescription ,
55
- notApplicableReason : info . error
56
- } , {
57
- name : removeBracesActionName ,
58
- description : removeBracesActionDescription ,
59
- notApplicableReason : info . error
60
- } ]
47
+ actions : [
48
+ { ...addBracesAction , notApplicableReason : info . error } ,
49
+ { ...removeBracesAction , notApplicableReason : info . error } ,
50
+ ]
61
51
} ] ;
62
52
}
63
53
@@ -67,19 +57,19 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
67
57
function getEditsForAction ( context : RefactorContext , actionName : string ) : RefactorEditInfo | undefined {
68
58
const { file, startPosition } = context ;
69
59
const info = getConvertibleArrowFunctionAtPosition ( file , startPosition ) ;
70
- if ( ! info || ! info . info ) return undefined ;
60
+ Debug . assert ( info && ! isRefactorErrorInfo ( info ) , "Expected applicable refactor info" ) ;
71
61
72
- const { expression, returnStatement, func } = info . info ;
62
+ const { expression, returnStatement, func } = info ;
73
63
74
64
let body : ConciseBody ;
75
65
76
- if ( actionName === addBracesActionName ) {
66
+ if ( actionName === addBracesAction . name ) {
77
67
const returnStatement = factory . createReturnStatement ( expression ) ;
78
68
body = factory . createBlock ( [ returnStatement ] , /* multiLine */ true ) ;
79
69
suppressLeadingAndTrailingTrivia ( body ) ;
80
70
copyLeadingComments ( expression ! , returnStatement , file , SyntaxKind . MultiLineCommentTrivia , /* hasTrailingNewLine */ true ) ;
81
71
}
82
- else if ( actionName === removeBracesActionName && returnStatement ) {
72
+ else if ( actionName === removeBracesAction . name && returnStatement ) {
83
73
const actualExpression = expression || factory . createVoidZero ( ) ;
84
74
body = needsParentheses ( actualExpression ) ? factory . createParenthesizedExpression ( actualExpression ) : actualExpression ;
85
75
suppressLeadingAndTrailingTrivia ( body ) ;
@@ -98,7 +88,7 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
98
88
return { renameFilename : undefined , renameLocation : undefined , edits } ;
99
89
}
100
90
101
- function getConvertibleArrowFunctionAtPosition ( file : SourceFile , startPosition : number , considerFunctionBodies = true ) : InfoOrError | undefined {
91
+ function getConvertibleArrowFunctionAtPosition ( file : SourceFile , startPosition : number , considerFunctionBodies = true , kind ?: string ) : FunctionBracesInfo | RefactorErrorInfo | undefined {
102
92
const node = getTokenAtPosition ( file , startPosition ) ;
103
93
const func = getContainingFunction ( node ) ;
104
94
@@ -118,26 +108,13 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
118
108
return undefined ;
119
109
}
120
110
121
- if ( isExpression ( func . body ) ) {
122
- return {
123
- info : {
124
- func,
125
- addBraces : true ,
126
- expression : func . body
127
- }
128
- } ;
111
+ if ( refactorKindBeginsWith ( addBracesAction . kind , kind ) && isExpression ( func . body ) ) {
112
+ return { func, addBraces : true , expression : func . body } ;
129
113
}
130
- else if ( func . body . statements . length === 1 ) {
114
+ else if ( refactorKindBeginsWith ( removeBracesAction . kind , kind ) && isBlock ( func . body ) && func . body . statements . length === 1 ) {
131
115
const firstStatement = first ( func . body . statements ) ;
132
116
if ( isReturnStatement ( firstStatement ) ) {
133
- return {
134
- info : {
135
- func,
136
- addBraces : false ,
137
- expression : firstStatement . expression ,
138
- returnStatement : firstStatement
139
- }
140
- } ;
117
+ return { func, addBraces : false , expression : firstStatement . expression , returnStatement : firstStatement } ;
141
118
}
142
119
}
143
120
return undefined ;
0 commit comments