Skip to content

Commit

Permalink
Object literals computed property names allow literal-typed expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
sandersn committed Sep 7, 2017
1 parent c82881f commit ac58751
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13526,6 +13526,7 @@ namespace ts {
for (let i = 0; i < node.properties.length; i++) {
const memberDecl = node.properties[i];
let member = memberDecl.symbol;
let literalName: __String | undefined;
if (memberDecl.kind === SyntaxKind.PropertyAssignment ||
memberDecl.kind === SyntaxKind.ShorthandPropertyAssignment ||
isObjectLiteralMethod(memberDecl)) {
Expand All @@ -13536,6 +13537,12 @@ namespace ts {

let type: Type;
if (memberDecl.kind === SyntaxKind.PropertyAssignment) {
if (memberDecl.name.kind === SyntaxKind.ComputedPropertyName) {
const t = checkComputedPropertyName(<ComputedPropertyName>memberDecl.name);
if (t.flags & TypeFlags.Literal) {
literalName = escapeLeadingUnderscores("" + (t as LiteralType).value);
}
}
type = checkPropertyAssignment(<PropertyAssignment>memberDecl, checkMode);
}
else if (memberDecl.kind === SyntaxKind.MethodDeclaration) {
Expand All @@ -13552,7 +13559,7 @@ namespace ts {
}

typeFlags |= type.flags;
const prop = createSymbol(SymbolFlags.Property | member.flags, member.escapedName);
const prop = createSymbol(SymbolFlags.Property | member.flags, literalName || member.escapedName);
if (inDestructuringPattern) {
// If object literal is an assignment pattern and if the assignment pattern specifies a default value
// for the property, make the property optional.
Expand All @@ -13562,7 +13569,7 @@ namespace ts {
if (isOptional) {
prop.flags |= SymbolFlags.Optional;
}
if (hasDynamicName(memberDecl)) {
if (!literalName && hasDynamicName(memberDecl)) {
patternWithComputedProperties = true;
}
}
Expand Down Expand Up @@ -13620,7 +13627,7 @@ namespace ts {
checkNodeDeferred(memberDecl);
}

if (hasDynamicName(memberDecl)) {
if (!literalName && hasDynamicName(memberDecl)) {
if (isNumericName(memberDecl.name)) {
hasComputedNumberProperty = true;
}
Expand Down

0 comments on commit ac58751

Please sign in to comment.