@@ -10,20 +10,20 @@ namespace ts.codefix {
10
10
Diagnostics . Argument_of_type_0_is_not_assignable_to_parameter_of_type_1 . code
11
11
] ;
12
12
13
- enum FixKind {
13
+ enum ProblemKind {
14
14
MissingReturnStatement ,
15
15
MissingParentheses
16
16
}
17
17
18
18
interface MissingReturnInfo {
19
- kind : FixKind . MissingReturnStatement ;
19
+ kind : ProblemKind . MissingReturnStatement ;
20
20
declaration : FunctionLikeDeclaration ;
21
21
expression : Expression ;
22
22
statement : Statement ;
23
23
}
24
24
25
25
interface MissingParenInfo {
26
- kind : FixKind . MissingParentheses ;
26
+ kind : ProblemKind . MissingParentheses ;
27
27
declaration : ArrowFunction ;
28
28
expression : Expression ;
29
29
statement : Statement ;
@@ -39,7 +39,7 @@ namespace ts.codefix {
39
39
const info = getInfo ( program . getTypeChecker ( ) , sourceFile , start , errorCode ) ;
40
40
if ( ! info ) return undefined ;
41
41
42
- if ( info . kind === FixKind . MissingReturnStatement ) {
42
+ if ( info . kind === ProblemKind . MissingReturnStatement ) {
43
43
return append (
44
44
[ getActionForfixAddReturnStatement ( context , info . expression , info . statement ) ] ,
45
45
isArrowFunction ( info . declaration ) ? getActionForfixRemoveBlockBodyBrace ( context , info . declaration , info . expression ) : undefined ) ;
@@ -70,33 +70,14 @@ namespace ts.codefix {
70
70
} ) ,
71
71
} ) ;
72
72
73
- function updateFunctionLikeBody ( declaration : FunctionLikeDeclaration , body : Block ) : FunctionLikeDeclaration {
74
- switch ( declaration . kind ) {
75
- case SyntaxKind . FunctionDeclaration :
76
- return createFunctionDeclaration ( declaration . decorators , declaration . modifiers , declaration . asteriskToken , declaration . name , declaration . typeParameters , declaration . parameters , declaration . type , body ) ;
77
- case SyntaxKind . MethodDeclaration :
78
- return createMethod ( declaration . decorators , declaration . modifiers , declaration . asteriskToken , declaration . name , declaration . questionToken , declaration . typeParameters , declaration . parameters , declaration . type , body ) ;
79
- case SyntaxKind . GetAccessor :
80
- return createGetAccessor ( declaration . decorators , declaration . modifiers , declaration . name , declaration . parameters , declaration . type , body ) ;
81
- case SyntaxKind . SetAccessor :
82
- return createSetAccessor ( declaration . decorators , declaration . modifiers , declaration . name , declaration . parameters , body ) ;
83
- case SyntaxKind . Constructor :
84
- return createConstructor ( declaration . decorators , declaration . modifiers , declaration . parameters , body ) ;
85
- case SyntaxKind . FunctionExpression :
86
- return createFunctionExpression ( declaration . modifiers , declaration . asteriskToken , declaration . name , declaration . typeParameters , declaration . parameters , declaration . type , body ) ;
87
- case SyntaxKind . ArrowFunction :
88
- return createArrowFunction ( declaration . modifiers , declaration . typeParameters , declaration . parameters , declaration . type , declaration . equalsGreaterThanToken , body ) ;
89
- }
90
- }
91
-
92
73
function getFixInfo ( checker : TypeChecker , declaration : FunctionLikeDeclaration , expectType : Type , isFunctionType : boolean ) : Info | undefined {
93
74
if ( ! declaration . body || ! isBlock ( declaration . body ) || length ( declaration . body . statements ) !== 1 ) return undefined ;
94
75
95
76
const firstStatement = first ( declaration . body . statements ) ;
96
77
if ( isExpressionStatement ( firstStatement ) && checkFixedAssignableTo ( checker , declaration , firstStatement . expression , expectType , isFunctionType ) ) {
97
78
return {
98
79
declaration,
99
- kind : FixKind . MissingReturnStatement ,
80
+ kind : ProblemKind . MissingReturnStatement ,
100
81
expression : firstStatement . expression ,
101
82
statement : firstStatement
102
83
} ;
@@ -106,12 +87,12 @@ namespace ts.codefix {
106
87
if ( checkFixedAssignableTo ( checker , declaration , node , expectType , isFunctionType ) ) {
107
88
return isArrowFunction ( declaration ) ? {
108
89
declaration,
109
- kind : FixKind . MissingParentheses ,
90
+ kind : ProblemKind . MissingParentheses ,
110
91
expression : node ,
111
92
statement : firstStatement
112
93
} : {
113
94
declaration,
114
- kind : FixKind . MissingReturnStatement ,
95
+ kind : ProblemKind . MissingReturnStatement ,
115
96
expression : node ,
116
97
statement : firstStatement
117
98
} ;
@@ -124,7 +105,7 @@ namespace ts.codefix {
124
105
if ( checkFixedAssignableTo ( checker , declaration , node , expectType , isFunctionType ) ) {
125
106
return {
126
107
declaration,
127
- kind : FixKind . MissingReturnStatement ,
108
+ kind : ProblemKind . MissingReturnStatement ,
128
109
expression : node ,
129
110
statement : firstStatement
130
111
} ;
@@ -183,11 +164,16 @@ namespace ts.codefix {
183
164
}
184
165
185
166
function addReturnStatement ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , expression : Expression , statement : Statement ) {
167
+ suppressLeadingAndTrailingTrivia ( expression ) ;
186
168
changes . replaceNode ( sourceFile , statement , createReturn ( expression ) ) ;
187
169
}
188
170
189
171
function removeBlockBodyBrace ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , declaration : ArrowFunction , expression : Expression , withParen : boolean ) {
190
- changes . replaceNode ( sourceFile , declaration . body , ( withParen || needsParentheses ( expression ) ) ? createParen ( expression ) : expression ) ;
172
+ const newBody = ( withParen || needsParentheses ( expression ) ) ? createParen ( expression ) : expression ;
173
+ suppressLeadingAndTrailingTrivia ( expression ) ;
174
+ copyComments ( expression , newBody ) ;
175
+
176
+ changes . replaceNode ( sourceFile , declaration . body , newBody ) ;
191
177
}
192
178
193
179
function wrapBlockWithParen ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , declaration : ArrowFunction , expression : Expression ) {
@@ -206,6 +192,6 @@ namespace ts.codefix {
206
192
207
193
function getActionForfixWrapTheBlockWithParen ( context : CodeFixContext , declaration : ArrowFunction , expression : Expression ) {
208
194
const changes = textChanges . ChangeTracker . with ( context , t => wrapBlockWithParen ( t , context . sourceFile , declaration , expression ) ) ;
209
- return createCodeFixAction ( fixId , changes , Diagnostics . Wrap_this_object_literal_with_parentheses , fixIdWrapTheBlockWithParen , Diagnostics . Wrap_all_object_literal_with_parentheses ) ;
195
+ return createCodeFixAction ( fixId , changes , Diagnostics . Wrap_the_following_body_with_parentheses_which_should_be_an_object_literal , fixIdWrapTheBlockWithParen , Diagnostics . Wrap_all_object_literal_with_parentheses ) ;
210
196
}
211
197
}
0 commit comments