@@ -657,32 +657,46 @@ namespace ts.Completions {
657
657
None ,
658
658
}
659
659
660
- function getRecommendedCompletion ( currentToken : Node , checker : TypeChecker ) : Symbol | undefined {
661
- const ty = getContextualType ( currentToken , checker ) ;
660
+ function getRecommendedCompletion ( currentToken : Node , position : number , sourceFile : SourceFile , checker : TypeChecker ) : Symbol | undefined {
661
+ const ty = getContextualType ( currentToken , position , sourceFile , checker ) ;
662
662
const symbol = ty && ty . symbol ;
663
663
// Don't include make a recommended completion for an abstract class
664
664
return symbol && ( symbol . flags & SymbolFlags . Enum || symbol . flags & SymbolFlags . Class && ! isAbstractConstructorSymbol ( symbol ) )
665
665
? getFirstSymbolInChain ( symbol , currentToken , checker )
666
666
: undefined ;
667
667
}
668
668
669
- function getContextualType ( currentToken : Node , checker : ts . TypeChecker ) : Type | undefined {
669
+ function getContextualType ( currentToken : Node , position : number , sourceFile : SourceFile , checker : TypeChecker ) : Type | undefined {
670
670
const { parent } = currentToken ;
671
671
switch ( currentToken . kind ) {
672
- case ts . SyntaxKind . Identifier :
673
- return getContextualTypeFromParent ( currentToken as ts . Identifier , checker ) ;
674
- case ts . SyntaxKind . EqualsToken :
675
- return ts . isVariableDeclaration ( parent ) ? checker . getContextualType ( parent . initializer ) :
676
- ts . isBinaryExpression ( parent ) ? checker . getTypeAtLocation ( parent . left ) : undefined ;
677
- case ts . SyntaxKind . NewKeyword :
678
- return checker . getContextualType ( parent as ts . Expression ) ;
679
- case ts . SyntaxKind . CaseKeyword :
680
- return getSwitchedType ( cast ( currentToken . parent , isCaseClause ) , checker ) ;
672
+ case SyntaxKind . Identifier :
673
+ return getContextualTypeFromParent ( currentToken as Identifier , checker ) ;
674
+ case SyntaxKind . EqualsToken :
675
+ switch ( parent . kind ) {
676
+ case ts . SyntaxKind . VariableDeclaration :
677
+ return checker . getContextualType ( ( parent as VariableDeclaration ) . initializer ) ;
678
+ case ts . SyntaxKind . BinaryExpression :
679
+ return checker . getTypeAtLocation ( ( parent as BinaryExpression ) . left ) ;
680
+ case ts . SyntaxKind . JsxAttribute :
681
+ return checker . getContextualTypeForJsxAttribute ( parent as JsxAttribute ) ;
682
+ default :
683
+ return undefined ;
684
+ }
685
+ case SyntaxKind . NewKeyword :
686
+ return checker . getContextualType ( parent as Expression ) ;
687
+ case SyntaxKind . CaseKeyword :
688
+ return getSwitchedType ( cast ( parent , isCaseClause ) , checker ) ;
689
+ case SyntaxKind . OpenBraceToken :
690
+ return isJsxExpression ( parent ) && parent . parent . kind !== SyntaxKind . JsxElement ? checker . getContextualTypeForJsxAttribute ( parent . parent ) : undefined ;
681
691
default :
682
- return isEqualityOperatorKind ( currentToken . kind ) && ts . isBinaryExpression ( parent ) && isEqualityOperatorKind ( parent . operatorToken . kind )
692
+ const argInfo = SignatureHelp . getImmediatelyContainingArgumentInfo ( currentToken , position , sourceFile ) ;
693
+ return argInfo
694
+ // At `,`, treat this as the next argument after the comma.
695
+ ? checker . getContextualTypeForArgumentAtIndex ( argInfo . invocation , argInfo . argumentIndex + ( currentToken . kind === SyntaxKind . CommaToken ? 1 : 0 ) )
696
+ : isEqualityOperatorKind ( currentToken . kind ) && isBinaryExpression ( parent ) && isEqualityOperatorKind ( parent . operatorToken . kind )
683
697
// completion at `x ===/**/` should be for the right side
684
698
? checker . getTypeAtLocation ( parent . left )
685
- : checker . getContextualType ( currentToken as ts . Expression ) ;
699
+ : checker . getContextualType ( currentToken as Expression ) ;
686
700
}
687
701
}
688
702
@@ -956,7 +970,7 @@ namespace ts.Completions {
956
970
957
971
log ( "getCompletionData: Semantic work: " + ( timestamp ( ) - semanticStart ) ) ;
958
972
959
- const recommendedCompletion = previousToken && getRecommendedCompletion ( previousToken , typeChecker ) ;
973
+ const recommendedCompletion = previousToken && getRecommendedCompletion ( previousToken , position , sourceFile , typeChecker ) ;
960
974
return { kind : CompletionDataKind . Data , symbols, completionKind, propertyAccessToConvert, isNewIdentifierLocation, location, keywordFilters, symbolToOriginInfoMap, recommendedCompletion, previousToken, isJsxInitializer } ;
961
975
962
976
type JSDocTagWithTypeExpression = JSDocParameterTag | JSDocPropertyTag | JSDocReturnTag | JSDocTypeTag | JSDocTypedefTag ;
0 commit comments