Skip to content

Fixed a crash in getPropertyNameForPropertyNameNode on NoSubstitutionTemplateLiteral #55930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) ?
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 () { };
Original file line number Diff line number Diff line change
@@ -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))


Original file line number Diff line number Diff line change
@@ -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


Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @strict: true

function Foo() {}
Foo[`b`] = function () {};

type Test = keyof typeof Foo;