@@ -10,23 +10,25 @@ namespace ts.codefix {
1010 Diagnostics . Argument_of_type_0_is_not_assignable_to_parameter_of_type_1 . code
1111 ] ;
1212
13- enum FixKind {
13+ enum ProblemKind {
1414 MissingReturnStatement ,
1515 MissingParentheses
1616 }
1717
1818 interface MissingReturnInfo {
19- kind : FixKind . MissingReturnStatement ;
19+ kind : ProblemKind . MissingReturnStatement ;
2020 declaration : FunctionLikeDeclaration ;
2121 expression : Expression ;
2222 statement : Statement ;
23+ commentSource : Node ;
2324 }
2425
2526 interface MissingParenInfo {
26- kind : FixKind . MissingParentheses ;
27+ kind : ProblemKind . MissingParentheses ;
2728 declaration : ArrowFunction ;
2829 expression : Expression ;
2930 statement : Statement ;
31+ commentSource : Node ;
3032 }
3133
3234 type Info = MissingReturnInfo | MissingParenInfo ;
@@ -39,10 +41,10 @@ namespace ts.codefix {
3941 const info = getInfo ( program . getTypeChecker ( ) , sourceFile , start , errorCode ) ;
4042 if ( ! info ) return undefined ;
4143
42- if ( info . kind === FixKind . MissingReturnStatement ) {
44+ if ( info . kind === ProblemKind . MissingReturnStatement ) {
4345 return append (
4446 [ getActionForfixAddReturnStatement ( context , info . expression , info . statement ) ] ,
45- isArrowFunction ( info . declaration ) ? getActionForfixRemoveBlockBodyBrace ( context , info . declaration , info . expression ) : undefined ) ;
47+ isArrowFunction ( info . declaration ) ? getActionForfixRemoveBlockBodyBrace ( context , info . declaration , info . expression , info . commentSource ) : undefined ) ;
4648 }
4749 else {
4850 return [ getActionForfixWrapTheBlockWithParen ( context , info . declaration , info . expression ) ] ;
@@ -58,7 +60,7 @@ namespace ts.codefix {
5860 break ;
5961 case fixIdRemoveBlockBodyBrace :
6062 if ( ! isArrowFunction ( info . declaration ) ) return undefined ;
61- removeBlockBodyBrace ( changes , diag . file , info . declaration , info . expression , /* withParen */ false ) ;
63+ removeBlockBodyBrace ( changes , diag . file , info . declaration , info . expression , info . commentSource , /* withParen */ false ) ;
6264 break ;
6365 case fixIdWrapTheBlockWithParen :
6466 if ( ! isArrowFunction ( info . declaration ) ) return undefined ;
@@ -70,50 +72,34 @@ namespace ts.codefix {
7072 } ) ,
7173 } ) ;
7274
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-
9275 function getFixInfo ( checker : TypeChecker , declaration : FunctionLikeDeclaration , expectType : Type , isFunctionType : boolean ) : Info | undefined {
9376 if ( ! declaration . body || ! isBlock ( declaration . body ) || length ( declaration . body . statements ) !== 1 ) return undefined ;
9477
9578 const firstStatement = first ( declaration . body . statements ) ;
9679 if ( isExpressionStatement ( firstStatement ) && checkFixedAssignableTo ( checker , declaration , firstStatement . expression , expectType , isFunctionType ) ) {
9780 return {
9881 declaration,
99- kind : FixKind . MissingReturnStatement ,
82+ kind : ProblemKind . MissingReturnStatement ,
10083 expression : firstStatement . expression ,
101- statement : firstStatement
84+ statement : firstStatement ,
85+ commentSource : firstStatement . expression
10286 } ;
10387 }
10488 else if ( isLabeledStatement ( firstStatement ) && isExpressionStatement ( firstStatement . statement ) ) {
10589 const node = createObjectLiteral ( [ createPropertyAssignment ( firstStatement . label , firstStatement . statement . expression ) ] ) ;
10690 if ( checkFixedAssignableTo ( checker , declaration , node , expectType , isFunctionType ) ) {
10791 return isArrowFunction ( declaration ) ? {
10892 declaration,
109- kind : FixKind . MissingParentheses ,
93+ kind : ProblemKind . MissingParentheses ,
11094 expression : node ,
111- statement : firstStatement
95+ statement : firstStatement ,
96+ commentSource : firstStatement . statement . expression
11297 } : {
11398 declaration,
114- kind : FixKind . MissingReturnStatement ,
99+ kind : ProblemKind . MissingReturnStatement ,
115100 expression : node ,
116- statement : firstStatement
101+ statement : firstStatement ,
102+ commentSource : firstStatement . statement . expression
117103 } ;
118104 }
119105 }
@@ -124,9 +110,10 @@ namespace ts.codefix {
124110 if ( checkFixedAssignableTo ( checker , declaration , node , expectType , isFunctionType ) ) {
125111 return {
126112 declaration,
127- kind : FixKind . MissingReturnStatement ,
113+ kind : ProblemKind . MissingReturnStatement ,
128114 expression : node ,
129- statement : firstStatement
115+ statement : firstStatement ,
116+ commentSource : firstBlockStatement
130117 } ;
131118 }
132119 }
@@ -183,11 +170,16 @@ namespace ts.codefix {
183170 }
184171
185172 function addReturnStatement ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , expression : Expression , statement : Statement ) {
173+ suppressLeadingAndTrailingTrivia ( expression ) ;
186174 changes . replaceNode ( sourceFile , statement , createReturn ( expression ) ) ;
187175 }
188176
189- 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 ) ;
177+ function removeBlockBodyBrace ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , declaration : ArrowFunction , expression : Expression , commentSource : Node , withParen : boolean ) {
178+ const newBody = ( withParen || needsParentheses ( expression ) ) ? createParen ( expression ) : expression ;
179+ suppressLeadingAndTrailingTrivia ( commentSource ) ;
180+ copyComments ( commentSource , newBody ) ;
181+
182+ changes . replaceNode ( sourceFile , declaration . body , newBody ) ;
191183 }
192184
193185 function wrapBlockWithParen ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , declaration : ArrowFunction , expression : Expression ) {
@@ -199,13 +191,13 @@ namespace ts.codefix {
199191 return createCodeFixAction ( fixId , changes , Diagnostics . Add_a_return_statement , fixIdAddReturnStatement , Diagnostics . Add_all_missing_return_statement ) ;
200192 }
201193
202- function getActionForfixRemoveBlockBodyBrace ( context : CodeFixContext , declaration : ArrowFunction , expression : Expression ) {
203- const changes = textChanges . ChangeTracker . with ( context , t => removeBlockBodyBrace ( t , context . sourceFile , declaration , expression , /* withParen */ false ) ) ;
194+ function getActionForfixRemoveBlockBodyBrace ( context : CodeFixContext , declaration : ArrowFunction , expression : Expression , commentSource : Node ) {
195+ const changes = textChanges . ChangeTracker . with ( context , t => removeBlockBodyBrace ( t , context . sourceFile , declaration , expression , commentSource , /* withParen */ false ) ) ;
204196 return createCodeFixAction ( fixId , changes , Diagnostics . Remove_block_body_braces , fixIdRemoveBlockBodyBrace , Diagnostics . Remove_all_incorrect_body_block_braces ) ;
205197 }
206198
207199 function getActionForfixWrapTheBlockWithParen ( context : CodeFixContext , declaration : ArrowFunction , expression : Expression ) {
208200 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 ) ;
201+ 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 ) ;
210202 }
211203}
0 commit comments