2
2
namespace ts . codefix {
3
3
const fixId = "convertToAsyncFunction" ;
4
4
const errorCodes = [ Diagnostics . This_may_be_converted_to_an_async_function . code ] ;
5
+ let codeActionSucceeded = true ;
5
6
registerCodeFix ( {
6
7
errorCodes,
7
8
getCodeActions ( context : CodeFixContext ) {
9
+ codeActionSucceeded = true ;
8
10
const changes = textChanges . ChangeTracker . with ( context , ( t ) => convertToAsyncFunction ( t , context . sourceFile , context . span . start , context . program . getTypeChecker ( ) , context ) ) ;
9
- return [ createCodeFixAction ( fixId , changes , Diagnostics . Convert_to_async_function , fixId , Diagnostics . Convert_all_to_async_functions ) ] ;
11
+ return codeActionSucceeded ? [ createCodeFixAction ( fixId , changes , Diagnostics . Convert_to_async_function , fixId , Diagnostics . Convert_all_to_async_functions ) ] : [ ] ;
10
12
} ,
11
13
fixIds : [ fixId ] ,
12
14
getAllCodeActions : context => codeFixAll ( context , errorCodes , ( changes , err ) => convertToAsyncFunction ( changes , err . file , err . start , context . program . getTypeChecker ( ) , context ) ) ,
@@ -252,6 +254,7 @@ namespace ts.codefix {
252
254
}
253
255
254
256
// dispatch function to recursively build the refactoring
257
+ // should be kept up to date with isFixablePromiseHandler in suggestionDiagnostics.ts
255
258
function transformExpression ( node : Expression , transformer : Transformer , outermostParent : CallExpression , prevArgName ?: SynthIdentifier ) : Statement [ ] {
256
259
if ( ! node ) {
257
260
return [ ] ;
@@ -273,6 +276,7 @@ namespace ts.codefix {
273
276
return transformPromiseCall ( node , transformer , prevArgName ) ;
274
277
}
275
278
279
+ codeActionSucceeded = false ;
276
280
return [ ] ;
277
281
}
278
282
@@ -381,13 +385,18 @@ namespace ts.codefix {
381
385
( createVariableDeclarationList ( [ createVariableDeclaration ( getSynthesizedDeepClone ( prevArgName . identifier ) , /*type*/ undefined , rightHandSide ) ] , getFlagOfIdentifier ( prevArgName . identifier , transformer . constIdentifiers ) ) ) ) ] ) ;
382
386
}
383
387
388
+ // should be kept up to date with isFixablePromiseArgument in suggestionDiagnostics.ts
384
389
function getTransformationBody ( func : Node , prevArgName : SynthIdentifier | undefined , argName : SynthIdentifier , parent : CallExpression , transformer : Transformer ) : NodeArray < Statement > {
385
390
386
391
const hasPrevArgName = prevArgName && prevArgName . identifier . text . length > 0 ;
387
392
const hasArgName = argName && argName . identifier . text . length > 0 ;
388
393
const shouldReturn = transformer . setOfExpressionsToReturn . get ( getNodeId ( parent ) . toString ( ) ) ;
389
394
switch ( func . kind ) {
395
+ case SyntaxKind . NullKeyword :
396
+ // do not produce a transformed statement for a null argument
397
+ break ;
390
398
case SyntaxKind . Identifier :
399
+ // identifier includes undefined
391
400
if ( ! hasArgName ) break ;
392
401
393
402
const synthCall = createCall ( getSynthesizedDeepClone ( func ) as Identifier , /*typeArguments*/ undefined , [ argName . identifier ] ) ;
@@ -443,6 +452,9 @@ namespace ts.codefix {
443
452
return createNodeArray ( [ createReturn ( getSynthesizedDeepClone ( funcBody ) as Expression ) ] ) ;
444
453
}
445
454
}
455
+ default :
456
+ // We've found a transformation body we don't know how to handle, so the refactoring should no-op to avoid deleting code.
457
+ codeActionSucceeded = false ;
446
458
break ;
447
459
}
448
460
return createNodeArray ( [ ] ) ;
@@ -492,14 +504,6 @@ namespace ts.codefix {
492
504
return innerCbBody ;
493
505
}
494
506
495
- function hasPropertyAccessExpressionWithName ( node : CallExpression , funcName : string ) : boolean {
496
- if ( ! isPropertyAccessExpression ( node . expression ) ) {
497
- return false ;
498
- }
499
-
500
- return node . expression . name . text === funcName ;
501
- }
502
-
503
507
function getArgName ( funcNode : Node , transformer : Transformer ) : SynthIdentifier {
504
508
505
509
const numberOfAssignmentsOriginal = 0 ;
0 commit comments