@@ -338,6 +338,7 @@ namespace ts.Completions {
338
338
) : CompletionEntry | undefined {
339
339
let insertText : string | undefined ;
340
340
let replacementSpan : TextSpan | undefined ;
341
+
341
342
const insertQuestionDot = origin && originIsNullableMember ( origin ) ;
342
343
const useBraces = origin && originIsSymbolMember ( origin ) || needsConvertPropertyAccess ;
343
344
if ( origin && originIsThisType ( origin ) ) {
@@ -780,7 +781,7 @@ namespace ts.Completions {
780
781
sourceFile : SourceFile ,
781
782
isUncheckedFile : boolean ,
782
783
position : number ,
783
- preferences : Pick < UserPreferences , "includeCompletionsForModuleExports" | "includeCompletionsWithInsertText" > ,
784
+ preferences : Pick < UserPreferences , "includeCompletionsForModuleExports" | "includeCompletionsWithInsertText" | "includeAutomaticOptionalChainCompletions" > ,
784
785
detailsEntryId : CompletionEntryIdentifier | undefined ,
785
786
host : LanguageServiceHost ,
786
787
) : CompletionData | Request | undefined {
@@ -1116,8 +1117,17 @@ namespace ts.Completions {
1116
1117
let type = typeChecker . getTypeOfSymbolAtLocation ( symbol , node ) . getNonOptionalType ( ) ;
1117
1118
let insertQuestionDot = false ;
1118
1119
if ( type . isNullableType ( ) ) {
1119
- insertQuestionDot = isRightOfDot && ! isRightOfQuestionDot ;
1120
- type = type . getNonNullableType ( ) ;
1120
+ const canCorrectToQuestionDot =
1121
+ isRightOfDot &&
1122
+ ! isRightOfQuestionDot &&
1123
+ preferences . includeAutomaticOptionalChainCompletions !== false ;
1124
+
1125
+ if ( canCorrectToQuestionDot || isRightOfQuestionDot ) {
1126
+ type = type . getNonNullableType ( ) ;
1127
+ if ( canCorrectToQuestionDot ) {
1128
+ insertQuestionDot = true ;
1129
+ }
1130
+ }
1121
1131
}
1122
1132
addTypeProperties ( type , ! ! ( node . flags & NodeFlags . AwaitContext ) , insertQuestionDot ) ;
1123
1133
}
@@ -1137,8 +1147,17 @@ namespace ts.Completions {
1137
1147
let type = typeChecker . getTypeAtLocation ( node ) . getNonOptionalType ( ) ;
1138
1148
let insertQuestionDot = false ;
1139
1149
if ( type . isNullableType ( ) ) {
1140
- insertQuestionDot = isRightOfDot && ! isRightOfQuestionDot ;
1141
- type = type . getNonNullableType ( ) ;
1150
+ const canCorrectToQuestionDot =
1151
+ isRightOfDot &&
1152
+ ! isRightOfQuestionDot &&
1153
+ preferences . includeAutomaticOptionalChainCompletions !== false ;
1154
+
1155
+ if ( canCorrectToQuestionDot || isRightOfQuestionDot ) {
1156
+ type = type . getNonNullableType ( ) ;
1157
+ if ( canCorrectToQuestionDot ) {
1158
+ insertQuestionDot = true ;
1159
+ }
1160
+ }
1142
1161
}
1143
1162
addTypeProperties ( type , ! ! ( node . flags & NodeFlags . AwaitContext ) , insertQuestionDot ) ;
1144
1163
}
0 commit comments