fix(54013): bug: Incorrect Typescript completion with computed property name (#54067)

This commit is contained in:
Oleksandr T 2023-07-18 23:34:01 +03:00 committed by GitHub
parent 4879087bef
commit 405251ebd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 401 additions and 1 deletions

View File

@ -3585,7 +3585,8 @@ function getCompletionData(
const nameSymbol = leftMostName && typeChecker.getSymbolAtLocation(leftMostName);
// If this is nested like for `namespace N { export const sym = Symbol(); }`, we'll add the completion for `N`.
const firstAccessibleSymbol = nameSymbol && getFirstSymbolInChain(nameSymbol, contextToken, typeChecker);
if (firstAccessibleSymbol && addToSeen(seenPropertySymbols, getSymbolId(firstAccessibleSymbol))) {
const firstAccessibleSymbolId = firstAccessibleSymbol && getSymbolId(firstAccessibleSymbol);
if (firstAccessibleSymbolId && addToSeen(seenPropertySymbols, firstAccessibleSymbolId)) {
const index = symbols.length;
symbols.push(firstAccessibleSymbol);
const moduleSymbol = firstAccessibleSymbol.parent;
@ -3621,6 +3622,9 @@ function getCompletionData(
}
}
else if (preferences.includeCompletionsWithInsertText) {
if (firstAccessibleSymbolId && seenPropertySymbols.has(firstAccessibleSymbolId)) {
return;
}
addSymbolOriginInfo(symbol);
addSymbolSortInfo(symbol);
symbols.push(symbol);

View File

@ -0,0 +1,365 @@
=== /tests/cases/fourslash/completionsUniqueSymbol2.ts ===
// const a = {
// KEY_1: 'key_1',
// KEY_2: 'key_2',
// KEY_3: 'key_3',
// } as const;
//
// const b = {
// KEY_1: 'key_1',
// KEY_2: 'key_2',
// KEY_3: 'key_3',
// } as const;
//
// interface I {
// [b.KEY_1]: string,
// [a.KEY_2]: string,
// [a.KEY_3]: string
// }
//
// const foo: I = {
// key_1: 'value_1',
// key_2: 'value_2',
// key_3: 'value_3',
// }
//
// foo.
// ^
// | ----------------------------------------------------------------------
// | const a: {
// | readonly KEY_1: "key_1";
// | readonly KEY_2: "key_2";
// | readonly KEY_3: "key_3";
// | }
// | const b: {
// | readonly KEY_1: "key_1";
// | readonly KEY_2: "key_2";
// | readonly KEY_3: "key_3";
// | }
// | ----------------------------------------------------------------------
[
{
"marker": {
"fileName": "/tests/cases/fourslash/completionsUniqueSymbol2.ts",
"position": 345,
"name": ""
},
"item": {
"flags": 0,
"isGlobalCompletion": false,
"isMemberCompletion": true,
"isNewIdentifierLocation": false,
"entries": [
{
"name": "a",
"kind": "const",
"kindModifiers": "",
"sortText": "11",
"insertText": "[a]",
"replacementSpan": {
"start": 344,
"length": 1
},
"displayParts": [
{
"text": "const",
"kind": "keyword"
},
{
"text": " ",
"kind": "space"
},
{
"text": "a",
"kind": "localName"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "{",
"kind": "punctuation"
},
{
"text": "\n",
"kind": "lineBreak"
},
{
"text": " ",
"kind": "space"
},
{
"text": "readonly",
"kind": "keyword"
},
{
"text": " ",
"kind": "space"
},
{
"text": "KEY_1",
"kind": "propertyName"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "\"key_1\"",
"kind": "stringLiteral"
},
{
"text": ";",
"kind": "punctuation"
},
{
"text": "\n",
"kind": "lineBreak"
},
{
"text": " ",
"kind": "space"
},
{
"text": "readonly",
"kind": "keyword"
},
{
"text": " ",
"kind": "space"
},
{
"text": "KEY_2",
"kind": "propertyName"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "\"key_2\"",
"kind": "stringLiteral"
},
{
"text": ";",
"kind": "punctuation"
},
{
"text": "\n",
"kind": "lineBreak"
},
{
"text": " ",
"kind": "space"
},
{
"text": "readonly",
"kind": "keyword"
},
{
"text": " ",
"kind": "space"
},
{
"text": "KEY_3",
"kind": "propertyName"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "\"key_3\"",
"kind": "stringLiteral"
},
{
"text": ";",
"kind": "punctuation"
},
{
"text": "\n",
"kind": "lineBreak"
},
{
"text": "}",
"kind": "punctuation"
}
],
"documentation": []
},
{
"name": "b",
"kind": "const",
"kindModifiers": "",
"sortText": "11",
"insertText": "[b]",
"replacementSpan": {
"start": 344,
"length": 1
},
"displayParts": [
{
"text": "const",
"kind": "keyword"
},
{
"text": " ",
"kind": "space"
},
{
"text": "b",
"kind": "localName"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "{",
"kind": "punctuation"
},
{
"text": "\n",
"kind": "lineBreak"
},
{
"text": " ",
"kind": "space"
},
{
"text": "readonly",
"kind": "keyword"
},
{
"text": " ",
"kind": "space"
},
{
"text": "KEY_1",
"kind": "propertyName"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "\"key_1\"",
"kind": "stringLiteral"
},
{
"text": ";",
"kind": "punctuation"
},
{
"text": "\n",
"kind": "lineBreak"
},
{
"text": " ",
"kind": "space"
},
{
"text": "readonly",
"kind": "keyword"
},
{
"text": " ",
"kind": "space"
},
{
"text": "KEY_2",
"kind": "propertyName"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "\"key_2\"",
"kind": "stringLiteral"
},
{
"text": ";",
"kind": "punctuation"
},
{
"text": "\n",
"kind": "lineBreak"
},
{
"text": " ",
"kind": "space"
},
{
"text": "readonly",
"kind": "keyword"
},
{
"text": " ",
"kind": "space"
},
{
"text": "KEY_3",
"kind": "propertyName"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "\"key_3\"",
"kind": "stringLiteral"
},
{
"text": ";",
"kind": "punctuation"
},
{
"text": "\n",
"kind": "lineBreak"
},
{
"text": "}",
"kind": "punctuation"
}
],
"documentation": []
}
]
}
}
]

View File

@ -0,0 +1,31 @@
/// <reference path="fourslash.ts" />
////const a = {
//// KEY_1: 'key_1',
//// KEY_2: 'key_2',
//// KEY_3: 'key_3',
////} as const;
////
////const b = {
//// KEY_1: 'key_1',
//// KEY_2: 'key_2',
//// KEY_3: 'key_3',
////} as const;
////
////interface I {
//// [b.KEY_1]: string,
//// [a.KEY_2]: string,
//// [a.KEY_3]: string
////}
////
////const foo: I = {
//// key_1: 'value_1',
//// key_2: 'value_2',
//// key_3: 'value_3',
////}
////
////foo./**/
verify.baselineCompletions({
includeCompletionsWithInsertText: true,
});