From 382de9176308dafa5c1edc86611076139937b871 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Tue, 2 Jan 2024 21:34:45 +0200 Subject: [PATCH] fix(56737): unique symbol quick info is different in JS than TS (#56743) --- src/compiler/checker.ts | 6 ++ .../quickInfoUniqueSymbolJsDoc.baseline | 74 +++++++++++++++++++ .../reference/uniqueSymbolJs.errors.txt | 15 ++++ .../reference/uniqueSymbolJs.symbols | 11 +++ .../baselines/reference/uniqueSymbolJs.types | 12 +++ .../uniqueSymbolsDeclarationsInJs.types | 6 +- .../uniqueSymbolsDeclarationsInJsErrors.types | 2 +- tests/cases/compiler/uniqueSymbolJs.ts | 11 +++ .../fourslash/quickInfoUniqueSymbolJsDoc.ts | 10 +++ 9 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 tests/baselines/reference/quickInfoUniqueSymbolJsDoc.baseline create mode 100644 tests/baselines/reference/uniqueSymbolJs.errors.txt create mode 100644 tests/baselines/reference/uniqueSymbolJs.symbols create mode 100644 tests/baselines/reference/uniqueSymbolJs.types create mode 100644 tests/cases/compiler/uniqueSymbolJs.ts create mode 100644 tests/cases/fourslash/quickInfoUniqueSymbolJsDoc.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index fa379549fb7..78d32f8fbd5 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -19107,6 +19107,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } function getESSymbolLikeTypeForNode(node: Node) { + if (isInJSFile(node) && isJSDocTypeExpression(node)) { + const host = getJSDocHost(node); + if (host) { + node = getSingleVariableOfVariableStatement(host) || host; + } + } if (isValidESSymbolDeclaration(node)) { const symbol = isCommonJsExportPropertyAssignment(node) ? getSymbolOfNode((node as BinaryExpression).left) : getSymbolOfNode(node); if (symbol) { diff --git a/tests/baselines/reference/quickInfoUniqueSymbolJsDoc.baseline b/tests/baselines/reference/quickInfoUniqueSymbolJsDoc.baseline new file mode 100644 index 00000000000..ddc8034c631 --- /dev/null +++ b/tests/baselines/reference/quickInfoUniqueSymbolJsDoc.baseline @@ -0,0 +1,74 @@ +// === QuickInfo === +=== /tests/cases/fourslash/./a.js === +// /** @type {unique symbol} */ +// const foo = Symbol(); +// foo +// ^^^ +// | ---------------------------------------------------------------------- +// | const foo: typeof foo +// | @type {unique symbol} +// | ---------------------------------------------------------------------- + +[ + { + "marker": { + "fileName": "/tests/cases/fourslash/./a.js", + "position": 54, + "name": "" + }, + "item": { + "kind": "const", + "kindModifiers": "", + "textSpan": { + "start": 51, + "length": 3 + }, + "displayParts": [ + { + "text": "const", + "kind": "keyword" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "foo", + "kind": "localName" + }, + { + "text": ":", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "typeof", + "kind": "keyword" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "foo", + "kind": "localName" + } + ], + "documentation": [], + "tags": [ + { + "name": "type", + "text": [ + { + "text": "{unique symbol}", + "kind": "text" + } + ] + } + ] + } + } +] \ No newline at end of file diff --git a/tests/baselines/reference/uniqueSymbolJs.errors.txt b/tests/baselines/reference/uniqueSymbolJs.errors.txt new file mode 100644 index 00000000000..3304772c075 --- /dev/null +++ b/tests/baselines/reference/uniqueSymbolJs.errors.txt @@ -0,0 +1,15 @@ +a.js(5,18): error TS1337: An index signature parameter type cannot be a literal type or generic type. Consider using a mapped object type instead. +a.js(5,28): error TS1005: ';' expected. + + +==== ./a.js (2 errors) ==== + /** @type {unique symbol} */ + const foo = Symbol(); + + /** @typedef {{ [foo]: boolean }} A */ + /** @typedef {{ [key: foo] boolean }} B */ + ~~~ +!!! error TS1337: An index signature parameter type cannot be a literal type or generic type. Consider using a mapped object type instead. + ~~~~~~~ +!!! error TS1005: ';' expected. + \ No newline at end of file diff --git a/tests/baselines/reference/uniqueSymbolJs.symbols b/tests/baselines/reference/uniqueSymbolJs.symbols new file mode 100644 index 00000000000..89f44c0c8e3 --- /dev/null +++ b/tests/baselines/reference/uniqueSymbolJs.symbols @@ -0,0 +1,11 @@ +//// [tests/cases/compiler/uniqueSymbolJs.ts] //// + +=== ./a.js === +/** @type {unique symbol} */ +const foo = Symbol(); +>foo : Symbol(foo, Decl(a.js, 1, 5)) +>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --)) + +/** @typedef {{ [foo]: boolean }} A */ +/** @typedef {{ [key: foo] boolean }} B */ + diff --git a/tests/baselines/reference/uniqueSymbolJs.types b/tests/baselines/reference/uniqueSymbolJs.types new file mode 100644 index 00000000000..0d36ae028a1 --- /dev/null +++ b/tests/baselines/reference/uniqueSymbolJs.types @@ -0,0 +1,12 @@ +//// [tests/cases/compiler/uniqueSymbolJs.ts] //// + +=== ./a.js === +/** @type {unique symbol} */ +const foo = Symbol(); +>foo : unique symbol +>Symbol() : unique symbol +>Symbol : SymbolConstructor + +/** @typedef {{ [foo]: boolean }} A */ +/** @typedef {{ [key: foo] boolean }} B */ + diff --git a/tests/baselines/reference/uniqueSymbolsDeclarationsInJs.types b/tests/baselines/reference/uniqueSymbolsDeclarationsInJs.types index 9b449b0321f..e812b448aae 100644 --- a/tests/baselines/reference/uniqueSymbolsDeclarationsInJs.types +++ b/tests/baselines/reference/uniqueSymbolsDeclarationsInJs.types @@ -18,14 +18,14 @@ class C { * @readonly */ static readonlyStaticType; ->readonlyStaticType : symbol +>readonlyStaticType : unique symbol /** * @type {unique symbol} * @readonly */ static readonlyStaticTypeAndCall = Symbol(); ->readonlyStaticTypeAndCall : symbol +>readonlyStaticTypeAndCall : unique symbol >Symbol() : unique symbol >Symbol : SymbolConstructor @@ -50,7 +50,7 @@ class C { /** @type {unique symbol} */ const a = Symbol(); ->a : symbol +>a : unique symbol >Symbol() : unique symbol >Symbol : SymbolConstructor diff --git a/tests/baselines/reference/uniqueSymbolsDeclarationsInJsErrors.types b/tests/baselines/reference/uniqueSymbolsDeclarationsInJsErrors.types index b3827f6afa8..94045cdeb1d 100644 --- a/tests/baselines/reference/uniqueSymbolsDeclarationsInJsErrors.types +++ b/tests/baselines/reference/uniqueSymbolsDeclarationsInJsErrors.types @@ -15,7 +15,7 @@ class C { * @readonly */ static readonlyType; ->readonlyType : symbol +>readonlyType : unique symbol /** * @type {unique symbol} diff --git a/tests/cases/compiler/uniqueSymbolJs.ts b/tests/cases/compiler/uniqueSymbolJs.ts new file mode 100644 index 00000000000..faa4bd5fcf8 --- /dev/null +++ b/tests/cases/compiler/uniqueSymbolJs.ts @@ -0,0 +1,11 @@ +// @target: esnext +// @checkJs: true +// @allowJs: true +// @noEmit: true +// @filename: ./a.js + +/** @type {unique symbol} */ +const foo = Symbol(); + +/** @typedef {{ [foo]: boolean }} A */ +/** @typedef {{ [key: foo] boolean }} B */ diff --git a/tests/cases/fourslash/quickInfoUniqueSymbolJsDoc.ts b/tests/cases/fourslash/quickInfoUniqueSymbolJsDoc.ts new file mode 100644 index 00000000000..58f9feb6259 --- /dev/null +++ b/tests/cases/fourslash/quickInfoUniqueSymbolJsDoc.ts @@ -0,0 +1,10 @@ +/// + +// @checkJs: true +// @allowJs: true +// @filename: ./a.js +/////** @type {unique symbol} */ +////const foo = Symbol(); +////foo/**/ + +verify.baselineQuickInfo();