Skip to content

Commit 7eaad14

Browse files
author
Andy
authored
Work around bug with global completion with invalid identifier (#23086)
1 parent 8e14ac7 commit 7eaad14

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
@@ -206,11 +206,13 @@ namespace ts.Completions {
206206
if (origin && origin.type === "this-type") {
207207
insertText = needsConvertPropertyAccess ? `this[${quote(name, preferences)}]` : `this.${name}`;
208208
}
209-
else if (needsConvertPropertyAccess) {
209+
// We should only have needsConvertPropertyAccess if there's a property access to convert. But see #21790.
210+
// 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.
211+
else if (needsConvertPropertyAccess && propertyAccessToConvert) {
210212
insertText = `[${quote(name, preferences)}]`;
211-
const dot = findChildOfKind(propertyAccessToConvert!, SyntaxKind.DotToken, sourceFile)!;
213+
const dot = findChildOfKind(propertyAccessToConvert, SyntaxKind.DotToken, sourceFile)!;
212214
// If the text after the '.' starts with this name, write over it. Else, add new text.
213-
const end = startsWith(name, propertyAccessToConvert!.name.text) ? propertyAccessToConvert!.name.end : dot.end;
215+
const end = startsWith(name, propertyAccessToConvert.name.text) ? propertyAccessToConvert.name.end : dot.end;
214216
replacementSpan = createTextSpanFromBounds(dot.getStart(sourceFile), end);
215217
}
216218

@@ -2057,7 +2059,7 @@ namespace ts.Completions {
20572059
// TODO: GH#18169
20582060
return { name: JSON.stringify(name), needsConvertPropertyAccess: false };
20592061
case CompletionKind.PropertyAccess:
2060-
case CompletionKind.Global:
2062+
case CompletionKind.Global: // For a 'this.' completion it will be in a global context, but may have a non-identifier name.
20612063
// Don't add a completion for a name starting with a space. See https://github.com/Microsoft/TypeScript/pull/20547
20622064
return name.charCodeAt(0) === CharacterCodes.space ? undefined : { name, needsConvertPropertyAccess: true };
20632065
case CompletionKind.None:

0 commit comments

Comments
 (0)