Skip to content

Commit

Permalink
Merge pull request #18317 from Microsoft/object-literal-enum-property…
Browse files Browse the repository at this point in the history
…-names

Object literals computed property names allow literal-typed expressions
  • Loading branch information
sandersn authored Sep 7, 2017
2 parents 1b5a0ae + 3c5b2a5 commit 397ff84
Show file tree
Hide file tree
Showing 15 changed files with 511 additions and 23 deletions.
13 changes: 10 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13532,6 +13532,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 @@ -13542,6 +13543,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 @@ -13558,7 +13565,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 @@ -13568,7 +13575,7 @@ namespace ts {
if (isOptional) {
prop.flags |= SymbolFlags.Optional;
}
if (hasDynamicName(memberDecl)) {
if (!literalName && hasDynamicName(memberDecl)) {
patternWithComputedProperties = true;
}
}
Expand Down Expand Up @@ -13626,7 +13633,7 @@ namespace ts {
checkNodeDeferred(memberDecl);
}

if (hasDynamicName(memberDecl)) {
if (!literalName && hasDynamicName(memberDecl)) {
if (isNumericName(memberDecl.name)) {
hasComputedNumberProperty = true;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames46_ES5.types
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
=== tests/cases/conformance/es6/computedProperties/computedPropertyNames46_ES5.ts ===
var o = {
>o : { [x: number]: number; }
>{ ["" || 0]: 0} : { [x: number]: number; }
>o : { ["" || 0]: number; }
>{ ["" || 0]: 0} : { ["" || 0]: number; }

["" || 0]: 0
>"" || 0 : 0
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames46_ES6.types
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
=== tests/cases/conformance/es6/computedProperties/computedPropertyNames46_ES6.ts ===
var o = {
>o : { [x: number]: number; }
>{ ["" || 0]: 0} : { [x: number]: number; }
>o : { ["" || 0]: number; }
>{ ["" || 0]: 0} : { ["" || 0]: number; }

["" || 0]: 0
>"" || 0 : 0
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames47_ES5.types
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ enum E2 { x }
>x : E2

var o = {
>o : { [x: number]: number; }
>{ [E1.x || E2.x]: 0} : { [x: number]: number; }
>o : { [E1.x || E2.x]: number; }
>{ [E1.x || E2.x]: 0} : { [E1.x || E2.x]: number; }

[E1.x || E2.x]: 0
>E1.x || E2.x : E2
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames47_ES6.types
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ enum E2 { x }
>x : E2

var o = {
>o : { [x: number]: number; }
>{ [E1.x || E2.x]: 0} : { [x: number]: number; }
>o : { [E1.x || E2.x]: number; }
>{ [E1.x || E2.x]: 0} : { [E1.x || E2.x]: number; }

[E1.x || E2.x]: 0
>E1.x || E2.x : E2
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames48_ES5.types
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extractIndexer({
extractIndexer({
>extractIndexer({ [E.x]: ""}) : string
>extractIndexer : <T>(p: { [n: number]: T; }) => T
>{ [E.x]: ""} : { [x: number]: string; }
>{ [E.x]: ""} : { [E.x]: string; }

[E.x]: ""
>E.x : E
Expand All @@ -41,7 +41,7 @@ extractIndexer({
extractIndexer({
>extractIndexer({ ["" || 0]: ""}) : string
>extractIndexer : <T>(p: { [n: number]: T; }) => T
>{ ["" || 0]: ""} : { [x: number]: string; }
>{ ["" || 0]: ""} : { ["" || 0]: string; }

["" || 0]: ""
>"" || 0 : 0
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames48_ES6.types
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extractIndexer({
extractIndexer({
>extractIndexer({ [E.x]: ""}) : string
>extractIndexer : <T>(p: { [n: number]: T; }) => T
>{ [E.x]: ""} : { [x: number]: string; }
>{ [E.x]: ""} : { [E.x]: string; }

[E.x]: ""
>E.x : E
Expand All @@ -41,7 +41,7 @@ extractIndexer({
extractIndexer({
>extractIndexer({ ["" || 0]: ""}) : string
>extractIndexer : <T>(p: { [n: number]: T; }) => T
>{ ["" || 0]: ""} : { [x: number]: string; }
>{ ["" || 0]: ""} : { ["" || 0]: string; }

["" || 0]: ""
>"" || 0 : 0
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames4_ES5.types
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ var a: any;
>a : any

var v = {
>v : { [x: string]: string | number; [x: number]: string | number; [""]: number; [0]: number; }
>{ [s]: 0, [n]: n, [s + s]: 1, [s + n]: 2, [+s]: s, [""]: 0, [0]: 0, [a]: 1, [<any>true]: 0, [`hello bye`]: 0, [`hello ${a} bye`]: 0} : { [x: string]: string | number; [x: number]: string | number; [""]: number; [0]: number; }
>v : { [x: string]: string | number; [x: number]: string | number; [""]: number; [0]: number; [`hello bye`]: number; }
>{ [s]: 0, [n]: n, [s + s]: 1, [s + n]: 2, [+s]: s, [""]: 0, [0]: 0, [a]: 1, [<any>true]: 0, [`hello bye`]: 0, [`hello ${a} bye`]: 0} : { [x: string]: string | number; [x: number]: string | number; [""]: number; [0]: number; [`hello bye`]: number; }

[s]: 0,
>s : string
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames4_ES6.types
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ var a: any;
>a : any

var v = {
>v : { [x: string]: string | number; [x: number]: string | number; [""]: number; [0]: number; }
>{ [s]: 0, [n]: n, [s + s]: 1, [s + n]: 2, [+s]: s, [""]: 0, [0]: 0, [a]: 1, [<any>true]: 0, [`hello bye`]: 0, [`hello ${a} bye`]: 0} : { [x: string]: string | number; [x: number]: string | number; [""]: number; [0]: number; }
>v : { [x: string]: string | number; [x: number]: string | number; [""]: number; [0]: number; [`hello bye`]: number; }
>{ [s]: 0, [n]: n, [s + s]: 1, [s + n]: 2, [+s]: s, [""]: 0, [0]: 0, [a]: 1, [<any>true]: 0, [`hello bye`]: 0, [`hello ${a} bye`]: 0} : { [x: string]: string | number; [x: number]: string | number; [""]: number; [0]: number; [`hello bye`]: number; }

[s]: 0,
>s : string
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames7_ES5.types
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ enum E {
>member : E
}
var v = {
>v : { [x: number]: number; }
>{ [E.member]: 0} : { [x: number]: number; }
>v : { [E.member]: number; }
>{ [E.member]: 0} : { [E.member]: number; }

[E.member]: 0
>E.member : E
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames7_ES6.types
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ enum E {
>member : E
}
var v = {
>v : { [x: number]: number; }
>{ [E.member]: 0} : { [x: number]: number; }
>v : { [E.member]: number; }
>{ [E.member]: 0} : { [E.member]: number; }

[E.member]: 0
>E.member : E
Expand Down
108 changes: 108 additions & 0 deletions tests/baselines/reference/objectLiteralEnumPropertyNames.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//// [objectLiteralEnumPropertyNames.ts]
// Fixes #16887
enum Strs {
A = 'a',
B = 'b'
}
type TestStrs = { [key in Strs]: string }
const x: TestStrs = {
[Strs.A]: 'xo',
[Strs.B]: 'xe'
}
const ux = {
[Strs.A]: 'xo',
[Strs.B]: 'xe'
}
const y: TestStrs = {
['a']: 'yo',
['b']: 'ye'
}
const a = 'a';
const b = 'b';
const z: TestStrs = {
[a]: 'zo',
[b]: 'ze'
}
const uz = {
[a]: 'zo',
[b]: 'ze'
}

enum Nums {
A,
B
}
type TestNums = { 0: number, 1: number }
const n: TestNums = {
[Nums.A]: 1,
[Nums.B]: 2
}
const un = {
[Nums.A]: 3,
[Nums.B]: 4
}
const an = 0;
const bn = 1;
const m: TestNums = {
[an]: 5,
[bn]: 6
}
const um = {
[an]: 7,
[bn]: 8
}


//// [objectLiteralEnumPropertyNames.js]
// Fixes #16887
var Strs;
(function (Strs) {
Strs["A"] = "a";
Strs["B"] = "b";
})(Strs || (Strs = {}));
var x = (_a = {},
_a[Strs.A] = 'xo',
_a[Strs.B] = 'xe',
_a);
var ux = (_b = {},
_b[Strs.A] = 'xo',
_b[Strs.B] = 'xe',
_b);
var y = (_c = {},
_c['a'] = 'yo',
_c['b'] = 'ye',
_c);
var a = 'a';
var b = 'b';
var z = (_d = {},
_d[a] = 'zo',
_d[b] = 'ze',
_d);
var uz = (_e = {},
_e[a] = 'zo',
_e[b] = 'ze',
_e);
var Nums;
(function (Nums) {
Nums[Nums["A"] = 0] = "A";
Nums[Nums["B"] = 1] = "B";
})(Nums || (Nums = {}));
var n = (_f = {},
_f[Nums.A] = 1,
_f[Nums.B] = 2,
_f);
var un = (_g = {},
_g[Nums.A] = 3,
_g[Nums.B] = 4,
_g);
var an = 0;
var bn = 1;
var m = (_h = {},
_h[an] = 5,
_h[bn] = 6,
_h);
var um = (_j = {},
_j[an] = 7,
_j[bn] = 8,
_j);
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
Loading

0 comments on commit 397ff84

Please sign in to comment.