From 55f9aba85581e96d12e1c6d32d46ce148f4a1d34 Mon Sep 17 00:00:00 2001 From: Yui T Date: Thu, 16 Apr 2015 14:32:23 -0700 Subject: [PATCH] Move tests with errors to separate file --- ... destructuringParameterDeclaration1ES5.ts} | 25 ---- .../destructuringParameterDeclaration2.ts | 110 +++++++++--------- ... destructuringParameterDeclaration3ES5.ts} | 3 +- .../destructuringParameterDeclaration3ES6.ts | 46 ++++++++ .../destructuringParameterDeclaration4.ts | 77 +++++------- .../destructuringParameterDeclaration5.ts | 50 ++++++++ ... => destructuringParameterDeclaration6.ts} | 0 7 files changed, 187 insertions(+), 124 deletions(-) rename tests/cases/conformance/es6/destructuring/{destructuringParameterDeclaration1.ts => destructuringParameterDeclaration1ES5.ts} (73%) rename tests/cases/conformance/es6/destructuring/{destructuringParameterDeclaration2ES6.ts => destructuringParameterDeclaration3ES5.ts} (91%) create mode 100644 tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES6.ts create mode 100644 tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration5.ts rename tests/cases/conformance/es6/destructuring/{destructuringParameterDeclaration3.ts => destructuringParameterDeclaration6.ts} (100%) diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES5.ts similarity index 73% rename from tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts rename to tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES5.ts index a36e679751c..01f3cda52bf 100644 --- a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES5.ts @@ -10,9 +10,6 @@ function a4({x, a}: { x: number, a: number }) { } a1([1, 2, [["world"]]]); a1([1, 2, [["world"]], 3]); -a1([1, "string", [["world"]]); // Error -a1([1, 2, [["world"]], "string"]); // Error - // If the declaration includes an initializer expression (which is permitted only // when the parameter list occurs in conjunction with a function body), @@ -23,7 +20,6 @@ function b2(z = null, o = { x: 0, y: undefined }) { } function b3({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } interface F1 { - b4(z = 10, [[a, b], d, {u}] = [[1, 2], "string", { u: false }]); // Error, no function body b5(z, y, [, a, b], {p, m: { q, r}}); } @@ -33,7 +29,6 @@ function b7([[a], b, [[c, d]]] = [[undefined], undefined, [[undefined, undefined b1([1, 2, 3]); // z is widen to the type any[] b2("string", { x: 200, y: "string" }); b2("string", { x: 200, y: true }); -b2("string", { x: "string", y: true }); // Error b6(["string", 1, 2]); // Shouldn't be an error b7([["string"], 1, [[true, false]]]); // Shouldn't be an error @@ -44,39 +39,28 @@ function c0({z: {x, y: {j}}}) { } function c1({z} = { z: 10 }) { } function c2({z = 10}) { } function c3({b}: { b: number|string} = { b: "hello" }) { } -function c4([z], z: number) { } // Duplicate identifier function c5([a, b, [[c]]]) { } function c6([a, b, [[c=1]]]) { } c0({z : { x: 1, y: { j: "world" } }}); // Implied type is { z: {x: any, y: {j: any}} } c0({z : { x: "string", y: { j: true } }}); // Implied type is { z: {x: any, y: {j: any}} } -c0({z : 1}); // Error, implied type is { z: {x: any, y: {j: any}} } -c1({}); // Error, implied type is {z:number}? -c1({ z: true }); // Error, implied type is {z:number}? c1(); // Implied type is {z:number}? c1({ z: 1 }) // Implied type is {z:number}? c2({}); // Implied type is {z?: number} c2({z:1}); // Implied type is {z?: number} -c2({z:false}); // Error, implied type is {z?: number} c3({ b: 1 }); // Implied type is { b: number|string }. -c3({ b: true }); // Error, implied type is { b: number|string }. c5([1, 2, [["string"]]]); // Implied type is is [any, any, [[any]]] c5([1, 2, [["string"]], false, true]); // Implied type is is [any, any, [[any]]] -c5([1, 2, false, true]); // Error, implied type is [any, any, [[any]]] - -c6([1, 2, [["string"]]]); // Error, implied type is [any, any, [[number]]] // Use initializer // A parameter can be marked optional by following its name or binding pattern with a question mark (?) // or by including an initializer. function d0(x?) { } function d0(x = 10) { } -function d1([a, b, c]?) { } // Error, binding pattern can't be optional in implementation signature -function d2({x, y, z}?) { } // Error, binding pattern can't be optional in implementation signature interface F2 { d3([a, b, c]?); @@ -97,11 +81,6 @@ class C3 implements F2 { e0([a, b, c]) { } } -class C4 implements F2 { - d3([a, b, c]?) { } - d4({x, y, c}) { } - e0([a, b, q]) { } -} function d5({x, y} = { x: 1, y: 2 }) { } d5(); // Parameter is optional as its declaration included an initializer @@ -115,7 +94,3 @@ function e2({x}: { x: number }) { } // x is type number function e3({x}: { x?: number }) { } // x is an optional with type number function e4({x: [number,string,any] }) { } // x has type [any, any, any] function e5({x: [a, b, c]}: { x: [number, number, number] }) { } // x has type [any, any, any] - -function e6({x: [number, number, number]}) { } // should be an error, duplicate identifier; - - diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts index b764e4f286c..19f508c0625 100644 --- a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts @@ -1,61 +1,67 @@ -// If the parameter is a rest parameter, the parameter type is any[] -// A type annotation for a rest parameter must denote an array type. +// A parameter declaration may specify either an identifier or a binding pattern. +// The identifiers specified in parameter declarations and binding patterns +// in a parameter list must be unique within that parameter list. -// RestParameter: -// ... Identifier TypeAnnotation(opt) - -type arrayString = Array -type someArray = Array | number[]; -type stringOrNumArray = Array; - -function a0(...x: [number, number, string]) { } // Error, rest parameter must be array type -function a1(...x: (number|string)[]) { } -function a2(...a) { } -function a3(...a: Array) { } -function a4(...a: arrayString) { } -function a5(...a: stringOrNumArray) { } -function a6(...a: someArray) { } // Error, rest parameter must be array type -function a7(...b?) { } // Error, can't be optional -function a8(...b = [1,2,3]) { } // Error, can't have initializer -function a9([a, b, [[c]]]) { } -function a10([a, b, [[c]], ...x]) { } -function a11([a, b, c, ...x]: number[]) { } +// If the declaration includes a type annotation, the parameter is of that type +function a0([a, b, [[c]]]: [number, number, string[][]]) { } +a0([1, "string", [["world"]]); // Error +a0([1, 2, [["world"]], "string"]); // Error -a1(1, 2, "hello", true); // Error, parameter type is (number|string)[] -var array = [1, 2, 3]; -var array2 = [true, false, "hello"]; -a2([...array]); -a1(...array); -a1(...array2); // Error parameter type is (number|string)[] +// If the declaration includes an initializer expression (which is permitted only +// when the parameter list occurs in conjunction with a function body), +// the parameter type is the widened form (section 3.11) of the type of the initializer expression. -a9([1, 2, [["string"]], false, true]); // Parameter type is [any, any, [[any]]] -a9([1, 2, "string", false, true]); // Error, parameter type is [any, any, [[any]]] -a9([1, 2]); // Error, parameter type is [any, any, [[any]]] - -a10([1, 2, [["string"]], false, true]); // Parameter type is any[] -a10([1, 2, 3, false, true]); // Parameter type is any[] -a10([1, 2]); // Parameter type is any[] - -a11([1, 2]); // Parameter type is number[] -a11([1, 2, "string"]); // Error, parameter type is number[] - - -class C { - constructor(public ...a) { } // Error, rest parameter can't have accessibilityModifier +interface F1 { + b0(z = 10, [[a, b], d, {u}] = [[1, 2], "string", { u: false }]); // Error, no function body } -// Rest parameter with generic -function foo(...a: T[]) { } -foo("hello", 1, 2); // Error -foo("hello", 1, 2); -foo("hello", "world"); +function b1(z = null, o = { x: 0, y: undefined }) { } +function b2([a, z, y] = [undefined, null, undefined]) { } +function b3([[a], b, [[c, d]]] = [[undefined], undefined, [[undefined, undefined]]]) { } -enum E { a, b } -const enum E1 { a, b } -function foo1(...a: T[]) { } -foo1(1, 2, 3, E.a); -foo1(1, 2, 3, E1.a, E.b); -foo1(1, 2, "string", E1.a, E.b); // Error +b1("string", { x: "string", y: true }); // Error + +// If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3) +function c0({z: {x, y: {j}}}) { } +function c1({z} = { z: 10 }) { } +function c2({z = 10}) { } +function c3({b}: { b: number|string } = { b: "hello" }) { } +function c4([z], z: number) { } // Error Duplicate identifier +function c5([a, b, [[c]]]) { } +function c6([a, b, [[c = 1]]]) { } + +c0({ z: 1 }); // Error, implied type is { z: {x: any, y: {j: any}} } +c1({}); // Error, implied type is {z:number}? +c1({ z: true }); // Error, implied type is {z:number}? +c2({ z: false }); // Error, implied type is {z?: number} +c3({ b: true }); // Error, implied type is { b: number|string }. +c5([1, 2, false, true]); // Error, implied type is [any, any, [[any]]] +c6([1, 2, [["string"]]]); // Error, implied type is [any, any, [[number]]] // Use initializer + +// A parameter can be marked optional by following its name or binding pattern with a question mark (?) +// or by including an initializer. Initializers (including binding property or element initializers) are +// permitted only when the parameter list occurs in conjunction with a function body + +function d1([a, b, c]?) { } // Error, binding pattern can't be optional in implementation signature +function d2({x, y, z}?) { } // Error, binding pattern can't be optional in implementation signature + +interface F2 { + d3([a, b, c]?); + d4({x, y, z}?); + e0([a, b, c]); +} + +class C4 implements F2 { + d3([a, b, c]?) { } // Error, binding pattern can't be optional in implementation signature + d4({x, y, c}) { } + e0([a, b, q]) { } +} + +// Destructuring parameter declarations do not permit type annotations on the individual binding patterns, +// as such annotations would conflict with the already established meaning of colons in object literals. +// Type annotations must instead be written on the top- level parameter declaration + +function e0({x: [number, number, number]}) { } // should be an error, duplicate identifier; diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES5.ts similarity index 91% rename from tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts rename to tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES5.ts index 59b08013a2a..6eab2225e03 100644 --- a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES5.ts @@ -30,8 +30,7 @@ a9([1, 2, [["string"]], false, true]); // Parameter type is [any, any, [[any]] a10([1, 2, [["string"]], false, true]); // Parameter type is any[] a10([1, 2, 3, false, true]); // Parameter type is any[] a10([1, 2]); // Parameter type is any[] - -a11([1, 2]); // Parameter type is number[] +a11([1, 2]); // Parameter type is number[] // Rest parameter with generic function foo(...a: T[]) { } diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES6.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES6.ts new file mode 100644 index 00000000000..6eab2225e03 --- /dev/null +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES6.ts @@ -0,0 +1,46 @@ +// @target: es6 + +// If the parameter is a rest parameter, the parameter type is any[] +// A type annotation for a rest parameter must denote an array type. + +// RestParameter: +// ... Identifier TypeAnnotation(opt) + +type arrayString = Array +type someArray = Array | number[]; +type stringOrNumArray = Array; + +function a1(...x: (number|string)[]) { } +function a2(...a) { } +function a3(...a: Array) { } +function a4(...a: arrayString) { } +function a5(...a: stringOrNumArray) { } +function a9([a, b, [[c]]]) { } +function a10([a, b, [[c]], ...x]) { } +function a11([a, b, c, ...x]: number[]) { } + + +var array = [1, 2, 3]; +var array2 = [true, false, "hello"]; +a2([...array]); +a1(...array); + +a9([1, 2, [["string"]], false, true]); // Parameter type is [any, any, [[any]]] + +a10([1, 2, [["string"]], false, true]); // Parameter type is any[] +a10([1, 2, 3, false, true]); // Parameter type is any[] +a10([1, 2]); // Parameter type is any[] +a11([1, 2]); // Parameter type is number[] + +// Rest parameter with generic +function foo(...a: T[]) { } +foo("hello", 1, 2); +foo("hello", "world"); + +enum E { a, b } +const enum E1 { a, b } +function foo1(...a: T[]) { } +foo1(1, 2, 3, E.a); +foo1(1, 2, 3, E1.a, E.b); + + diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts index 4f0f52d3065..02fc84b380a 100644 --- a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts @@ -1,49 +1,36 @@ -// Parameter with generic -interface F { } -class Class implements F { - constructor() { } +// If the parameter is a rest parameter, the parameter type is any[] +// A type annotation for a rest parameter must denote an array type. + +// RestParameter: +// ... Identifier TypeAnnotation(opt) + +type arrayString = Array +type someArray = Array | number[]; +type stringOrNumArray = Array; + +function a0(...x: [number, number, string]) { } // Error, rest parameter must be array type +function a1(...x: (number|string)[]) { } +function a2(...a: someArray) { } // Error, rest parameter must be array type +function a3(...b?) { } // Error, can't be optional +function a4(...b = [1,2,3]) { } // Error, can't have initializer +function a5([a, b, [[c]]]) { } +function a6([a, b, c, ...x]: number[]) { } + + +a1(1, 2, "hello", true); // Error, parameter type is (number|string)[] +a1(...array2); // Error parameter type is (number|string)[] +a5([1, 2, "string", false, true]); // Error, parameter type is [any, any, [[any]]] +a5([1, 2]); // Error, parameter type is [any, any, [[any]]] +a6([1, 2, "string"]); // Error, parameter type is number[] + + +var temp = [1, 2, 3]; +class C { + constructor(public ...temp) { } // Error, rest parameter can't have accessibilityModifier } -class SubClass extends Class { - foo: boolean; - constructor() { super(); } -} - -class D implements F { - foo: boolean - constructor() { } -} - -class SubD extends D { - bar: number - constructor() { - super(); - } -} +// Rest parameter with generic +function foo1(...a: T[]) { } +foo1(1, 2, "string", E1.a, E.b); // Error -function d0({x} = { x: new Class() }) { } -function d1({x}: { x: F }) { } -function d2({x}: { x: Class }) { } -function d3({y}: { y: D }) { } -function d4({y} = { y: new D() }) { } - -var obj = new Class(); -d0({ x: 1 }); -d0({ x: {} }); -d0({ x: "string" }); - -d1({ x: new Class() }); -d1({ x: {} }); -d1({ x: "string" }); - -d2({ x: new SubClass() }); -d2({ x: {} }); - -d3({ y: new SubD() }); -d3({ y: new SubClass() }); -// Error -d3({ y: new Class() }); -d3({}); -d3({ y: 1 }); -d3({ y: "world" }); \ No newline at end of file diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration5.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration5.ts new file mode 100644 index 00000000000..c4fcffed491 --- /dev/null +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration5.ts @@ -0,0 +1,50 @@ +// Parameter Declaration with generic + +interface F { } +class Class implements F { + constructor() { } +} + +class SubClass extends Class { + foo: boolean; + constructor() { super(); } +} + +class D implements F { + foo: boolean + constructor() { } +} + +class SubD extends D { + bar: number + constructor() { + super(); + } +} + + +function d0({x} = { x: new Class() }) { } +function d1({x}: { x: F }) { } +function d2({x}: { x: Class }) { } +function d3({y}: { y: D }) { } +function d4({y} = { y: new D() }) { } + +var obj = new Class(); +d0({ x: 1 }); +d0({ x: {} }); +d0({ x: "string" }); + +d1({ x: new Class() }); +d1({ x: {} }); +d1({ x: "string" }); + +d2({ x: new SubClass() }); +d2({ x: {} }); + +d3({ y: new SubD() }); +d3({ y: new SubClass() }); +// Error +d3({ y: new Class() }); +d3({}); +d3({ y: 1 }); +d3({ y: "world" }); \ No newline at end of file diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration6.ts similarity index 100% rename from tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts rename to tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration6.ts