@@ -83,7 +83,7 @@ namespace ts.Completions {
83
83
}
84
84
case StringLiteralCompletionKind . Types : {
85
85
const entries = completion . types . map ( type => ( { name : type . value , kindModifiers : ScriptElementKindModifier . none , kind : ScriptElementKind . typeElement , sortText : "0" } ) ) ;
86
- return { isGlobalCompletion : false , isMemberCompletion : false , isNewIdentifierLocation : false , entries } ;
86
+ return { isGlobalCompletion : false , isMemberCompletion : false , isNewIdentifierLocation : completion . isNewIdentifier , entries } ;
87
87
}
88
88
default :
89
89
return Debug . assertNever ( completion ) ;
@@ -358,16 +358,18 @@ namespace ts.Completions {
358
358
readonly symbols : ReadonlyArray < Symbol > ;
359
359
readonly hasIndexSignature : boolean ;
360
360
}
361
- type StringLiteralCompletion =
362
- | { readonly kind : StringLiteralCompletionKind . Paths , readonly paths : ReadonlyArray < PathCompletions . PathCompletion > }
363
- | StringLiteralCompletionsFromProperties
364
- | { readonly kind : StringLiteralCompletionKind . Types , readonly types : ReadonlyArray < StringLiteralType > } ;
361
+ interface StringLiteralCompletionsFromTypes {
362
+ readonly kind : StringLiteralCompletionKind . Types ;
363
+ readonly types : ReadonlyArray < StringLiteralType > ;
364
+ readonly isNewIdentifier : boolean ;
365
+ }
366
+ type StringLiteralCompletion = { readonly kind : StringLiteralCompletionKind . Paths , readonly paths : ReadonlyArray < PathCompletions . PathCompletion > } | StringLiteralCompletionsFromProperties | StringLiteralCompletionsFromTypes ;
365
367
function getStringLiteralCompletionEntries ( sourceFile : SourceFile , node : StringLiteralLike , position : number , typeChecker : TypeChecker , compilerOptions : CompilerOptions , host : LanguageServiceHost ) : StringLiteralCompletion | undefined {
366
368
switch ( node . parent . kind ) {
367
369
case SyntaxKind . LiteralType :
368
370
switch ( node . parent . parent . kind ) {
369
371
case SyntaxKind . TypeReference :
370
- return { kind : StringLiteralCompletionKind . Types , types : getStringLiteralTypes ( typeChecker . getTypeArgumentConstraint ( node . parent as LiteralTypeNode ) , typeChecker ) } ;
372
+ return { kind : StringLiteralCompletionKind . Types , types : getStringLiteralTypes ( typeChecker . getTypeArgumentConstraint ( node . parent as LiteralTypeNode ) , typeChecker ) , isNewIdentifier : false } ;
371
373
case SyntaxKind . IndexedAccessType :
372
374
// Get all apparent property names
373
375
// i.e. interface Foo {
@@ -419,13 +421,7 @@ namespace ts.Completions {
419
421
// Get string literal completions from specialized signatures of the target
420
422
// i.e. declare function f(a: 'A');
421
423
// f("/*completion position*/")
422
- if ( argumentInfo ) {
423
- const candidates : Signature [ ] = [ ] ;
424
- typeChecker . getResolvedSignature ( argumentInfo . invocation , candidates , argumentInfo . argumentCount ) ;
425
- const uniques = createMap < true > ( ) ;
426
- return { kind : StringLiteralCompletionKind . Types , types : flatMap ( candidates , candidate => getStringLiteralTypes ( typeChecker . getParameterType ( candidate , argumentInfo . argumentIndex ) , typeChecker , uniques ) ) } ;
427
- }
428
- return fromContextualType ( ) ;
424
+ return argumentInfo ? getStringLiteralCompletionsFromSignature ( argumentInfo , typeChecker ) : fromContextualType ( ) ;
429
425
}
430
426
// falls through (is `require("")` or `import("")`)
431
427
@@ -447,10 +443,26 @@ namespace ts.Completions {
447
443
function fromContextualType ( ) : StringLiteralCompletion {
448
444
// Get completion for string literal from string literal type
449
445
// i.e. var x: "hi" | "hello" = "/*completion position*/"
450
- return { kind : StringLiteralCompletionKind . Types , types : getStringLiteralTypes ( getContextualTypeFromParent ( node , typeChecker ) , typeChecker ) } ;
446
+ return { kind : StringLiteralCompletionKind . Types , types : getStringLiteralTypes ( getContextualTypeFromParent ( node , typeChecker ) , typeChecker ) , isNewIdentifier : false } ;
451
447
}
452
448
}
453
449
450
+ function getStringLiteralCompletionsFromSignature ( argumentInfo : SignatureHelp . ArgumentListInfo , checker : TypeChecker ) : StringLiteralCompletionsFromTypes {
451
+ let isNewIdentifier = false ;
452
+
453
+ const uniques = createMap < true > ( ) ;
454
+ const candidates : Signature [ ] = [ ] ;
455
+ checker . getResolvedSignature ( argumentInfo . invocation , candidates , argumentInfo . argumentCount ) ;
456
+ const types = flatMap ( candidates , candidate => {
457
+ if ( ! candidate . hasRestParameter && argumentInfo . argumentCount > candidate . parameters . length ) return ;
458
+ const type = checker . getParameterType ( candidate , argumentInfo . argumentIndex ) ;
459
+ isNewIdentifier = isNewIdentifier || ! ! ( type . flags & TypeFlags . String ) ;
460
+ return getStringLiteralTypes ( type , checker , uniques ) ;
461
+ } ) ;
462
+
463
+ return { kind : StringLiteralCompletionKind . Types , types, isNewIdentifier } ;
464
+ }
465
+
454
466
function stringLiteralCompletionsFromProperties ( type : Type | undefined ) : StringLiteralCompletionsFromProperties | undefined {
455
467
return type && { kind : StringLiteralCompletionKind . Properties , symbols : type . getApparentProperties ( ) , hasIndexSignature : hasIndexSignature ( type ) } ;
456
468
}
0 commit comments