Skip to content

Commit cf947c5

Browse files
Max HeiberJoseph Watts
authored andcommitted
Tests for computed field scope fix
Signed-off-by: Max Heiber <mheiber@bloomberg.net>
1 parent 29d667d commit cf947c5

19 files changed

+335
-11
lines changed

src/testRunner/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
"unittests/versionCache.ts",
8989
"unittests/evaluation/asyncArrow.ts",
9090
"unittests/evaluation/asyncGenerator.ts",
91+
"unittests/evaluation/computedFieldLoop.ts",
9192
"unittests/evaluation/forAwaitOf.ts",
9293
"unittests/services/colorization.ts",
9394
"unittests/services/documentRegistry.ts",
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
describe("computed scope is correct for a computed field defined in a loop", () => {
2+
it("return (es5)", async () => {
3+
const result = evaluator.evaluateTypeScript(`
4+
const classes = [];
5+
for (let i = 0; i <= 10; ++i) {
6+
classes.push(
7+
class A {
8+
[i] = "my property";
9+
}
10+
);
11+
}
12+
export const output = classes.map(Object.keys)
13+
`);
14+
15+
result.main();
16+
assert.deepEqual(result.output, [
17+
['1'],
18+
['2'],
19+
['3'],
20+
['4'],
21+
['5'],
22+
['6'],
23+
['7'],
24+
['8'],
25+
['9'],
26+
['10'],
27+
['bananananananan']
28+
]);
29+
});
30+
});

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2757,11 +2757,15 @@ declare namespace ts {
27572757
MappedTypeParameter = 3,
27582758
Unspecified = 4
27592759
}
2760+
enum LexicalEnvironmentScoping {
2761+
Function = 0,
2762+
Block = 1
2763+
}
27602764
interface TransformationContext {
27612765
/** Gets the compiler options supplied to the transformer. */
27622766
getCompilerOptions(): CompilerOptions;
27632767
/** Starts a new lexical environment. */
2764-
startLexicalEnvironment(): void;
2768+
startLexicalEnvironment(scoping?: LexicalEnvironmentScoping): void;
27652769
/** Suspends the current lexical environment, usually after visiting a parameter list. */
27662770
suspendLexicalEnvironment(): void;
27672771
/** Resumes a suspended lexical environment, usually before visiting a function body. */

tests/baselines/reference/api/typescript.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2757,11 +2757,15 @@ declare namespace ts {
27572757
MappedTypeParameter = 3,
27582758
Unspecified = 4
27592759
}
2760+
enum LexicalEnvironmentScoping {
2761+
Function = 0,
2762+
Block = 1
2763+
}
27602764
interface TransformationContext {
27612765
/** Gets the compiler options supplied to the transformer. */
27622766
getCompilerOptions(): CompilerOptions;
27632767
/** Starts a new lexical environment. */
2764-
startLexicalEnvironment(): void;
2768+
startLexicalEnvironment(scoping?: LexicalEnvironmentScoping): void;
27652769
/** Suspends the current lexical environment, usually after visiting a parameter list. */
27662770
suspendLexicalEnvironment(): void;
27672771
/** Resumes a suspended lexical environment, usually before visiting a function body. */

tests/baselines/reference/classBlockScoping.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ function f(b: boolean) {
3535

3636
//// [classBlockScoping.js]
3737
function f(b) {
38-
var _a;
3938
var Foo;
4039
if (b) {
40+
var _a = void 0;
4141
Foo = (_a = /** @class */ (function () {
4242
function Foo() {
4343
}

tests/baselines/reference/classExpressionWithStaticProperties3.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ for (let i = 0; i < 3; i++) {
1010
arr.forEach(C => console.log(C.y()));
1111

1212
//// [classExpressionWithStaticProperties3.js]
13-
var _a;
1413
var arr = [];
1514
var _loop_1 = function (i) {
15+
var _a = void 0;
1616
arr.push((_a = /** @class */ (function () {
1717
function C() {
1818
}

tests/baselines/reference/classExpressionWithStaticPropertiesES63.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ for (let i = 0; i < 3; i++) {
1010
arr.forEach(C => console.log(C.y()));
1111

1212
//// [classExpressionWithStaticPropertiesES63.js]
13-
var _a;
1413
const arr = [];
1514
for (let i = 0; i < 3; i++) {
15+
let _a;
1616
arr.push((_a = class C {
1717
},
1818
_a.x = i,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/conformance/es6/computedProperties/computedPropertyNames52_ES5.ts(5,13): error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
2+
3+
4+
==== tests/cases/conformance/es6/computedProperties/computedPropertyNames52_ES5.ts (1 errors) ====
5+
const classes = [];
6+
for (let i = 0; i <= 10; ++i) {
7+
classes.push(
8+
class A {
9+
[i] = "my property";
10+
~~~
11+
!!! error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.
12+
}
13+
);
14+
}
15+
for (const clazz of classes) {
16+
console.log(Object.getOwnPropertyNames(new clazz()));
17+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//// [computedPropertyNames52_ES5.ts]
2+
const classes = [];
3+
for (let i = 0; i <= 10; ++i) {
4+
classes.push(
5+
class A {
6+
[i] = "my property";
7+
}
8+
);
9+
}
10+
for (const clazz of classes) {
11+
console.log(Object.getOwnPropertyNames(new clazz()));
12+
}
13+
14+
//// [computedPropertyNames52_ES5.js]
15+
var classes = [];
16+
var _loop_1 = function (i) {
17+
var _a = void 0, _b = void 0;
18+
classes.push((_b = /** @class */ (function () {
19+
function A() {
20+
this[_a] = "my property";
21+
}
22+
return A;
23+
}()),
24+
_a = i,
25+
_b));
26+
};
27+
for (var i = 0; i <= 10; ++i) {
28+
_loop_1(i);
29+
}
30+
for (var _i = 0, classes_1 = classes; _i < classes_1.length; _i++) {
31+
var clazz = classes_1[_i];
32+
console.log(Object.getOwnPropertyNames(new clazz()));
33+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
=== tests/cases/conformance/es6/computedProperties/computedPropertyNames52_ES5.ts ===
2+
const classes = [];
3+
>classes : Symbol(classes, Decl(computedPropertyNames52_ES5.ts, 0, 5))
4+
5+
for (let i = 0; i <= 10; ++i) {
6+
>i : Symbol(i, Decl(computedPropertyNames52_ES5.ts, 1, 8))
7+
>i : Symbol(i, Decl(computedPropertyNames52_ES5.ts, 1, 8))
8+
>i : Symbol(i, Decl(computedPropertyNames52_ES5.ts, 1, 8))
9+
10+
classes.push(
11+
>classes.push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --))
12+
>classes : Symbol(classes, Decl(computedPropertyNames52_ES5.ts, 0, 5))
13+
>push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --))
14+
15+
class A {
16+
>A : Symbol(A, Decl(computedPropertyNames52_ES5.ts, 2, 17))
17+
18+
[i] = "my property";
19+
>[i] : Symbol(A[i], Decl(computedPropertyNames52_ES5.ts, 3, 17))
20+
>i : Symbol(i, Decl(computedPropertyNames52_ES5.ts, 1, 8))
21+
}
22+
);
23+
}
24+
for (const clazz of classes) {
25+
>clazz : Symbol(clazz, Decl(computedPropertyNames52_ES5.ts, 8, 10))
26+
>classes : Symbol(classes, Decl(computedPropertyNames52_ES5.ts, 0, 5))
27+
28+
console.log(Object.getOwnPropertyNames(new clazz()));
29+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
30+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
31+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
32+
>Object.getOwnPropertyNames : Symbol(ObjectConstructor.getOwnPropertyNames, Decl(lib.es5.d.ts, --, --))
33+
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
34+
>getOwnPropertyNames : Symbol(ObjectConstructor.getOwnPropertyNames, Decl(lib.es5.d.ts, --, --))
35+
>clazz : Symbol(clazz, Decl(computedPropertyNames52_ES5.ts, 8, 10))
36+
}

0 commit comments

Comments
 (0)