@@ -82,7 +82,7 @@ namespace ts.codefix {
82
82
}
83
83
84
84
for ( const statement of returnStatements ) {
85
- forEachChild ( statement , function visit ( node : Node ) {
85
+ forEachChild ( statement , function visit ( node ) {
86
86
if ( isCallExpression ( node ) ) {
87
87
startTransformation ( node , statement ) ;
88
88
}
@@ -179,7 +179,7 @@ namespace ts.codefix {
179
179
// Note - the choice of the last call signature is arbitrary
180
180
if ( lastCallSignature && lastCallSignature . parameters . length && ! synthNamesMap . has ( symbolIdString ) ) {
181
181
const name = lastCallSignature . parameters [ 0 ] . name ;
182
- const synthName = getNewNameIfConflict ( createIdentifier ( lastCallSignature . parameters [ 0 ] . name ) , allVarNames ) ;
182
+ const synthName = getNewNameIfConflict ( createIdentifier ( name ) , allVarNames ) ;
183
183
synthNamesMap . set ( symbolIdString , synthName ) ;
184
184
allVarNames . push ( { identifier : synthName . identifier , symbol, originalName : name } ) ;
185
185
}
@@ -404,8 +404,13 @@ namespace ts.codefix {
404
404
// Arrow functions with block bodies { } will enter this control flow
405
405
if ( isFunctionLikeDeclaration ( func ) && func . body && isBlock ( func . body ) && func . body . statements ) {
406
406
let refactoredStmts : Statement [ ] = [ ] ;
407
+ let seenReturnStatement = false ;
407
408
408
409
for ( const statement of func . body . statements ) {
410
+ if ( isReturnStatement ( statement ) ) {
411
+ seenReturnStatement = true ;
412
+ }
413
+
409
414
if ( getReturnStatementsWithPromiseHandlers ( statement ) . length ) {
410
415
refactoredStmts = refactoredStmts . concat ( getInnerTransformationBody ( transformer , [ statement ] , prevArgName ) ) ;
411
416
}
@@ -415,7 +420,7 @@ namespace ts.codefix {
415
420
}
416
421
417
422
return shouldReturn ? getSynthesizedDeepClones ( createNodeArray ( refactoredStmts ) ) :
418
- removeReturns ( createNodeArray ( refactoredStmts ) , prevArgName ! . identifier , transformer . constIdentifiers ) ;
423
+ removeReturns ( createNodeArray ( refactoredStmts ) , prevArgName ! . identifier , transformer . constIdentifiers , seenReturnStatement ) ;
419
424
}
420
425
else {
421
426
const funcBody = ( < ArrowFunction > func ) . body ;
@@ -443,12 +448,12 @@ namespace ts.codefix {
443
448
}
444
449
445
450
function getLastCallSignature ( type : Type , checker : TypeChecker ) : Signature | undefined {
446
- const callSignatures = type && checker . getSignaturesOfType ( type , SignatureKind . Call ) ;
451
+ const callSignatures = checker . getSignaturesOfType ( type , SignatureKind . Call ) ;
447
452
return callSignatures && callSignatures [ callSignatures . length - 1 ] ;
448
453
}
449
454
450
455
451
- function removeReturns ( stmts : NodeArray < Statement > , prevArgName : Identifier , constIdentifiers : Identifier [ ] ) : NodeArray < Statement > {
456
+ function removeReturns ( stmts : NodeArray < Statement > , prevArgName : Identifier , constIdentifiers : Identifier [ ] , seenReturnStatement : boolean ) : NodeArray < Statement > {
452
457
const ret : Statement [ ] = [ ] ;
453
458
for ( const stmt of stmts ) {
454
459
if ( isReturnStatement ( stmt ) ) {
@@ -462,6 +467,12 @@ namespace ts.codefix {
462
467
}
463
468
}
464
469
470
+ // if block has no return statement, need to define prevArgName as undefined to prevent undeclared variables
471
+ if ( ! seenReturnStatement ) {
472
+ ret . push ( createVariableStatement ( /*modifiers*/ undefined ,
473
+ ( createVariableDeclarationList ( [ createVariableDeclaration ( prevArgName , /*type*/ undefined , createIdentifier ( "undefined" ) ) ] , getFlagOfIdentifier ( prevArgName , constIdentifiers ) ) ) ) ) ;
474
+ }
475
+
465
476
return createNodeArray ( ret ) ;
466
477
}
467
478
0 commit comments