Skip to content

Commit 5a32fb0

Browse files
author
Andy
authored
Work around bug with global completion with invalid identifier (#23086) (#23093)
1 parent 056b6c4 commit 5a32fb0

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/services/completions.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,13 @@ namespace ts.Completions {
210210
if (origin && origin.type === "this-type") {
211211
insertText = needsConvertPropertyAccess ? `this[${quote(name)}]` : `this.${name}`;
212212
}
213-
else if (needsConvertPropertyAccess) {
213+
// We should only have needsConvertPropertyAccess if there's a property access to convert. But see #21790.
214+
// Somehow there was a global with a non-identifier name. Hopefully someone will complain about getting a "foo bar" global completion and provide a repro.
215+
else if (needsConvertPropertyAccess && propertyAccessToConvert) {
214216
insertText = `[${quote(name)}]`;
215-
const dot = findChildOfKind(propertyAccessToConvert!, SyntaxKind.DotToken, sourceFile)!;
217+
const dot = findChildOfKind(propertyAccessToConvert, SyntaxKind.DotToken, sourceFile)!;
216218
// If the text after the '.' starts with this name, write over it. Else, add new text.
217-
const end = startsWith(name, propertyAccessToConvert!.name.text) ? propertyAccessToConvert!.name.end : dot.end;
219+
const end = startsWith(name, propertyAccessToConvert.name.text) ? propertyAccessToConvert.name.end : dot.end;
218220
replacementSpan = createTextSpanFromBounds(dot.getStart(sourceFile), end);
219221
}
220222

@@ -2133,7 +2135,7 @@ namespace ts.Completions {
21332135
// TODO: GH#18169
21342136
return { name: JSON.stringify(name), needsConvertPropertyAccess: false };
21352137
case CompletionKind.PropertyAccess:
2136-
case CompletionKind.Global:
2138+
case CompletionKind.Global: // For a 'this.' completion it will be in a global context, but may have a non-identifier name.
21372139
// Don't add a completion for a name starting with a space. See https://github.com/Microsoft/TypeScript/pull/20547
21382140
return name.charCodeAt(0) === CharacterCodes.space ? undefined : { name, needsConvertPropertyAccess: true };
21392141
case CompletionKind.None:

0 commit comments

Comments
 (0)