From 3e3fb77ab182496e760a610fbfbcd617df947829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Sun, 1 Oct 2023 01:27:33 +0200 Subject: [PATCH 1/2] Fixed a crash in `getPropertyNameForPropertyNameNode` on `NoSubstitutionTemplateLiteral` --- src/compiler/checker.ts | 2 +- src/compiler/types.ts | 2 +- src/compiler/utilities.ts | 1 + .../indexTypeNoSubstitutionTemplateLiteral.js | 14 ++++++++++++++ ...exTypeNoSubstitutionTemplateLiteral.symbols | 15 +++++++++++++++ ...ndexTypeNoSubstitutionTemplateLiteral.types | 18 ++++++++++++++++++ .../indexTypeNoSubstitutionTemplateLiteral.ts | 7 +++++++ 7 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 tests/baselines/reference/indexTypeNoSubstitutionTemplateLiteral.js create mode 100644 tests/baselines/reference/indexTypeNoSubstitutionTemplateLiteral.symbols create mode 100644 tests/baselines/reference/indexTypeNoSubstitutionTemplateLiteral.types create mode 100644 tests/cases/compiler/indexTypeNoSubstitutionTemplateLiteral.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 58d6a8b867e20..563ab57ccc801 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -17727,7 +17727,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { return false; } - function getPropertyNameFromIndex(indexType: Type, accessNode: StringLiteral | Identifier | PrivateIdentifier | ObjectBindingPattern | ArrayBindingPattern | ComputedPropertyName | NumericLiteral | IndexedAccessTypeNode | ElementAccessExpression | SyntheticExpression | undefined) { + function getPropertyNameFromIndex(indexType: Type, accessNode: PropertyName | ObjectBindingPattern | ArrayBindingPattern | IndexedAccessTypeNode | ElementAccessExpression | SyntheticExpression | undefined) { return isTypeUsableAsPropertyName(indexType) ? getPropertyNameFromType(indexType) : accessNode && isPropertyName(accessNode) ? diff --git a/src/compiler/types.ts b/src/compiler/types.ts index d4860ec7621a8..9a185a4923249 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1692,7 +1692,7 @@ export interface QualifiedName extends Node, FlowContainer { export type EntityName = Identifier | QualifiedName; -export type PropertyName = Identifier | StringLiteral | NumericLiteral | ComputedPropertyName | PrivateIdentifier; +export type PropertyName = Identifier | StringLiteral | NoSubstitutionTemplateLiteral | NumericLiteral | ComputedPropertyName | PrivateIdentifier; export type MemberName = Identifier | PrivateIdentifier; diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 9d5e2de2bed23..126b74d91dac7 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -5062,6 +5062,7 @@ export function getPropertyNameForPropertyNameNode(name: PropertyName | JsxAttri case SyntaxKind.PrivateIdentifier: return name.escapedText; case SyntaxKind.StringLiteral: + case SyntaxKind.NoSubstitutionTemplateLiteral: case SyntaxKind.NumericLiteral: return escapeLeadingUnderscores(name.text); case SyntaxKind.ComputedPropertyName: diff --git a/tests/baselines/reference/indexTypeNoSubstitutionTemplateLiteral.js b/tests/baselines/reference/indexTypeNoSubstitutionTemplateLiteral.js new file mode 100644 index 0000000000000..899ea0c480a39 --- /dev/null +++ b/tests/baselines/reference/indexTypeNoSubstitutionTemplateLiteral.js @@ -0,0 +1,14 @@ +//// [tests/cases/compiler/indexTypeNoSubstitutionTemplateLiteral.ts] //// + +//// [indexTypeNoSubstitutionTemplateLiteral.ts] +function Foo() {} +Foo[`b`] = function () {}; + +type Test = keyof typeof Foo; + + + +//// [indexTypeNoSubstitutionTemplateLiteral.js] +"use strict"; +function Foo() { } +Foo["b"] = function () { }; diff --git a/tests/baselines/reference/indexTypeNoSubstitutionTemplateLiteral.symbols b/tests/baselines/reference/indexTypeNoSubstitutionTemplateLiteral.symbols new file mode 100644 index 0000000000000..ac79be00f624e --- /dev/null +++ b/tests/baselines/reference/indexTypeNoSubstitutionTemplateLiteral.symbols @@ -0,0 +1,15 @@ +//// [tests/cases/compiler/indexTypeNoSubstitutionTemplateLiteral.ts] //// + +=== indexTypeNoSubstitutionTemplateLiteral.ts === +function Foo() {} +>Foo : Symbol(Foo, Decl(indexTypeNoSubstitutionTemplateLiteral.ts, 0, 0), Decl(indexTypeNoSubstitutionTemplateLiteral.ts, 0, 17)) + +Foo[`b`] = function () {}; +>Foo : Symbol(Foo, Decl(indexTypeNoSubstitutionTemplateLiteral.ts, 0, 0), Decl(indexTypeNoSubstitutionTemplateLiteral.ts, 0, 17)) +>`b` : Symbol(Foo[`b`], Decl(indexTypeNoSubstitutionTemplateLiteral.ts, 0, 17)) + +type Test = keyof typeof Foo; +>Test : Symbol(Test, Decl(indexTypeNoSubstitutionTemplateLiteral.ts, 1, 26)) +>Foo : Symbol(Foo, Decl(indexTypeNoSubstitutionTemplateLiteral.ts, 0, 0), Decl(indexTypeNoSubstitutionTemplateLiteral.ts, 0, 17)) + + diff --git a/tests/baselines/reference/indexTypeNoSubstitutionTemplateLiteral.types b/tests/baselines/reference/indexTypeNoSubstitutionTemplateLiteral.types new file mode 100644 index 0000000000000..72b90d63893ab --- /dev/null +++ b/tests/baselines/reference/indexTypeNoSubstitutionTemplateLiteral.types @@ -0,0 +1,18 @@ +//// [tests/cases/compiler/indexTypeNoSubstitutionTemplateLiteral.ts] //// + +=== indexTypeNoSubstitutionTemplateLiteral.ts === +function Foo() {} +>Foo : typeof Foo + +Foo[`b`] = function () {}; +>Foo[`b`] = function () {} : () => void +>Foo[`b`] : () => void +>Foo : typeof Foo +>`b` : "b" +>function () {} : () => void + +type Test = keyof typeof Foo; +>Test : "b" +>Foo : typeof Foo + + diff --git a/tests/cases/compiler/indexTypeNoSubstitutionTemplateLiteral.ts b/tests/cases/compiler/indexTypeNoSubstitutionTemplateLiteral.ts new file mode 100644 index 0000000000000..65d0809a38f5e --- /dev/null +++ b/tests/cases/compiler/indexTypeNoSubstitutionTemplateLiteral.ts @@ -0,0 +1,7 @@ +// @strict: true + +function Foo() {} +Foo[`b`] = function () {}; + +type Test = keyof typeof Foo; + From 9a52b8edd190eeaa7788b55691f9dc203521618e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Sun, 1 Oct 2023 08:14:38 +0200 Subject: [PATCH 2/2] Update the API baseline --- tests/baselines/reference/api/typescript.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 8f70f7cfa107a..00e41bf46de3f 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -4976,7 +4976,7 @@ declare namespace ts { readonly right: Identifier; } type EntityName = Identifier | QualifiedName; - type PropertyName = Identifier | StringLiteral | NumericLiteral | ComputedPropertyName | PrivateIdentifier; + type PropertyName = Identifier | StringLiteral | NoSubstitutionTemplateLiteral | NumericLiteral | ComputedPropertyName | PrivateIdentifier; type MemberName = Identifier | PrivateIdentifier; type DeclarationName = PropertyName | JsxAttributeName | StringLiteralLike | ElementAccessExpression | BindingPattern | EntityNameExpression; interface Declaration extends Node {