@@ -7,11 +7,11 @@ namespace ts.codefix {
7
7
* @param importAdder If provided, type annotations will use identifier type references instead of ImportTypeNodes, and the missing imports will be added to the importAdder.
8
8
* @returns Empty string iff there are no member insertions.
9
9
*/
10
- export function createMissingMemberNodes ( classDeclaration : ClassLikeDeclaration , possiblyMissingSymbols : readonly Symbol [ ] , context : TypeConstructionContext , preferences : UserPreferences , importAdder : ImportAdder | undefined , addClassElement : ( node : ClassElement ) => void ) : void {
10
+ export function createMissingMemberNodes ( classDeclaration : ClassLikeDeclaration , possiblyMissingSymbols : readonly Symbol [ ] , sourceFile : SourceFile , context : TypeConstructionContext , preferences : UserPreferences , importAdder : ImportAdder | undefined , addClassElement : ( node : ClassElement ) => void ) : void {
11
11
const classMembers = classDeclaration . symbol . members ! ;
12
12
for ( const symbol of possiblyMissingSymbols ) {
13
13
if ( ! classMembers . has ( symbol . escapedName ) ) {
14
- addNewNodeForMemberSymbol ( symbol , classDeclaration , context , preferences , importAdder , addClassElement ) ;
14
+ addNewNodeForMemberSymbol ( symbol , classDeclaration , sourceFile , context , preferences , importAdder , addClassElement ) ;
15
15
}
16
16
}
17
17
}
@@ -31,7 +31,7 @@ namespace ts.codefix {
31
31
/**
32
32
* @returns Empty string iff there we can't figure out a representation for `symbol` in `enclosingDeclaration`.
33
33
*/
34
- function addNewNodeForMemberSymbol ( symbol : Symbol , enclosingDeclaration : ClassLikeDeclaration , context : TypeConstructionContext , preferences : UserPreferences , importAdder : ImportAdder | undefined , addClassElement : ( node : Node ) => void ) : void {
34
+ function addNewNodeForMemberSymbol ( symbol : Symbol , enclosingDeclaration : ClassLikeDeclaration , sourceFile : SourceFile , context : TypeConstructionContext , preferences : UserPreferences , importAdder : ImportAdder | undefined , addClassElement : ( node : Node ) => void ) : void {
35
35
const declarations = symbol . getDeclarations ( ) ;
36
36
if ( ! ( declarations && declarations . length ) ) {
37
37
return undefined ;
@@ -45,11 +45,12 @@ namespace ts.codefix {
45
45
const type = checker . getWidenedType ( checker . getTypeOfSymbolAtLocation ( symbol , enclosingDeclaration ) ) ;
46
46
const optional = ! ! ( symbol . flags & SymbolFlags . Optional ) ;
47
47
const ambient = ! ! ( enclosingDeclaration . flags & NodeFlags . Ambient ) ;
48
+ const quotePreference = getQuotePreference ( sourceFile , preferences ) ;
48
49
49
50
switch ( declaration . kind ) {
50
51
case SyntaxKind . PropertySignature :
51
52
case SyntaxKind . PropertyDeclaration :
52
- const flags = preferences . quotePreference === "single" ? NodeBuilderFlags . UseSingleQuotesForStringLiteralType : undefined ;
53
+ const flags = quotePreference === QuotePreference . Single ? NodeBuilderFlags . UseSingleQuotesForStringLiteralType : undefined ;
53
54
let typeNode = checker . typeToTypeNode ( type , enclosingDeclaration , flags , getNoopSymbolTrackerWithResolver ( context ) ) ;
54
55
if ( importAdder ) {
55
56
const importableReference = tryGetAutoImportableReferenceFromImportTypeNode ( typeNode , type , scriptTarget ) ;
@@ -88,7 +89,7 @@ namespace ts.codefix {
88
89
name ,
89
90
emptyArray ,
90
91
typeNode ,
91
- ambient ? undefined : createStubbedMethodBody ( preferences ) ) ) ;
92
+ ambient ? undefined : createStubbedMethodBody ( quotePreference ) ) ) ;
92
93
}
93
94
else {
94
95
Debug . assertNode ( accessor , isSetAccessorDeclaration , "The counterpart to a getter should be a setter" ) ;
@@ -99,7 +100,7 @@ namespace ts.codefix {
99
100
modifiers ,
100
101
name ,
101
102
createDummyParameters ( 1 , [ parameterName ] , [ typeNode ] , 1 , /*inJs*/ false ) ,
102
- ambient ? undefined : createStubbedMethodBody ( preferences ) ) ) ;
103
+ ambient ? undefined : createStubbedMethodBody ( quotePreference ) ) ) ;
103
104
}
104
105
}
105
106
break ;
@@ -121,36 +122,37 @@ namespace ts.codefix {
121
122
if ( declarations . length === 1 ) {
122
123
Debug . assert ( signatures . length === 1 , "One declaration implies one signature" ) ;
123
124
const signature = signatures [ 0 ] ;
124
- outputMethod ( signature , modifiers , name , ambient ? undefined : createStubbedMethodBody ( preferences ) ) ;
125
+ outputMethod ( quotePreference , signature , modifiers , name , ambient ? undefined : createStubbedMethodBody ( quotePreference ) ) ;
125
126
break ;
126
127
}
127
128
128
129
for ( const signature of signatures ) {
129
130
// Need to ensure nodes are fresh each time so they can have different positions.
130
- outputMethod ( signature , getSynthesizedDeepClones ( modifiers , /*includeTrivia*/ false ) , getSynthesizedDeepClone ( name , /*includeTrivia*/ false ) ) ;
131
+ outputMethod ( quotePreference , signature , getSynthesizedDeepClones ( modifiers , /*includeTrivia*/ false ) , getSynthesizedDeepClone ( name , /*includeTrivia*/ false ) ) ;
131
132
}
132
133
133
134
if ( ! ambient ) {
134
135
if ( declarations . length > signatures . length ) {
135
136
const signature = checker . getSignatureFromDeclaration ( declarations [ declarations . length - 1 ] as SignatureDeclaration ) ! ;
136
- outputMethod ( signature , modifiers , name , createStubbedMethodBody ( preferences ) ) ;
137
+ outputMethod ( quotePreference , signature , modifiers , name , createStubbedMethodBody ( quotePreference ) ) ;
137
138
}
138
139
else {
139
140
Debug . assert ( declarations . length === signatures . length , "Declarations and signatures should match count" ) ;
140
- addClassElement ( createMethodImplementingSignatures ( signatures , name , optional , modifiers , preferences ) ) ;
141
+ addClassElement ( createMethodImplementingSignatures ( signatures , name , optional , modifiers , quotePreference ) ) ;
141
142
}
142
143
}
143
144
break ;
144
145
}
145
146
146
- function outputMethod ( signature : Signature , modifiers : NodeArray < Modifier > | undefined , name : PropertyName , body ?: Block ) : void {
147
- const method = signatureToMethodDeclaration ( context , signature , enclosingDeclaration , modifiers , name , optional , body , importAdder ) ;
147
+ function outputMethod ( quotePreference : QuotePreference , signature : Signature , modifiers : NodeArray < Modifier > | undefined , name : PropertyName , body ?: Block ) : void {
148
+ const method = signatureToMethodDeclaration ( context , quotePreference , signature , enclosingDeclaration , modifiers , name , optional , body , importAdder ) ;
148
149
if ( method ) addClassElement ( method ) ;
149
150
}
150
151
}
151
152
152
153
function signatureToMethodDeclaration (
153
154
context : TypeConstructionContext ,
155
+ quotePreference : QuotePreference ,
154
156
signature : Signature ,
155
157
enclosingDeclaration : ClassLikeDeclaration ,
156
158
modifiers : NodeArray < Modifier > | undefined ,
@@ -162,7 +164,8 @@ namespace ts.codefix {
162
164
const program = context . program ;
163
165
const checker = program . getTypeChecker ( ) ;
164
166
const scriptTarget = getEmitScriptTarget ( program . getCompilerOptions ( ) ) ;
165
- const signatureDeclaration = < MethodDeclaration > checker . signatureToSignatureDeclaration ( signature , SyntaxKind . MethodDeclaration , enclosingDeclaration , NodeBuilderFlags . NoTruncation | NodeBuilderFlags . SuppressAnyReturnType , getNoopSymbolTrackerWithResolver ( context ) ) ;
167
+ const flags = NodeBuilderFlags . NoTruncation | NodeBuilderFlags . SuppressAnyReturnType | ( quotePreference === QuotePreference . Single ? NodeBuilderFlags . UseSingleQuotesForStringLiteralType : 0 ) ;
168
+ const signatureDeclaration = < MethodDeclaration > checker . signatureToSignatureDeclaration ( signature , SyntaxKind . MethodDeclaration , enclosingDeclaration , flags , getNoopSymbolTrackerWithResolver ( context ) ) ;
166
169
if ( ! signatureDeclaration ) {
167
170
return undefined ;
168
171
}
@@ -266,6 +269,7 @@ namespace ts.codefix {
266
269
isIdentifier ( arg ) ? arg . text : isPropertyAccessExpression ( arg ) && isIdentifier ( arg . name ) ? arg . name . text : undefined ) ;
267
270
const contextualType = checker . getContextualType ( call ) ;
268
271
const returnType = ( inJs || ! contextualType ) ? undefined : checker . typeToTypeNode ( contextualType , contextNode , /*flags*/ undefined , tracker ) ;
272
+ const quotePreference = getQuotePreference ( context . sourceFile , context . preferences ) ;
269
273
return factory . createMethodDeclaration (
270
274
/*decorators*/ undefined ,
271
275
/*modifiers*/ modifierFlags ? factory . createNodeArray ( factory . createModifiersFromModifierFlags ( modifierFlags ) ) : undefined ,
@@ -276,7 +280,7 @@ namespace ts.codefix {
276
280
factory . createTypeParameterDeclaration ( CharacterCodes . T + typeArguments ! . length - 1 <= CharacterCodes . Z ? String . fromCharCode ( CharacterCodes . T + i ) : `T${ i } ` ) ) ,
277
281
/*parameters*/ createDummyParameters ( args . length , names , types , /*minArgumentCount*/ undefined , inJs ) ,
278
282
/*type*/ returnType ,
279
- body ? createStubbedMethodBody ( context . preferences ) : undefined ) ;
283
+ body ? createStubbedMethodBody ( quotePreference ) : undefined ) ;
280
284
}
281
285
282
286
export function typeToAutoImportableTypeNode ( checker : TypeChecker , importAdder : ImportAdder , type : Type , contextNode : Node , scriptTarget : ScriptTarget , flags ?: NodeBuilderFlags , tracker ?: SymbolTracker ) : TypeNode | undefined {
@@ -312,7 +316,7 @@ namespace ts.codefix {
312
316
name : PropertyName ,
313
317
optional : boolean ,
314
318
modifiers : readonly Modifier [ ] | undefined ,
315
- preferences : UserPreferences ,
319
+ quotePreference : QuotePreference ,
316
320
) : MethodDeclaration {
317
321
/** This is *a* signature with the maximal number of arguments,
318
322
* such that if there is a "maximal" signature without rest arguments,
@@ -355,7 +359,7 @@ namespace ts.codefix {
355
359
/*typeParameters*/ undefined ,
356
360
parameters ,
357
361
/*returnType*/ undefined ,
358
- preferences ) ;
362
+ quotePreference ) ;
359
363
}
360
364
361
365
function createStubbedMethod (
@@ -365,7 +369,7 @@ namespace ts.codefix {
365
369
typeParameters : readonly TypeParameterDeclaration [ ] | undefined ,
366
370
parameters : readonly ParameterDeclaration [ ] ,
367
371
returnType : TypeNode | undefined ,
368
- preferences : UserPreferences
372
+ quotePreference : QuotePreference
369
373
) : MethodDeclaration {
370
374
return factory . createMethodDeclaration (
371
375
/*decorators*/ undefined ,
@@ -376,17 +380,17 @@ namespace ts.codefix {
376
380
typeParameters ,
377
381
parameters ,
378
382
returnType ,
379
- createStubbedMethodBody ( preferences ) ) ;
383
+ createStubbedMethodBody ( quotePreference ) ) ;
380
384
}
381
385
382
- function createStubbedMethodBody ( preferences : UserPreferences ) : Block {
386
+ function createStubbedMethodBody ( quotePreference : QuotePreference ) : Block {
383
387
return factory . createBlock (
384
388
[ factory . createThrowStatement (
385
389
factory . createNewExpression (
386
390
factory . createIdentifier ( "Error" ) ,
387
391
/*typeArguments*/ undefined ,
388
392
// TODO Handle auto quote preference.
389
- [ factory . createStringLiteral ( "Method not implemented." , /*isSingleQuote*/ preferences . quotePreference === "single" ) ] ) ) ] ,
393
+ [ factory . createStringLiteral ( "Method not implemented." , /*isSingleQuote*/ quotePreference === QuotePreference . Single ) ] ) ) ] ,
390
394
/*multiline*/ true ) ;
391
395
}
392
396
0 commit comments