@@ -459,6 +459,7 @@ namespace ts.codefix {
459
459
const importAdder = createImportAdder ( context . sourceFile , context . program , context . preferences , context . host ) ;
460
460
const functionDeclaration = createSignatureDeclarationFromCallExpression ( SyntaxKind . FunctionDeclaration , context , importAdder , info . call , idText ( info . token ) , info . modifierFlags , info . parentDeclaration ) as FunctionDeclaration ;
461
461
changes . insertNodeAtEndOfScope ( info . sourceFile , info . parentDeclaration , functionDeclaration ) ;
462
+ importAdder . writeFixes ( changes ) ;
462
463
}
463
464
464
465
function addJsxAttributes ( changes : textChanges . ChangeTracker , context : CodeFixContextBase , info : JsxAttributesInfo ) {
@@ -468,7 +469,7 @@ namespace ts.codefix {
468
469
const jsxAttributesNode = info . parentDeclaration . attributes ;
469
470
const hasSpreadAttribute = some ( jsxAttributesNode . properties , isJsxSpreadAttribute ) ;
470
471
const attrs = map ( info . attributes , attr => {
471
- const value = tryGetValueFromType ( context , checker , importAdder , quotePreference , checker . getTypeOfSymbol ( attr ) ) ;
472
+ const value = tryGetValueFromType ( context , checker , importAdder , quotePreference , checker . getTypeOfSymbol ( attr ) , info . parentDeclaration ) ;
472
473
const name = factory . createIdentifier ( attr . name ) ;
473
474
const jsxAttribute = factory . createJsxAttribute ( name , factory . createJsxExpression ( /*dotDotDotToken*/ undefined , value ) ) ;
474
475
// formattingScanner requires the Identifier to have a context for scanning attributes with "-" (data-foo).
@@ -478,6 +479,7 @@ namespace ts.codefix {
478
479
const jsxAttributes = factory . createJsxAttributes ( hasSpreadAttribute ? [ ...attrs , ...jsxAttributesNode . properties ] : [ ...jsxAttributesNode . properties , ...attrs ] ) ;
479
480
const options = { prefix : jsxAttributesNode . pos === jsxAttributesNode . end ? " " : undefined } ;
480
481
changes . replaceNode ( context . sourceFile , jsxAttributesNode , jsxAttributes , options ) ;
482
+ importAdder . writeFixes ( changes ) ;
481
483
}
482
484
483
485
function addObjectLiteralProperties ( changes : textChanges . ChangeTracker , context : CodeFixContextBase , info : ObjectLiteralInfo ) {
@@ -486,7 +488,7 @@ namespace ts.codefix {
486
488
const target = getEmitScriptTarget ( context . program . getCompilerOptions ( ) ) ;
487
489
const checker = context . program . getTypeChecker ( ) ;
488
490
const props = map ( info . properties , prop => {
489
- const initializer = tryGetValueFromType ( context , checker , importAdder , quotePreference , checker . getTypeOfSymbol ( prop ) ) ;
491
+ const initializer = tryGetValueFromType ( context , checker , importAdder , quotePreference , checker . getTypeOfSymbol ( prop ) , info . parentDeclaration ) ;
490
492
return factory . createPropertyAssignment ( createPropertyNameNodeForIdentifierOrLiteral ( prop . name , target , quotePreference === QuotePreference . Single ) , initializer ) ;
491
493
} ) ;
492
494
const options = {
@@ -495,9 +497,10 @@ namespace ts.codefix {
495
497
indentation : info . indentation
496
498
} ;
497
499
changes . replaceNode ( context . sourceFile , info . parentDeclaration , factory . createObjectLiteralExpression ( [ ...info . parentDeclaration . properties , ...props ] , /*multiLine*/ true ) , options ) ;
500
+ importAdder . writeFixes ( changes ) ;
498
501
}
499
502
500
- function tryGetValueFromType ( context : CodeFixContextBase , checker : TypeChecker , importAdder : ImportAdder , quotePreference : QuotePreference , type : Type ) : Expression {
503
+ function tryGetValueFromType ( context : CodeFixContextBase , checker : TypeChecker , importAdder : ImportAdder , quotePreference : QuotePreference , type : Type , enclosingDeclaration : Node | undefined ) : Expression {
501
504
if ( type . flags & TypeFlags . AnyOrUnknown ) {
502
505
return createUndefined ( ) ;
503
506
}
@@ -534,15 +537,15 @@ namespace ts.codefix {
534
537
return factory . createNull ( ) ;
535
538
}
536
539
if ( type . flags & TypeFlags . Union ) {
537
- const expression = firstDefined ( ( type as UnionType ) . types , t => tryGetValueFromType ( context , checker , importAdder , quotePreference , t ) ) ;
540
+ const expression = firstDefined ( ( type as UnionType ) . types , t => tryGetValueFromType ( context , checker , importAdder , quotePreference , t , enclosingDeclaration ) ) ;
538
541
return expression ?? createUndefined ( ) ;
539
542
}
540
543
if ( checker . isArrayLikeType ( type ) ) {
541
544
return factory . createArrayLiteralExpression ( ) ;
542
545
}
543
546
if ( isObjectLiteralType ( type ) ) {
544
547
const props = map ( checker . getPropertiesOfType ( type ) , prop => {
545
- const initializer = prop . valueDeclaration ? tryGetValueFromType ( context , checker , importAdder , quotePreference , checker . getTypeAtLocation ( prop . valueDeclaration ) ) : createUndefined ( ) ;
548
+ const initializer = prop . valueDeclaration ? tryGetValueFromType ( context , checker , importAdder , quotePreference , checker . getTypeAtLocation ( prop . valueDeclaration ) , enclosingDeclaration ) : createUndefined ( ) ;
546
549
return factory . createPropertyAssignment ( prop . name , initializer ) ;
547
550
} ) ;
548
551
return factory . createObjectLiteralExpression ( props , /*multiLine*/ true ) ;
@@ -555,7 +558,7 @@ namespace ts.codefix {
555
558
if ( signature === undefined ) return createUndefined ( ) ;
556
559
557
560
const func = createSignatureDeclarationFromSignature ( SyntaxKind . FunctionExpression , context , quotePreference , signature [ 0 ] ,
558
- createStubbedBody ( Diagnostics . Function_not_implemented . message , quotePreference ) , /*name*/ undefined , /*modifiers*/ undefined , /*optional*/ undefined , /*enclosingDeclaration*/ undefined , importAdder ) as FunctionExpression | undefined ;
561
+ createStubbedBody ( Diagnostics . Function_not_implemented . message , quotePreference ) , /*name*/ undefined , /*modifiers*/ undefined , /*optional*/ undefined , /*enclosingDeclaration*/ enclosingDeclaration , importAdder ) as FunctionExpression | undefined ;
559
562
return func ?? createUndefined ( ) ;
560
563
}
561
564
if ( getObjectFlags ( type ) & ObjectFlags . Class ) {
0 commit comments