Skip to content

Commit d8b21a8

Browse files
authored
Don't crash on non-literal computed property names during getPropertyAssignment (microsoft#48079)
1 parent 418989b commit d8b21a8

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

src/compiler/utilities.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ namespace ts {
987987
return name.kind === SyntaxKind.ComputedPropertyName && !isStringOrNumericLiteralLike(name.expression);
988988
}
989989

990-
export function getTextOfPropertyName(name: PropertyName | NoSubstitutionTemplateLiteral): __String {
990+
export function tryGetTextOfPropertyName(name: PropertyName | NoSubstitutionTemplateLiteral): __String | undefined {
991991
switch (name.kind) {
992992
case SyntaxKind.Identifier:
993993
case SyntaxKind.PrivateIdentifier:
@@ -998,12 +998,16 @@ namespace ts {
998998
return escapeLeadingUnderscores(name.text);
999999
case SyntaxKind.ComputedPropertyName:
10001000
if (isStringOrNumericLiteralLike(name.expression)) return escapeLeadingUnderscores(name.expression.text);
1001-
return Debug.fail("Text of property name cannot be read from non-literal-valued ComputedPropertyNames");
1001+
return undefined;
10021002
default:
10031003
return Debug.assertNever(name);
10041004
}
10051005
}
10061006

1007+
export function getTextOfPropertyName(name: PropertyName | NoSubstitutionTemplateLiteral): __String {
1008+
return Debug.checkDefined(tryGetTextOfPropertyName(name));
1009+
}
1010+
10071011
export function entityNameToString(name: EntityNameOrEntityNameExpression | JSDocMemberName | JsxTagNameExpression | PrivateIdentifier): string {
10081012
switch (name.kind) {
10091013
case SyntaxKind.ThisKeyword:
@@ -1573,7 +1577,7 @@ namespace ts {
15731577
export function getPropertyAssignment(objectLiteral: ObjectLiteralExpression, key: string, key2?: string): readonly PropertyAssignment[] {
15741578
return objectLiteral.properties.filter((property): property is PropertyAssignment => {
15751579
if (property.kind === SyntaxKind.PropertyAssignment) {
1576-
const propName = getTextOfPropertyName(property.name);
1580+
const propName = tryGetTextOfPropertyName(property.name);
15771581
return key === propName || (!!key2 && key2 === propName);
15781582
}
15791583
return false;

src/harness/client.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,12 @@ namespace ts.server {
110110
}
111111
}
112112

113-
// verify the sequence numbers
114-
Debug.assert(response.request_seq === request.seq, "Malformed response: response sequence number did not match request sequence number.");
115-
116113
// unmarshal errors
117114
if (!response.success) {
118115
throw new Error("Error " + response.message);
119116
}
120117

118+
Debug.assert(response.request_seq === request.seq, "Malformed response: response sequence number did not match request sequence number.");
121119
Debug.assert(expectEmptyBody || !!response.body, "Malformed response: Unexpected empty response body.");
122120
Debug.assert(!expectEmptyBody || !response.body, "Malformed response: Unexpected non-empty response body.");
123121

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path="../fourslash.ts"/>
2+
3+
// @filename: tsconfig.json
4+
////{
5+
//// ["oops!" + 42]: "true",
6+
//// "files": [
7+
//// "nonexistentfile.ts"
8+
//// ],
9+
//// "compileOnSave": true
10+
////}
11+
12+
verify.getSemanticDiagnostics([]);

0 commit comments

Comments
 (0)