Don't crash on non-literal computed property names during getPropertyAssignment (#48079)

This commit is contained in:
Jake Bailey
2022-03-07 14:18:55 -08:00
committed by GitHub
parent 418989b3c3
commit d8b21a8d6c
3 changed files with 20 additions and 6 deletions

View File

@@ -987,7 +987,7 @@ namespace ts {
return name.kind === SyntaxKind.ComputedPropertyName && !isStringOrNumericLiteralLike(name.expression);
}
export function getTextOfPropertyName(name: PropertyName | NoSubstitutionTemplateLiteral): __String {
export function tryGetTextOfPropertyName(name: PropertyName | NoSubstitutionTemplateLiteral): __String | undefined {
switch (name.kind) {
case SyntaxKind.Identifier:
case SyntaxKind.PrivateIdentifier:
@@ -998,12 +998,16 @@ namespace ts {
return escapeLeadingUnderscores(name.text);
case SyntaxKind.ComputedPropertyName:
if (isStringOrNumericLiteralLike(name.expression)) return escapeLeadingUnderscores(name.expression.text);
return Debug.fail("Text of property name cannot be read from non-literal-valued ComputedPropertyNames");
return undefined;
default:
return Debug.assertNever(name);
}
}
export function getTextOfPropertyName(name: PropertyName | NoSubstitutionTemplateLiteral): __String {
return Debug.checkDefined(tryGetTextOfPropertyName(name));
}
export function entityNameToString(name: EntityNameOrEntityNameExpression | JSDocMemberName | JsxTagNameExpression | PrivateIdentifier): string {
switch (name.kind) {
case SyntaxKind.ThisKeyword:
@@ -1573,7 +1577,7 @@ namespace ts {
export function getPropertyAssignment(objectLiteral: ObjectLiteralExpression, key: string, key2?: string): readonly PropertyAssignment[] {
return objectLiteral.properties.filter((property): property is PropertyAssignment => {
if (property.kind === SyntaxKind.PropertyAssignment) {
const propName = getTextOfPropertyName(property.name);
const propName = tryGetTextOfPropertyName(property.name);
return key === propName || (!!key2 && key2 === propName);
}
return false;

View File

@@ -110,14 +110,12 @@ namespace ts.server {
}
}
// verify the sequence numbers
Debug.assert(response.request_seq === request.seq, "Malformed response: response sequence number did not match request sequence number.");
// unmarshal errors
if (!response.success) {
throw new Error("Error " + response.message);
}
Debug.assert(response.request_seq === request.seq, "Malformed response: response sequence number did not match request sequence number.");
Debug.assert(expectEmptyBody || !!response.body, "Malformed response: Unexpected empty response body.");
Debug.assert(!expectEmptyBody || !response.body, "Malformed response: Unexpected non-empty response body.");