From 295a45d85763dfbe66e3e2ea444dc89570cb092d Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Tue, 10 May 2016 17:23:25 -0700 Subject: [PATCH 1/3] Add tests and Update baselines Update tests and baselines Update tests and baselines Update tests for downlevel emit Add tests and update baselines Add tests and baselines Update baselines Update baselines --- .../classExpressionWithStaticProperties1.js | 7 ++- ...assExpressionWithStaticProperties1.symbols | 18 +++++- ...classExpressionWithStaticProperties1.types | 19 +++++- .../classExpressionWithStaticProperties2.js | 13 ++++- ...assExpressionWithStaticProperties2.symbols | 23 +++++++- ...classExpressionWithStaticProperties2.types | 27 ++++++++- .../classExpressionWithStaticProperties3.js | 29 ++++++++++ ...assExpressionWithStaticProperties3.symbols | 42 ++++++++++++++ ...classExpressionWithStaticProperties3.types | 58 +++++++++++++++++++ ...classExpressionWithStaticPropertiesES61.js | 7 ++- ...ExpressionWithStaticPropertiesES61.symbols | 15 ++++- ...ssExpressionWithStaticPropertiesES61.types | 17 +++++- ...classExpressionWithStaticPropertiesES62.js | 13 ++++- ...ExpressionWithStaticPropertiesES62.symbols | 23 +++++++- ...ssExpressionWithStaticPropertiesES62.types | 27 ++++++++- ...classExpressionWithStaticPropertiesES63.js | 23 ++++++++ ...ExpressionWithStaticPropertiesES63.symbols | 42 ++++++++++++++ ...ssExpressionWithStaticPropertiesES63.types | 58 +++++++++++++++++++ .../classExpressionWithStaticProperties1.ts | 6 +- .../classExpressionWithStaticProperties2.ts | 10 +++- .../classExpressionWithStaticProperties3.ts | 11 ++++ ...classExpressionWithStaticPropertiesES61.ts | 6 +- ...classExpressionWithStaticPropertiesES62.ts | 9 ++- ...classExpressionWithStaticPropertiesES63.ts | 11 ++++ 24 files changed, 490 insertions(+), 24 deletions(-) create mode 100644 tests/baselines/reference/classExpressionWithStaticProperties3.js create mode 100644 tests/baselines/reference/classExpressionWithStaticProperties3.symbols create mode 100644 tests/baselines/reference/classExpressionWithStaticProperties3.types create mode 100644 tests/baselines/reference/classExpressionWithStaticPropertiesES63.js create mode 100644 tests/baselines/reference/classExpressionWithStaticPropertiesES63.symbols create mode 100644 tests/baselines/reference/classExpressionWithStaticPropertiesES63.types create mode 100644 tests/cases/compiler/classExpressionWithStaticProperties3.ts create mode 100644 tests/cases/compiler/classExpressionWithStaticPropertiesES63.ts diff --git a/tests/baselines/reference/classExpressionWithStaticProperties1.js b/tests/baselines/reference/classExpressionWithStaticProperties1.js index 23d18d7676627..205f59367ccba 100644 --- a/tests/baselines/reference/classExpressionWithStaticProperties1.js +++ b/tests/baselines/reference/classExpressionWithStaticProperties1.js @@ -1,5 +1,9 @@ //// [classExpressionWithStaticProperties1.ts] -var v = class C { static a = 1; static b = 2 }; +var v = class C { + static a = 1; + static b = 2; + static c = C.a + C.b; +}; //// [classExpressionWithStaticProperties1.js] var v = (_a = (function () { @@ -9,5 +13,6 @@ var v = (_a = (function () { }()), _a.a = 1, _a.b = 2, + _a.c = _a.a + _a.b, _a); var _a; diff --git a/tests/baselines/reference/classExpressionWithStaticProperties1.symbols b/tests/baselines/reference/classExpressionWithStaticProperties1.symbols index d4c811daebec3..f8b3c270ea935 100644 --- a/tests/baselines/reference/classExpressionWithStaticProperties1.symbols +++ b/tests/baselines/reference/classExpressionWithStaticProperties1.symbols @@ -1,7 +1,21 @@ === tests/cases/compiler/classExpressionWithStaticProperties1.ts === -var v = class C { static a = 1; static b = 2 }; +var v = class C { >v : Symbol(v, Decl(classExpressionWithStaticProperties1.ts, 0, 3)) +>C : Symbol(C, Decl(classExpressionWithStaticProperties1.ts, 0, 7)) + + static a = 1; +>a : Symbol(C.a, Decl(classExpressionWithStaticProperties1.ts, 0, 17)) + + static b = 2; +>b : Symbol(C.b, Decl(classExpressionWithStaticProperties1.ts, 1, 17)) + + static c = C.a + C.b; +>c : Symbol(C.c, Decl(classExpressionWithStaticProperties1.ts, 2, 17)) +>C.a : Symbol(C.a, Decl(classExpressionWithStaticProperties1.ts, 0, 17)) >C : Symbol(C, Decl(classExpressionWithStaticProperties1.ts, 0, 7)) >a : Symbol(C.a, Decl(classExpressionWithStaticProperties1.ts, 0, 17)) ->b : Symbol(C.b, Decl(classExpressionWithStaticProperties1.ts, 0, 31)) +>C.b : Symbol(C.b, Decl(classExpressionWithStaticProperties1.ts, 1, 17)) +>C : Symbol(C, Decl(classExpressionWithStaticProperties1.ts, 0, 7)) +>b : Symbol(C.b, Decl(classExpressionWithStaticProperties1.ts, 1, 17)) +}; diff --git a/tests/baselines/reference/classExpressionWithStaticProperties1.types b/tests/baselines/reference/classExpressionWithStaticProperties1.types index b014f78abf712..168bb02847421 100644 --- a/tests/baselines/reference/classExpressionWithStaticProperties1.types +++ b/tests/baselines/reference/classExpressionWithStaticProperties1.types @@ -1,10 +1,25 @@ === tests/cases/compiler/classExpressionWithStaticProperties1.ts === -var v = class C { static a = 1; static b = 2 }; +var v = class C { >v : typeof C ->class C { static a = 1; static b = 2 } : typeof C +>class C { static a = 1; static b = 2; static c = C.a + C.b;} : typeof C >C : typeof C + + static a = 1; >a : number >1 : number + + static b = 2; >b : number >2 : number + static c = C.a + C.b; +>c : number +>C.a + C.b : number +>C.a : number +>C : typeof C +>a : number +>C.b : number +>C : typeof C +>b : number + +}; diff --git a/tests/baselines/reference/classExpressionWithStaticProperties2.js b/tests/baselines/reference/classExpressionWithStaticProperties2.js index b9fc03455918f..eb1c2da2e216e 100644 --- a/tests/baselines/reference/classExpressionWithStaticProperties2.js +++ b/tests/baselines/reference/classExpressionWithStaticProperties2.js @@ -1,5 +1,12 @@ //// [classExpressionWithStaticProperties2.ts] -var v = class C { static a = 1; static b }; +var v = class C { + static a = 1; + static b + static c = { + x: "hi" + } + static d = C.c.x + " world"; + }; //// [classExpressionWithStaticProperties2.js] var v = (_a = (function () { @@ -8,5 +15,9 @@ var v = (_a = (function () { return C; }()), _a.a = 1, + _a.c = { + x: "hi" + }, + _a.d = _a.c.x + " world", _a); var _a; diff --git a/tests/baselines/reference/classExpressionWithStaticProperties2.symbols b/tests/baselines/reference/classExpressionWithStaticProperties2.symbols index 480123a1c3bfb..981e1e258c7aa 100644 --- a/tests/baselines/reference/classExpressionWithStaticProperties2.symbols +++ b/tests/baselines/reference/classExpressionWithStaticProperties2.symbols @@ -1,7 +1,26 @@ === tests/cases/compiler/classExpressionWithStaticProperties2.ts === -var v = class C { static a = 1; static b }; +var v = class C { >v : Symbol(v, Decl(classExpressionWithStaticProperties2.ts, 0, 3)) >C : Symbol(C, Decl(classExpressionWithStaticProperties2.ts, 0, 7)) + + static a = 1; >a : Symbol(C.a, Decl(classExpressionWithStaticProperties2.ts, 0, 17)) ->b : Symbol(C.b, Decl(classExpressionWithStaticProperties2.ts, 0, 31)) + static b +>b : Symbol(C.b, Decl(classExpressionWithStaticProperties2.ts, 1, 17)) + + static c = { +>c : Symbol(C.c, Decl(classExpressionWithStaticProperties2.ts, 2, 12)) + + x: "hi" +>x : Symbol(x, Decl(classExpressionWithStaticProperties2.ts, 3, 16)) + } + static d = C.c.x + " world"; +>d : Symbol(C.d, Decl(classExpressionWithStaticProperties2.ts, 5, 5)) +>C.c.x : Symbol(x, Decl(classExpressionWithStaticProperties2.ts, 3, 16)) +>C.c : Symbol(C.c, Decl(classExpressionWithStaticProperties2.ts, 2, 12)) +>C : Symbol(C, Decl(classExpressionWithStaticProperties2.ts, 0, 7)) +>c : Symbol(C.c, Decl(classExpressionWithStaticProperties2.ts, 2, 12)) +>x : Symbol(x, Decl(classExpressionWithStaticProperties2.ts, 3, 16)) + + }; diff --git a/tests/baselines/reference/classExpressionWithStaticProperties2.types b/tests/baselines/reference/classExpressionWithStaticProperties2.types index 9bfd6732350d2..221b479411116 100644 --- a/tests/baselines/reference/classExpressionWithStaticProperties2.types +++ b/tests/baselines/reference/classExpressionWithStaticProperties2.types @@ -1,9 +1,32 @@ === tests/cases/compiler/classExpressionWithStaticProperties2.ts === -var v = class C { static a = 1; static b }; +var v = class C { >v : typeof C ->class C { static a = 1; static b } : typeof C +>class C { static a = 1; static b static c = { x: "hi" } static d = C.c.x + " world"; } : typeof C >C : typeof C + + static a = 1; >a : number >1 : number + + static b >b : any + static c = { +>c : { x: string; } +>{ x: "hi" } : { x: string; } + + x: "hi" +>x : string +>"hi" : string + } + static d = C.c.x + " world"; +>d : string +>C.c.x + " world" : string +>C.c.x : string +>C.c : { x: string; } +>C : typeof C +>c : { x: string; } +>x : string +>" world" : string + + }; diff --git a/tests/baselines/reference/classExpressionWithStaticProperties3.js b/tests/baselines/reference/classExpressionWithStaticProperties3.js new file mode 100644 index 0000000000000..7bec5fca16733 --- /dev/null +++ b/tests/baselines/reference/classExpressionWithStaticProperties3.js @@ -0,0 +1,29 @@ +//// [classExpressionWithStaticProperties3.ts] + +declare var console: any; +const arr: {y(): number}[] = []; +for (let i = 0; i < 3; i++) { + arr.push(class C { + static x = i; + static y = () => C.x * 2; + }); +} +arr.forEach(C => console.log(C.y())); + +//// [classExpressionWithStaticProperties3.js] +var arr = []; +var _loop_1 = function (i) { + arr.push((_a = (function () { + function C() { + } + return C; + }()), + _a.x = i, + _a.y = function () { return _a.x * 2; }, + _a)); +}; +for (var i = 0; i < 3; i++) { + _loop_1(i); +} +arr.forEach(function (C) { return console.log(C.y()); }); +var _a; diff --git a/tests/baselines/reference/classExpressionWithStaticProperties3.symbols b/tests/baselines/reference/classExpressionWithStaticProperties3.symbols new file mode 100644 index 0000000000000..ff9b7b0d31130 --- /dev/null +++ b/tests/baselines/reference/classExpressionWithStaticProperties3.symbols @@ -0,0 +1,42 @@ +=== tests/cases/compiler/classExpressionWithStaticProperties3.ts === + +declare var console: any; +>console : Symbol(console, Decl(classExpressionWithStaticProperties3.ts, 1, 11)) + +const arr: {y(): number}[] = []; +>arr : Symbol(arr, Decl(classExpressionWithStaticProperties3.ts, 2, 5)) +>y : Symbol(y, Decl(classExpressionWithStaticProperties3.ts, 2, 12)) + +for (let i = 0; i < 3; i++) { +>i : Symbol(i, Decl(classExpressionWithStaticProperties3.ts, 3, 8)) +>i : Symbol(i, Decl(classExpressionWithStaticProperties3.ts, 3, 8)) +>i : Symbol(i, Decl(classExpressionWithStaticProperties3.ts, 3, 8)) + + arr.push(class C { +>arr.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>arr : Symbol(arr, Decl(classExpressionWithStaticProperties3.ts, 2, 5)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>C : Symbol(C, Decl(classExpressionWithStaticProperties3.ts, 4, 13)) + + static x = i; +>x : Symbol(C.x, Decl(classExpressionWithStaticProperties3.ts, 4, 22)) +>i : Symbol(i, Decl(classExpressionWithStaticProperties3.ts, 3, 8)) + + static y = () => C.x * 2; +>y : Symbol(C.y, Decl(classExpressionWithStaticProperties3.ts, 5, 21)) +>C.x : Symbol(C.x, Decl(classExpressionWithStaticProperties3.ts, 4, 22)) +>C : Symbol(C, Decl(classExpressionWithStaticProperties3.ts, 4, 13)) +>x : Symbol(C.x, Decl(classExpressionWithStaticProperties3.ts, 4, 22)) + + }); +} +arr.forEach(C => console.log(C.y())); +>arr.forEach : Symbol(Array.forEach, Decl(lib.d.ts, --, --)) +>arr : Symbol(arr, Decl(classExpressionWithStaticProperties3.ts, 2, 5)) +>forEach : Symbol(Array.forEach, Decl(lib.d.ts, --, --)) +>C : Symbol(C, Decl(classExpressionWithStaticProperties3.ts, 9, 12)) +>console : Symbol(console, Decl(classExpressionWithStaticProperties3.ts, 1, 11)) +>C.y : Symbol(y, Decl(classExpressionWithStaticProperties3.ts, 2, 12)) +>C : Symbol(C, Decl(classExpressionWithStaticProperties3.ts, 9, 12)) +>y : Symbol(y, Decl(classExpressionWithStaticProperties3.ts, 2, 12)) + diff --git a/tests/baselines/reference/classExpressionWithStaticProperties3.types b/tests/baselines/reference/classExpressionWithStaticProperties3.types new file mode 100644 index 0000000000000..c6ff6d1b56b47 --- /dev/null +++ b/tests/baselines/reference/classExpressionWithStaticProperties3.types @@ -0,0 +1,58 @@ +=== tests/cases/compiler/classExpressionWithStaticProperties3.ts === + +declare var console: any; +>console : any + +const arr: {y(): number}[] = []; +>arr : { y(): number; }[] +>y : () => number +>[] : undefined[] + +for (let i = 0; i < 3; i++) { +>i : number +>0 : number +>i < 3 : boolean +>i : number +>3 : number +>i++ : number +>i : number + + arr.push(class C { +>arr.push(class C { static x = i; static y = () => C.x * 2; }) : number +>arr.push : (...items: { y(): number; }[]) => number +>arr : { y(): number; }[] +>push : (...items: { y(): number; }[]) => number +>class C { static x = i; static y = () => C.x * 2; } : typeof C +>C : typeof C + + static x = i; +>x : number +>i : number + + static y = () => C.x * 2; +>y : () => number +>() => C.x * 2 : () => number +>C.x * 2 : number +>C.x : number +>C : typeof C +>x : number +>2 : number + + }); +} +arr.forEach(C => console.log(C.y())); +>arr.forEach(C => console.log(C.y())) : void +>arr.forEach : (callbackfn: (value: { y(): number; }, index: number, array: { y(): number; }[]) => void, thisArg?: any) => void +>arr : { y(): number; }[] +>forEach : (callbackfn: (value: { y(): number; }, index: number, array: { y(): number; }[]) => void, thisArg?: any) => void +>C => console.log(C.y()) : (C: { y(): number; }) => any +>C : { y(): number; } +>console.log(C.y()) : any +>console.log : any +>console : any +>log : any +>C.y() : number +>C.y : () => number +>C : { y(): number; } +>y : () => number + diff --git a/tests/baselines/reference/classExpressionWithStaticPropertiesES61.js b/tests/baselines/reference/classExpressionWithStaticPropertiesES61.js index 43f5e7415c4da..d3a210df7e262 100644 --- a/tests/baselines/reference/classExpressionWithStaticPropertiesES61.js +++ b/tests/baselines/reference/classExpressionWithStaticPropertiesES61.js @@ -1,10 +1,15 @@ //// [classExpressionWithStaticPropertiesES61.ts] -var v = class C { static a = 1; static b = 2 }; +var v = class C { + static a = 1; + static b = 2; + static c = C.a + 3; +}; //// [classExpressionWithStaticPropertiesES61.js] var v = (_a = class C { }, _a.a = 1, _a.b = 2, + _a.c = _a.a + 3, _a); var _a; diff --git a/tests/baselines/reference/classExpressionWithStaticPropertiesES61.symbols b/tests/baselines/reference/classExpressionWithStaticPropertiesES61.symbols index c5f53e19bffb1..1101c0bab2fc4 100644 --- a/tests/baselines/reference/classExpressionWithStaticPropertiesES61.symbols +++ b/tests/baselines/reference/classExpressionWithStaticPropertiesES61.symbols @@ -1,7 +1,18 @@ === tests/cases/compiler/classExpressionWithStaticPropertiesES61.ts === -var v = class C { static a = 1; static b = 2 }; +var v = class C { >v : Symbol(v, Decl(classExpressionWithStaticPropertiesES61.ts, 0, 3)) +>C : Symbol(C, Decl(classExpressionWithStaticPropertiesES61.ts, 0, 7)) + + static a = 1; +>a : Symbol(C.a, Decl(classExpressionWithStaticPropertiesES61.ts, 0, 17)) + + static b = 2; +>b : Symbol(C.b, Decl(classExpressionWithStaticPropertiesES61.ts, 1, 17)) + + static c = C.a + 3; +>c : Symbol(C.c, Decl(classExpressionWithStaticPropertiesES61.ts, 2, 17)) +>C.a : Symbol(C.a, Decl(classExpressionWithStaticPropertiesES61.ts, 0, 17)) >C : Symbol(C, Decl(classExpressionWithStaticPropertiesES61.ts, 0, 7)) >a : Symbol(C.a, Decl(classExpressionWithStaticPropertiesES61.ts, 0, 17)) ->b : Symbol(C.b, Decl(classExpressionWithStaticPropertiesES61.ts, 0, 31)) +}; diff --git a/tests/baselines/reference/classExpressionWithStaticPropertiesES61.types b/tests/baselines/reference/classExpressionWithStaticPropertiesES61.types index 02c385b7525df..0ba6ada4131f2 100644 --- a/tests/baselines/reference/classExpressionWithStaticPropertiesES61.types +++ b/tests/baselines/reference/classExpressionWithStaticPropertiesES61.types @@ -1,10 +1,23 @@ === tests/cases/compiler/classExpressionWithStaticPropertiesES61.ts === -var v = class C { static a = 1; static b = 2 }; +var v = class C { >v : typeof C ->class C { static a = 1; static b = 2 } : typeof C +>class C { static a = 1; static b = 2; static c = C.a + 3;} : typeof C >C : typeof C + + static a = 1; >a : number >1 : number + + static b = 2; >b : number >2 : number + static c = C.a + 3; +>c : number +>C.a + 3 : number +>C.a : number +>C : typeof C +>a : number +>3 : number + +}; diff --git a/tests/baselines/reference/classExpressionWithStaticPropertiesES62.js b/tests/baselines/reference/classExpressionWithStaticPropertiesES62.js index 1efa56ecaa23d..d0f56131d5406 100644 --- a/tests/baselines/reference/classExpressionWithStaticPropertiesES62.js +++ b/tests/baselines/reference/classExpressionWithStaticPropertiesES62.js @@ -1,9 +1,20 @@ //// [classExpressionWithStaticPropertiesES62.ts] -var v = class C { static a = 1; static b }; +var v = class C { + static a = 1; + static b + static c = { + x: "hi" + } + static d = C.c.x + " world"; + }; //// [classExpressionWithStaticPropertiesES62.js] var v = (_a = class C { }, _a.a = 1, + _a.c = { + x: "hi" + }, + _a.d = _a.c.x + " world", _a); var _a; diff --git a/tests/baselines/reference/classExpressionWithStaticPropertiesES62.symbols b/tests/baselines/reference/classExpressionWithStaticPropertiesES62.symbols index be57a289f533b..697be499595c9 100644 --- a/tests/baselines/reference/classExpressionWithStaticPropertiesES62.symbols +++ b/tests/baselines/reference/classExpressionWithStaticPropertiesES62.symbols @@ -1,7 +1,26 @@ === tests/cases/compiler/classExpressionWithStaticPropertiesES62.ts === -var v = class C { static a = 1; static b }; +var v = class C { >v : Symbol(v, Decl(classExpressionWithStaticPropertiesES62.ts, 0, 3)) >C : Symbol(C, Decl(classExpressionWithStaticPropertiesES62.ts, 0, 7)) + + static a = 1; >a : Symbol(C.a, Decl(classExpressionWithStaticPropertiesES62.ts, 0, 17)) ->b : Symbol(C.b, Decl(classExpressionWithStaticPropertiesES62.ts, 0, 31)) + static b +>b : Symbol(C.b, Decl(classExpressionWithStaticPropertiesES62.ts, 1, 17)) + + static c = { +>c : Symbol(C.c, Decl(classExpressionWithStaticPropertiesES62.ts, 2, 12)) + + x: "hi" +>x : Symbol(x, Decl(classExpressionWithStaticPropertiesES62.ts, 3, 16)) + } + static d = C.c.x + " world"; +>d : Symbol(C.d, Decl(classExpressionWithStaticPropertiesES62.ts, 5, 5)) +>C.c.x : Symbol(x, Decl(classExpressionWithStaticPropertiesES62.ts, 3, 16)) +>C.c : Symbol(C.c, Decl(classExpressionWithStaticPropertiesES62.ts, 2, 12)) +>C : Symbol(C, Decl(classExpressionWithStaticPropertiesES62.ts, 0, 7)) +>c : Symbol(C.c, Decl(classExpressionWithStaticPropertiesES62.ts, 2, 12)) +>x : Symbol(x, Decl(classExpressionWithStaticPropertiesES62.ts, 3, 16)) + + }; diff --git a/tests/baselines/reference/classExpressionWithStaticPropertiesES62.types b/tests/baselines/reference/classExpressionWithStaticPropertiesES62.types index e8ded1422f1e4..97d6940a3fc61 100644 --- a/tests/baselines/reference/classExpressionWithStaticPropertiesES62.types +++ b/tests/baselines/reference/classExpressionWithStaticPropertiesES62.types @@ -1,9 +1,32 @@ === tests/cases/compiler/classExpressionWithStaticPropertiesES62.ts === -var v = class C { static a = 1; static b }; +var v = class C { >v : typeof C ->class C { static a = 1; static b } : typeof C +>class C { static a = 1; static b static c = { x: "hi" } static d = C.c.x + " world"; } : typeof C >C : typeof C + + static a = 1; >a : number >1 : number + + static b >b : any + static c = { +>c : { x: string; } +>{ x: "hi" } : { x: string; } + + x: "hi" +>x : string +>"hi" : string + } + static d = C.c.x + " world"; +>d : string +>C.c.x + " world" : string +>C.c.x : string +>C.c : { x: string; } +>C : typeof C +>c : { x: string; } +>x : string +>" world" : string + + }; diff --git a/tests/baselines/reference/classExpressionWithStaticPropertiesES63.js b/tests/baselines/reference/classExpressionWithStaticPropertiesES63.js new file mode 100644 index 0000000000000..3993f18c0c741 --- /dev/null +++ b/tests/baselines/reference/classExpressionWithStaticPropertiesES63.js @@ -0,0 +1,23 @@ +//// [classExpressionWithStaticPropertiesES63.ts] + +declare var console: any; +const arr: {y(): number}[] = []; +for (let i = 0; i < 3; i++) { + arr.push(class C { + static x = i; + static y = () => C.x * 2; + }); +} +arr.forEach(C => console.log(C.y())); + +//// [classExpressionWithStaticPropertiesES63.js] +const arr = []; +for (let i = 0; i < 3; i++) { + arr.push((_a = class C { + }, + _a.x = i, + _a.y = () => _a.x * 2, + _a)); +} +arr.forEach(C => console.log(C.y())); +var _a; diff --git a/tests/baselines/reference/classExpressionWithStaticPropertiesES63.symbols b/tests/baselines/reference/classExpressionWithStaticPropertiesES63.symbols new file mode 100644 index 0000000000000..f1a7fa807f56b --- /dev/null +++ b/tests/baselines/reference/classExpressionWithStaticPropertiesES63.symbols @@ -0,0 +1,42 @@ +=== tests/cases/compiler/classExpressionWithStaticPropertiesES63.ts === + +declare var console: any; +>console : Symbol(console, Decl(classExpressionWithStaticPropertiesES63.ts, 1, 11)) + +const arr: {y(): number}[] = []; +>arr : Symbol(arr, Decl(classExpressionWithStaticPropertiesES63.ts, 2, 5)) +>y : Symbol(y, Decl(classExpressionWithStaticPropertiesES63.ts, 2, 12)) + +for (let i = 0; i < 3; i++) { +>i : Symbol(i, Decl(classExpressionWithStaticPropertiesES63.ts, 3, 8)) +>i : Symbol(i, Decl(classExpressionWithStaticPropertiesES63.ts, 3, 8)) +>i : Symbol(i, Decl(classExpressionWithStaticPropertiesES63.ts, 3, 8)) + + arr.push(class C { +>arr.push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --)) +>arr : Symbol(arr, Decl(classExpressionWithStaticPropertiesES63.ts, 2, 5)) +>push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --)) +>C : Symbol(C, Decl(classExpressionWithStaticPropertiesES63.ts, 4, 13)) + + static x = i; +>x : Symbol(C.x, Decl(classExpressionWithStaticPropertiesES63.ts, 4, 22)) +>i : Symbol(i, Decl(classExpressionWithStaticPropertiesES63.ts, 3, 8)) + + static y = () => C.x * 2; +>y : Symbol(C.y, Decl(classExpressionWithStaticPropertiesES63.ts, 5, 21)) +>C.x : Symbol(C.x, Decl(classExpressionWithStaticPropertiesES63.ts, 4, 22)) +>C : Symbol(C, Decl(classExpressionWithStaticPropertiesES63.ts, 4, 13)) +>x : Symbol(C.x, Decl(classExpressionWithStaticPropertiesES63.ts, 4, 22)) + + }); +} +arr.forEach(C => console.log(C.y())); +>arr.forEach : Symbol(Array.forEach, Decl(lib.es5.d.ts, --, --)) +>arr : Symbol(arr, Decl(classExpressionWithStaticPropertiesES63.ts, 2, 5)) +>forEach : Symbol(Array.forEach, Decl(lib.es5.d.ts, --, --)) +>C : Symbol(C, Decl(classExpressionWithStaticPropertiesES63.ts, 9, 12)) +>console : Symbol(console, Decl(classExpressionWithStaticPropertiesES63.ts, 1, 11)) +>C.y : Symbol(y, Decl(classExpressionWithStaticPropertiesES63.ts, 2, 12)) +>C : Symbol(C, Decl(classExpressionWithStaticPropertiesES63.ts, 9, 12)) +>y : Symbol(y, Decl(classExpressionWithStaticPropertiesES63.ts, 2, 12)) + diff --git a/tests/baselines/reference/classExpressionWithStaticPropertiesES63.types b/tests/baselines/reference/classExpressionWithStaticPropertiesES63.types new file mode 100644 index 0000000000000..92f14f3f65f50 --- /dev/null +++ b/tests/baselines/reference/classExpressionWithStaticPropertiesES63.types @@ -0,0 +1,58 @@ +=== tests/cases/compiler/classExpressionWithStaticPropertiesES63.ts === + +declare var console: any; +>console : any + +const arr: {y(): number}[] = []; +>arr : { y(): number; }[] +>y : () => number +>[] : undefined[] + +for (let i = 0; i < 3; i++) { +>i : number +>0 : number +>i < 3 : boolean +>i : number +>3 : number +>i++ : number +>i : number + + arr.push(class C { +>arr.push(class C { static x = i; static y = () => C.x * 2; }) : number +>arr.push : (...items: { y(): number; }[]) => number +>arr : { y(): number; }[] +>push : (...items: { y(): number; }[]) => number +>class C { static x = i; static y = () => C.x * 2; } : typeof C +>C : typeof C + + static x = i; +>x : number +>i : number + + static y = () => C.x * 2; +>y : () => number +>() => C.x * 2 : () => number +>C.x * 2 : number +>C.x : number +>C : typeof C +>x : number +>2 : number + + }); +} +arr.forEach(C => console.log(C.y())); +>arr.forEach(C => console.log(C.y())) : void +>arr.forEach : (callbackfn: (value: { y(): number; }, index: number, array: { y(): number; }[]) => void, thisArg?: any) => void +>arr : { y(): number; }[] +>forEach : (callbackfn: (value: { y(): number; }, index: number, array: { y(): number; }[]) => void, thisArg?: any) => void +>C => console.log(C.y()) : (C: { y(): number; }) => any +>C : { y(): number; } +>console.log(C.y()) : any +>console.log : any +>console : any +>log : any +>C.y() : number +>C.y : () => number +>C : { y(): number; } +>y : () => number + diff --git a/tests/cases/compiler/classExpressionWithStaticProperties1.ts b/tests/cases/compiler/classExpressionWithStaticProperties1.ts index 19fe035e6a70e..79c669c32a996 100644 --- a/tests/cases/compiler/classExpressionWithStaticProperties1.ts +++ b/tests/cases/compiler/classExpressionWithStaticProperties1.ts @@ -1 +1,5 @@ -var v = class C { static a = 1; static b = 2 }; \ No newline at end of file +var v = class C { + static a = 1; + static b = 2; + static c = C.a + C.b; +}; \ No newline at end of file diff --git a/tests/cases/compiler/classExpressionWithStaticProperties2.ts b/tests/cases/compiler/classExpressionWithStaticProperties2.ts index f7b9606b6e0b7..353926e7abdbd 100644 --- a/tests/cases/compiler/classExpressionWithStaticProperties2.ts +++ b/tests/cases/compiler/classExpressionWithStaticProperties2.ts @@ -1 +1,9 @@ -var v = class C { static a = 1; static b }; \ No newline at end of file +//@target: es5 +var v = class C { + static a = 1; + static b + static c = { + x: "hi" + } + static d = C.c.x + " world"; + }; \ No newline at end of file diff --git a/tests/cases/compiler/classExpressionWithStaticProperties3.ts b/tests/cases/compiler/classExpressionWithStaticProperties3.ts new file mode 100644 index 0000000000000..7c733c458be84 --- /dev/null +++ b/tests/cases/compiler/classExpressionWithStaticProperties3.ts @@ -0,0 +1,11 @@ +//@target: es5 + +declare var console: any; +const arr: {y(): number}[] = []; +for (let i = 0; i < 3; i++) { + arr.push(class C { + static x = i; + static y = () => C.x * 2; + }); +} +arr.forEach(C => console.log(C.y())); \ No newline at end of file diff --git a/tests/cases/compiler/classExpressionWithStaticPropertiesES61.ts b/tests/cases/compiler/classExpressionWithStaticPropertiesES61.ts index 8df07b9569a03..3f9fc43835e1e 100644 --- a/tests/cases/compiler/classExpressionWithStaticPropertiesES61.ts +++ b/tests/cases/compiler/classExpressionWithStaticPropertiesES61.ts @@ -1,2 +1,6 @@ //@target: es6 -var v = class C { static a = 1; static b = 2 }; \ No newline at end of file +var v = class C { + static a = 1; + static b = 2; + static c = C.a + 3; +}; \ No newline at end of file diff --git a/tests/cases/compiler/classExpressionWithStaticPropertiesES62.ts b/tests/cases/compiler/classExpressionWithStaticPropertiesES62.ts index ee0430eb793ab..afb87b10de96a 100644 --- a/tests/cases/compiler/classExpressionWithStaticPropertiesES62.ts +++ b/tests/cases/compiler/classExpressionWithStaticPropertiesES62.ts @@ -1,2 +1,9 @@ //@target: es6 -var v = class C { static a = 1; static b }; \ No newline at end of file +var v = class C { + static a = 1; + static b + static c = { + x: "hi" + } + static d = C.c.x + " world"; + }; \ No newline at end of file diff --git a/tests/cases/compiler/classExpressionWithStaticPropertiesES63.ts b/tests/cases/compiler/classExpressionWithStaticPropertiesES63.ts new file mode 100644 index 0000000000000..939a344f9bb05 --- /dev/null +++ b/tests/cases/compiler/classExpressionWithStaticPropertiesES63.ts @@ -0,0 +1,11 @@ +//@target: es6 + +declare var console: any; +const arr: {y(): number}[] = []; +for (let i = 0; i < 3; i++) { + arr.push(class C { + static x = i; + static y = () => C.x * 2; + }); +} +arr.forEach(C => console.log(C.y())); \ No newline at end of file From 02e1fa3402acde88c9dd37020d13ccf831a1a8e9 Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Fri, 13 May 2016 16:44:07 -0700 Subject: [PATCH 2/3] Fix incorrect emit of referring to container classExpression inside its own static propertyDeclaration Wip: attempt to fix down-level emit of class-expression with static property Fix incorrect emit of referring to container classExpression inside its own static propertyDeclaration --- src/compiler/transformers/ts.ts | 82 +++++++++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 3 deletions(-) diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index fe0c49e08367a..0254b06355a07 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -18,8 +18,10 @@ namespace ts { NamespaceExports = 1 << 1, /** Enables substitutions for async methods with `super` calls. */ AsyncMethodsWithSuper = 1 << 2, - /* Enables substitutions for unqualified enum members */ - NonQualifiedEnumMembers = 1 << 3 + /** Enables substitutions for unqualified enum members */ + NonQualifiedEnumMembers = 1 << 3, + /** Enables substitutions for classExpression referenced in its static property initializer */ + ClassExpressionReferredInStaticPropertyInitializer = 1 << 4 } export function transformTypeScript(context: TransformationContext) { @@ -68,6 +70,29 @@ namespace ts { */ let currentDecoratedClassAliases: Map; + /** + * A map that keeps track between generated name of classExpression and original source-node classExpression that contains static propertyDeclaration. + * During transformation phase, when we visit classExpression that contains static propertyDeclaration, we will record the newly + * created name and its original source-node classExpression. + * During substitution, the map is used to determine what is the active generated name of classExpression given classExpression. + */ + let generatedNameOfClassExpression: Map; + + /** + * A map that keep track between static propertyDeclaration and its containing classExpression. + * During transformation phase, when we visit each static propertyDeclaration, we will record propertyDeclaration's + * containing classExpression. + * During substitution, the map is used to determine what is the source-node classExpression that contains currently + * visiting propertyDeclaration. + */ + let classExpressionContainStaticPropertyDeclaration: Map; + + /** + * A map that keeps track of currently active generated name of classExpression defined in `generatedNameOfClassExpression` + * when just-in-time substitution occurs while printing an expression identifier. + */ + let currentGeneratedNameOfClassExpression: Map; + /** * Keeps track of whether we are within any containing namespaces when performing * just-in-time substitution while printing an expression identifier. @@ -695,6 +720,11 @@ namespace ts { // the body of a class with static initializers. setNodeEmitFlags(classExpression, NodeEmitFlags.Indented | getNodeEmitFlags(classExpression)); addNode(expressions, createAssignment(temp, classExpression), true); + + // Enable substitution when classExpression is referred inside its static propertyDeclaration + enableSubstitutionForClassExpressionReferredInStaticPropertyInitializer(); + generatedNameOfClassExpression[getOriginalNodeId(node)] = temp; + addNodes(expressions, generateInitializedPropertyExpressions(node, staticProperties, temp), true); addNode(expressions, temp, true); return inlineExpressions(expressions); @@ -1004,11 +1034,17 @@ namespace ts { function transformInitializedProperty(node: ClassExpression | ClassDeclaration, property: PropertyDeclaration, receiver: LeftHandSideExpression, location?: TextRange) { const propertyName = visitPropertyNameOfClassElement(property); const initializer = visitNode(property.initializer, visitor, isExpression); - return createAssignment( + const assignment = createAssignment( createMemberAccessForPropertyName(receiver, propertyName, /*location*/ propertyName), initializer, location ); + if (node.kind === SyntaxKind.ClassExpression && hasModifier(property, ModifierFlags.Static)) { + setOriginalNode(assignment, property); + setNodeEmitFlags(assignment, NodeEmitFlags.AdviseOnEmitNode); + classExpressionContainStaticPropertyDeclaration[getOriginalNodeId(property)] = node; + } + return assignment; } /** @@ -2906,6 +2942,17 @@ namespace ts { } } + function enableSubstitutionForClassExpressionReferredInStaticPropertyInitializer() { + if ((enabledSubstitutions & TypeScriptSubstitutionFlags.ClassExpressionReferredInStaticPropertyInitializer) === 0) { + enabledSubstitutions |= TypeScriptSubstitutionFlags.ClassExpressionReferredInStaticPropertyInitializer; + context.enableSubstitution(SyntaxKind.Identifier); + + generatedNameOfClassExpression = {}; + classExpressionContainStaticPropertyDeclaration = {}; + currentGeneratedNameOfClassExpression = {}; + } + } + function isClassWithDecorators(node: Node): node is ClassDeclaration { return node.kind === SyntaxKind.ClassDeclaration && node.decorators !== undefined; } @@ -2964,12 +3011,29 @@ namespace ts { applicableSubstitutions |= TypeScriptSubstitutionFlags.NonQualifiedEnumMembers; } + let needClassExpressionSubstituation = false; + let classExpressionId: number; + if (enabledSubstitutions & TypeScriptSubstitutionFlags.ClassExpressionReferredInStaticPropertyInitializer && + isBinaryExpression(node) && node.operatorToken.kind === SyntaxKind.EqualsToken) { + const original = getOriginalNode(node); + if (original.kind === SyntaxKind.PropertyDeclaration) { + needClassExpressionSubstituation = true; + const classExp = classExpressionContainStaticPropertyDeclaration[original.id]; + classExpressionId = getOriginalNodeId(classExp); + currentGeneratedNameOfClassExpression[classExpressionId] = generatedNameOfClassExpression[classExpressionId]; + } + } + previousOnEmitNode(node, emit); if (enabledSubstitutions & TypeScriptSubstitutionFlags.DecoratedClasses && isClassWithDecorators(node)) { currentDecoratedClassAliases[getOriginalNodeId(node)] = undefined; } + if (needClassExpressionSubstituation) { + currentGeneratedNameOfClassExpression[classExpressionId] = undefined; + } + applicableSubstitutions = savedApplicableSubstitutions; currentSuperContainer = savedCurrentSuperContainer; } @@ -3033,6 +3097,7 @@ namespace ts { function substituteExpressionIdentifier(node: Identifier): Expression { return trySubstituteDecoratedClassName(node) || trySubstituteNamespaceExportedName(node) + || trySubstituteClassExpressionNameInStaticPropertyDeclaration(node) || node; } @@ -3074,6 +3139,17 @@ namespace ts { return undefined; } + function trySubstituteClassExpressionNameInStaticPropertyDeclaration(node: Identifier): Expression { + if (!nodeIsSynthesized(node) && (enabledSubstitutions & TypeScriptSubstitutionFlags.ClassExpressionReferredInStaticPropertyInitializer)) { + const referenced = resolver.getReferencedValueDeclaration(node); + const substitute = currentGeneratedNameOfClassExpression[getOriginalNodeId(referenced)]; + if (substitute) { + return getRelocatedClone(substitute, node); + } + } + return undefined; + } + function substituteCallExpression(node: CallExpression): Expression { const expression = node.expression; if (isSuperProperty(expression)) { From be4268c222173ac714b69bc9f9a0092065f07ba1 Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Fri, 20 May 2016 13:07:45 -0700 Subject: [PATCH 3/3] Fix error from merging --- src/compiler/transformers/ts.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 85c64502c8ca2..b6486eeef8573 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -1069,7 +1069,14 @@ namespace ts { setNodeEmitFlags(memberAccess, NodeEmitFlags.NoNestedSourceMaps); } - return createAssignment(memberAccess, initializer); + const assignment = createAssignment(memberAccess, initializer); + if (node.kind === SyntaxKind.ClassExpression && hasModifier(property, ModifierFlags.Static)) { + setOriginalNode(assignment, property); + setNodeEmitFlags(assignment, NodeEmitFlags.AdviseOnEmitNode); + classExpressionContainStaticPropertyDeclaration[getOriginalNodeId(property)] = node; + } + + return assignment; } /**