From 8a334ac00d82243b6371db24ee612eef59702e3e Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Mon, 21 Nov 2016 23:13:22 -0800 Subject: [PATCH 1/4] Add special handeling for function and array in Object.freeze --- src/lib/es5.d.ts | 12 ++++++++++++ .../reference/objectFreeze.errors.txt | 15 +++++++++++++++ tests/baselines/reference/objectFreeze.js | 18 ++++++++++++++++++ tests/cases/compiler/objectFreeze.ts | 6 ++++++ 4 files changed, 51 insertions(+) create mode 100644 tests/baselines/reference/objectFreeze.errors.txt create mode 100644 tests/baselines/reference/objectFreeze.js create mode 100644 tests/cases/compiler/objectFreeze.ts diff --git a/src/lib/es5.d.ts b/src/lib/es5.d.ts index 7ee478178021d..63b60f534af3b 100644 --- a/src/lib/es5.d.ts +++ b/src/lib/es5.d.ts @@ -176,6 +176,18 @@ interface ObjectConstructor { */ seal(o: T): T; + /** + * Prevents the modification of existing property attributes and values, and prevents the addition of new properties. + * @param o Object on which to lock the attributes. + */ + freeze(a: T[]): ReadonlyArray; + + /** + * Prevents the modification of existing property attributes and values, and prevents the addition of new properties. + * @param o Object on which to lock the attributes. + */ + freeze(f: (...args: T[]) => U): (...args: T[]) => U; + /** * Prevents the modification of existing property attributes and values, and prevents the addition of new properties. * @param o Object on which to lock the attributes. diff --git a/tests/baselines/reference/objectFreeze.errors.txt b/tests/baselines/reference/objectFreeze.errors.txt new file mode 100644 index 0000000000000..e820520ed8a4e --- /dev/null +++ b/tests/baselines/reference/objectFreeze.errors.txt @@ -0,0 +1,15 @@ +tests/cases/compiler/objectFreeze.ts(5,24): error TS2345: Argument of type '123' is not assignable to parameter of type 'string'. +tests/cases/compiler/objectFreeze.ts(6,2): error TS1128: Declaration or statement expected. + + +==== tests/cases/compiler/objectFreeze.ts (2 errors) ==== + class A { + constructor(public a1: string) { + } + } + function foo(x = new A(123)) { //should error, 123 is not string + ~~~ +!!! error TS2345: Argument of type '123' is not assignable to parameter of type 'string'. + }} + ~ +!!! error TS1128: Declaration or statement expected. \ No newline at end of file diff --git a/tests/baselines/reference/objectFreeze.js b/tests/baselines/reference/objectFreeze.js new file mode 100644 index 0000000000000..86c2833dabf77 --- /dev/null +++ b/tests/baselines/reference/objectFreeze.js @@ -0,0 +1,18 @@ +//// [objectFreeze.ts] +class A { + constructor(public a1: string) { + } +} +function foo(x = new A(123)) { //should error, 123 is not string +}} + +//// [objectFreeze.js] +var A = (function () { + function A(a1) { + this.a1 = a1; + } + return A; +}()); +function foo(x) { + if (x === void 0) { x = new A(123); } +} diff --git a/tests/cases/compiler/objectFreeze.ts b/tests/cases/compiler/objectFreeze.ts new file mode 100644 index 0000000000000..e92f9ecd00e4b --- /dev/null +++ b/tests/cases/compiler/objectFreeze.ts @@ -0,0 +1,6 @@ +class A { + constructor(public a1: string) { + } +} +function foo(x = new A(123)) { //should error, 123 is not string +}} \ No newline at end of file From 72df02cbbd70061574988eef38f9cb775b5bfa61 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Mon, 21 Nov 2016 23:27:18 -0800 Subject: [PATCH 2/4] Add function --- src/lib/es5.d.ts | 2 +- .../reference/objectFreeze.errors.txt | 27 +++++++++-------- tests/baselines/reference/objectFreeze.js | 30 +++++++++---------- tests/cases/compiler/objectFreeze.ts | 14 +++++---- 4 files changed, 39 insertions(+), 34 deletions(-) diff --git a/src/lib/es5.d.ts b/src/lib/es5.d.ts index 63b60f534af3b..3ad6046c85e71 100644 --- a/src/lib/es5.d.ts +++ b/src/lib/es5.d.ts @@ -186,7 +186,7 @@ interface ObjectConstructor { * Prevents the modification of existing property attributes and values, and prevents the addition of new properties. * @param o Object on which to lock the attributes. */ - freeze(f: (...args: T[]) => U): (...args: T[]) => U; + freeze(f: (...args: any[]) => T): (...args: any[]) => T; /** * Prevents the modification of existing property attributes and values, and prevents the addition of new properties. diff --git a/tests/baselines/reference/objectFreeze.errors.txt b/tests/baselines/reference/objectFreeze.errors.txt index e820520ed8a4e..0a1a05188296b 100644 --- a/tests/baselines/reference/objectFreeze.errors.txt +++ b/tests/baselines/reference/objectFreeze.errors.txt @@ -1,15 +1,18 @@ -tests/cases/compiler/objectFreeze.ts(5,24): error TS2345: Argument of type '123' is not assignable to parameter of type 'string'. -tests/cases/compiler/objectFreeze.ts(6,2): error TS1128: Declaration or statement expected. +tests/cases/compiler/objectFreeze.ts(5,1): error TS2542: Index signature in type 'ReadonlyArray' only permits reading. +tests/cases/compiler/objectFreeze.ts(8,3): error TS2540: Cannot assign to 'b' because it is a constant or a read-only property. ==== tests/cases/compiler/objectFreeze.ts (2 errors) ==== - class A { - constructor(public a1: string) { - } - } - function foo(x = new A(123)) { //should error, 123 is not string - ~~~ -!!! error TS2345: Argument of type '123' is not assignable to parameter of type 'string'. - }} - ~ -!!! error TS1128: Declaration or statement expected. \ No newline at end of file + const f = Object.freeze(function foo(a: number, b: string) { return false; }); + f(1, "") === false; + + const a = Object.freeze([1, 2, 3]); + a[0] = 1; + ~~~~ +!!! error TS2542: Index signature in type 'ReadonlyArray' only permits reading. + + const o = Object.freeze({ a: 1, b: "string" }); + o.b = "another"; + ~ +!!! error TS2540: Cannot assign to 'b' because it is a constant or a read-only property. + \ No newline at end of file diff --git a/tests/baselines/reference/objectFreeze.js b/tests/baselines/reference/objectFreeze.js index 86c2833dabf77..51308c8e07cc1 100644 --- a/tests/baselines/reference/objectFreeze.js +++ b/tests/baselines/reference/objectFreeze.js @@ -1,18 +1,18 @@ //// [objectFreeze.ts] -class A { - constructor(public a1: string) { - } -} -function foo(x = new A(123)) { //should error, 123 is not string -}} +const f = Object.freeze(function foo(a: number, b: string) { return false; }); +f(1, "") === false; + +const a = Object.freeze([1, 2, 3]); +a[0] = 1; + +const o = Object.freeze({ a: 1, b: "string" }); +o.b = "another"; + //// [objectFreeze.js] -var A = (function () { - function A(a1) { - this.a1 = a1; - } - return A; -}()); -function foo(x) { - if (x === void 0) { x = new A(123); } -} +var f = Object.freeze(function foo(a, b) { return false; }); +f(1, "") === false; +var a = Object.freeze([1, 2, 3]); +a[0] = 1; +var o = Object.freeze({ a: 1, b: "string" }); +o.b = "another"; diff --git a/tests/cases/compiler/objectFreeze.ts b/tests/cases/compiler/objectFreeze.ts index e92f9ecd00e4b..2c93fc8f0757f 100644 --- a/tests/cases/compiler/objectFreeze.ts +++ b/tests/cases/compiler/objectFreeze.ts @@ -1,6 +1,8 @@ -class A { - constructor(public a1: string) { - } -} -function foo(x = new A(123)) { //should error, 123 is not string -}} \ No newline at end of file +const f = Object.freeze(function foo(a: number, b: string) { return false; }); +f(1, "") === false; + +const a = Object.freeze([1, 2, 3]); +a[0] = 1; + +const o = Object.freeze({ a: 1, b: "string" }); +o.b = "another"; From 60395565e6fb9767aa6a73a0086238793bb7df58 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Wed, 23 Nov 2016 09:48:25 -0800 Subject: [PATCH 3/4] Handel call and construct signatures --- src/lib/es5.d.ts | 12 ++++++++++++ .../reference/objectFreeze.errors.txt | 12 ++++++++---- tests/baselines/reference/objectFreeze.js | 19 +++++++++++++++---- tests/cases/compiler/objectFreeze.ts | 8 ++++++-- 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/lib/es5.d.ts b/src/lib/es5.d.ts index 3ad6046c85e71..097089e371ff2 100644 --- a/src/lib/es5.d.ts +++ b/src/lib/es5.d.ts @@ -182,6 +182,18 @@ interface ObjectConstructor { */ freeze(a: T[]): ReadonlyArray; + /** + * Prevents the modification of existing property attributes and values, and prevents the addition of new properties. + * @param o Object on which to lock the attributes. + */ + freeze any>(f: T): T; + + /** + * Prevents the modification of existing property attributes and values, and prevents the addition of new properties. + * @param o Object on which to lock the attributes. + */ + freeze any>(c: T): T; + /** * Prevents the modification of existing property attributes and values, and prevents the addition of new properties. * @param o Object on which to lock the attributes. diff --git a/tests/baselines/reference/objectFreeze.errors.txt b/tests/baselines/reference/objectFreeze.errors.txt index 0a1a05188296b..9c28c62f5346b 100644 --- a/tests/baselines/reference/objectFreeze.errors.txt +++ b/tests/baselines/reference/objectFreeze.errors.txt @@ -1,18 +1,22 @@ -tests/cases/compiler/objectFreeze.ts(5,1): error TS2542: Index signature in type 'ReadonlyArray' only permits reading. -tests/cases/compiler/objectFreeze.ts(8,3): error TS2540: Cannot assign to 'b' because it is a constant or a read-only property. +tests/cases/compiler/objectFreeze.ts(9,1): error TS2542: Index signature in type 'ReadonlyArray' only permits reading. +tests/cases/compiler/objectFreeze.ts(12,3): error TS2540: Cannot assign to 'b' because it is a constant or a read-only property. ==== tests/cases/compiler/objectFreeze.ts (2 errors) ==== const f = Object.freeze(function foo(a: number, b: string) { return false; }); f(1, "") === false; + class C { constructor(a: number) { } } + const c = Object.freeze(C); + new c(1); + const a = Object.freeze([1, 2, 3]); - a[0] = 1; + a[0] = a[2].toString(); ~~~~ !!! error TS2542: Index signature in type 'ReadonlyArray' only permits reading. const o = Object.freeze({ a: 1, b: "string" }); - o.b = "another"; + o.b = o.a.toString(); ~ !!! error TS2540: Cannot assign to 'b' because it is a constant or a read-only property. \ No newline at end of file diff --git a/tests/baselines/reference/objectFreeze.js b/tests/baselines/reference/objectFreeze.js index 51308c8e07cc1..4c37631bb1057 100644 --- a/tests/baselines/reference/objectFreeze.js +++ b/tests/baselines/reference/objectFreeze.js @@ -2,17 +2,28 @@ const f = Object.freeze(function foo(a: number, b: string) { return false; }); f(1, "") === false; +class C { constructor(a: number) { } } +const c = Object.freeze(C); +new c(1); + const a = Object.freeze([1, 2, 3]); -a[0] = 1; +a[0] = a[2].toString(); const o = Object.freeze({ a: 1, b: "string" }); -o.b = "another"; +o.b = o.a.toString(); //// [objectFreeze.js] var f = Object.freeze(function foo(a, b) { return false; }); f(1, "") === false; +var C = (function () { + function C(a) { + } + return C; +}()); +var c = Object.freeze(C); +new c(1); var a = Object.freeze([1, 2, 3]); -a[0] = 1; +a[0] = a[2].toString(); var o = Object.freeze({ a: 1, b: "string" }); -o.b = "another"; +o.b = o.a.toString(); diff --git a/tests/cases/compiler/objectFreeze.ts b/tests/cases/compiler/objectFreeze.ts index 2c93fc8f0757f..5e8539831b1fa 100644 --- a/tests/cases/compiler/objectFreeze.ts +++ b/tests/cases/compiler/objectFreeze.ts @@ -1,8 +1,12 @@ const f = Object.freeze(function foo(a: number, b: string) { return false; }); f(1, "") === false; +class C { constructor(a: number) { } } +const c = Object.freeze(C); +new c(1); + const a = Object.freeze([1, 2, 3]); -a[0] = 1; +a[0] = a[2].toString(); const o = Object.freeze({ a: 1, b: "string" }); -o.b = "another"; +o.b = o.a.toString(); From 15d870bdcf6875b3d0824e642ce4aa673826dba6 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Wed, 23 Nov 2016 10:56:42 -0800 Subject: [PATCH 4/4] Use `Function` instead of call and construct signatures --- src/lib/es5.d.ts | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/lib/es5.d.ts b/src/lib/es5.d.ts index 097089e371ff2..1e6b44788b37b 100644 --- a/src/lib/es5.d.ts +++ b/src/lib/es5.d.ts @@ -186,19 +186,7 @@ interface ObjectConstructor { * Prevents the modification of existing property attributes and values, and prevents the addition of new properties. * @param o Object on which to lock the attributes. */ - freeze any>(f: T): T; - - /** - * Prevents the modification of existing property attributes and values, and prevents the addition of new properties. - * @param o Object on which to lock the attributes. - */ - freeze any>(c: T): T; - - /** - * Prevents the modification of existing property attributes and values, and prevents the addition of new properties. - * @param o Object on which to lock the attributes. - */ - freeze(f: (...args: any[]) => T): (...args: any[]) => T; + freeze(f: T): T; /** * Prevents the modification of existing property attributes and values, and prevents the addition of new properties.