From cabfdba7327a5e2c738c7488f4b6f4b470c191eb Mon Sep 17 00:00:00 2001 From: Yui T Date: Tue, 5 May 2015 15:01:01 -0700 Subject: [PATCH] Add tests for duplicate identifier in parameter declaration --- .../declarationEmitDestructuring2.errors.txt | 25 ++++ ...cturingParameterDeclaration1ES6.errors.txt | 111 ++++++++++++++++++ ...gElementInParameterDeclaration1.errors.txt | 70 +++++++++++ ...erBindingElementInParameterDeclaration1.js | 39 ++++++ ...gElementInParameterDeclaration2.errors.txt | 71 +++++++++++ ...erBindingElementInParameterDeclaration2.js | 41 +++++++ ...erBindingElementInParameterDeclaration1.ts | 10 ++ ...erBindingElementInParameterDeclaration2.ts | 11 ++ 8 files changed, 378 insertions(+) create mode 100644 tests/baselines/reference/declarationEmitDestructuring2.errors.txt create mode 100644 tests/baselines/reference/destructuringParameterDeclaration1ES6.errors.txt create mode 100644 tests/baselines/reference/duplicateIdentifierBindingElementInParameterDeclaration1.errors.txt create mode 100644 tests/baselines/reference/duplicateIdentifierBindingElementInParameterDeclaration1.js create mode 100644 tests/baselines/reference/duplicateIdentifierBindingElementInParameterDeclaration2.errors.txt create mode 100644 tests/baselines/reference/duplicateIdentifierBindingElementInParameterDeclaration2.js create mode 100644 tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration1.ts create mode 100644 tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration2.ts diff --git a/tests/baselines/reference/declarationEmitDestructuring2.errors.txt b/tests/baselines/reference/declarationEmitDestructuring2.errors.txt new file mode 100644 index 00000000000..4e8561e83ad --- /dev/null +++ b/tests/baselines/reference/declarationEmitDestructuring2.errors.txt @@ -0,0 +1,25 @@ +tests/cases/compiler/declarationEmitDestructuring2.ts(3,13): error TS2300: Duplicate identifier 'a'. +tests/cases/compiler/declarationEmitDestructuring2.ts(3,17): error TS2300: Duplicate identifier 'b'. +tests/cases/compiler/declarationEmitDestructuring2.ts(3,23): error TS2300: Duplicate identifier 'c'. +tests/cases/compiler/declarationEmitDestructuring2.ts(3,41): error TS2300: Duplicate identifier 'a'. +tests/cases/compiler/declarationEmitDestructuring2.ts(3,44): error TS2300: Duplicate identifier 'b'. +tests/cases/compiler/declarationEmitDestructuring2.ts(3,47): error TS2300: Duplicate identifier 'c'. + + +==== tests/cases/compiler/declarationEmitDestructuring2.ts (6 errors) ==== + function f({x = 10, y: [a, b, c, d] = [1, 2, 3, 4]} = { x: 10, y: [2, 4, 6, 8] }) { } + function g([a, b, c, d] = [1, 2, 3, 4]) { } + function h([a, [b], [[c]], {x = 10, y: [a, b, c], z: {a1, b1}}]){ } + ~ +!!! error TS2300: Duplicate identifier 'a'. + ~ +!!! error TS2300: Duplicate identifier 'b'. + ~ +!!! error TS2300: Duplicate identifier 'c'. + ~ +!!! error TS2300: Duplicate identifier 'a'. + ~ +!!! error TS2300: Duplicate identifier 'b'. + ~ +!!! error TS2300: Duplicate identifier 'c'. + function h1([a, [b], [[c]], {x = 10, y = [1, 2, 3], z: {a1, b1}}]){ } \ No newline at end of file diff --git a/tests/baselines/reference/destructuringParameterDeclaration1ES6.errors.txt b/tests/baselines/reference/destructuringParameterDeclaration1ES6.errors.txt new file mode 100644 index 00000000000..e900b9be393 --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration1ES6.errors.txt @@ -0,0 +1,111 @@ +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts(96,18): error TS2300: Duplicate identifier 'number'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts(96,26): error TS2300: Duplicate identifier 'number'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts(96,34): error TS2300: Duplicate identifier 'number'. + + +==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts (3 errors) ==== + // Conformance for emitting ES6 + + // 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. + + // If the declaration includes a type annotation, the parameter is of that type + function a1([a, b, [[c]]]: [number, number, string[][]]) { } + function a2(o: { x: number, a: number }) { } + function a3({j, k, l: {m, n}, q: [a, b, c]}: { j: number, k: string, l: { m: boolean, n: number }, q: (number|string)[] }) { }; + function a4({x, a}: { x: number, a: number }) { } + + a1([1, 2, [["world"]]]); + a1([1, 2, [["world"]], 3]); + + + // 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. + + function b1(z = [undefined, null]) { }; + function b2(z = null, o = { x: 0, y: undefined }) { } + function b3({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } + + interface F1 { + b5(z, y, [, a, b], {p, m: { q, r}}); + } + + function b6([a, z, y] = [undefined, null, undefined]) { } + 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 }); + + + // If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3) + enum Foo { a } + function c0({z: {x, y: {j}}}) { } + function c1({z} = { z: 10 }) { } + function c2({z = 10}) { } + function c3({b}: { b: number|string} = { b: "hello" }) { } + 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}} } + + 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} + + c3({ b: 1 }); // 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]]] + + + // A parameter can be marked optional by following its name or binding pattern with a question mark (?) + // or by including an initializer. + + interface F2 { + d3([a, b, c]?); + d4({x, y, z}?); + e0([a, b, c]); + } + + class C2 implements F2 { + constructor() { } + d3() { } + d4() { } + e0([a, b, c]) { } + } + + class C3 implements F2 { + d3([a, b, c]) { } + d4({x, y, z}) { } + e0([a, b, c]) { } + } + + function d5({x, y} = { x: 1, y: 2 }) { } + d5(); // Parameter is optional as its declaration included an initializer + + // 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 e1({x: number}) { } // x has type any NOT number + 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]}) { } // error, duplicate identifier; + ~~~~~~ +!!! error TS2300: Duplicate identifier 'number'. + ~~~~~~ +!!! error TS2300: Duplicate identifier 'number'. + ~~~~~~ +!!! error TS2300: Duplicate identifier 'number'. + + + \ No newline at end of file diff --git a/tests/baselines/reference/duplicateIdentifierBindingElementInParameterDeclaration1.errors.txt b/tests/baselines/reference/duplicateIdentifierBindingElementInParameterDeclaration1.errors.txt new file mode 100644 index 00000000000..62872f6c806 --- /dev/null +++ b/tests/baselines/reference/duplicateIdentifierBindingElementInParameterDeclaration1.errors.txt @@ -0,0 +1,70 @@ +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration1.ts(2,13): error TS2300: Duplicate identifier 'a'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration1.ts(2,17): error TS2300: Duplicate identifier 'a'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration1.ts(2,21): error TS2300: Duplicate identifier 'b'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration1.ts(2,27): error TS2300: Duplicate identifier 'b'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration1.ts(3,14): error TS2300: Duplicate identifier 'a'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration1.ts(3,17): error TS2300: Duplicate identifier 'a'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration1.ts(4,14): error TS2300: Duplicate identifier 'b'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration1.ts(4,19): error TS2300: Duplicate identifier 'b'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration1.ts(5,14): error TS2300: Duplicate identifier 'c'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration1.ts(5,17): error TS2300: Duplicate identifier 'c'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration1.ts(5,22): error TS2300: Duplicate identifier 'c'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration1.ts(6,14): error TS2300: Duplicate identifier 'd'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration1.ts(6,20): error TS2300: Duplicate identifier 'd'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration1.ts(7,14): error TS2300: Duplicate identifier 'e'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration1.ts(7,21): error TS2300: Duplicate identifier 'e'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration1.ts(7,27): error TS2300: Duplicate identifier 'e'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration1.ts(7,34): error TS2300: Duplicate identifier 'e'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration1.ts(7,39): error TS2300: Duplicate identifier 'e'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration1.ts(7,48): error TS2300: Duplicate identifier 'e'. + + +==== tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration1.ts (19 errors) ==== + + function f0(a, [a, [b]], {b}) { } + ~ +!!! error TS2300: Duplicate identifier 'a'. + ~ +!!! error TS2300: Duplicate identifier 'a'. + ~ +!!! error TS2300: Duplicate identifier 'b'. + ~ +!!! error TS2300: Duplicate identifier 'b'. + function f1([a, a]) { } + ~ +!!! error TS2300: Duplicate identifier 'a'. + ~ +!!! error TS2300: Duplicate identifier 'a'. + function f2({b}, {b}) { } + ~ +!!! error TS2300: Duplicate identifier 'b'. + ~ +!!! error TS2300: Duplicate identifier 'b'. + function f3([c,[c],[[c]]]) { } + ~ +!!! error TS2300: Duplicate identifier 'c'. + ~ +!!! error TS2300: Duplicate identifier 'c'. + ~ +!!! error TS2300: Duplicate identifier 'c'. + function f4({d, d:{d}}) { } + ~ +!!! error TS2300: Duplicate identifier 'd'. + ~ +!!! error TS2300: Duplicate identifier 'd'. + function f5({e, e: {e}}, {e}, [d,e, [[e]]], ...e) { } + ~ +!!! error TS2300: Duplicate identifier 'e'. + ~ +!!! error TS2300: Duplicate identifier 'e'. + ~ +!!! error TS2300: Duplicate identifier 'e'. + ~ +!!! error TS2300: Duplicate identifier 'e'. + ~ +!!! error TS2300: Duplicate identifier 'e'. + ~ +!!! error TS2300: Duplicate identifier 'e'. + + + \ No newline at end of file diff --git a/tests/baselines/reference/duplicateIdentifierBindingElementInParameterDeclaration1.js b/tests/baselines/reference/duplicateIdentifierBindingElementInParameterDeclaration1.js new file mode 100644 index 00000000000..cdf70b29835 --- /dev/null +++ b/tests/baselines/reference/duplicateIdentifierBindingElementInParameterDeclaration1.js @@ -0,0 +1,39 @@ +//// [duplicateIdentifierBindingElementInParameterDeclaration1.ts] + +function f0(a, [a, [b]], {b}) { } +function f1([a, a]) { } +function f2({b}, {b}) { } +function f3([c,[c],[[c]]]) { } +function f4({d, d:{d}}) { } +function f5({e, e: {e}}, {e}, [d,e, [[e]]], ...e) { } + + + + +//// [duplicateIdentifierBindingElementInParameterDeclaration1.js] +function f0(a, _a, _b) { + var a = _a[0], b = _a[1][0]; + var b = _b.b; +} +function f1(_a) { + var a = _a[0], a = _a[1]; +} +function f2(_a, _b) { + var b = _a.b; + var b = _b.b; +} +function f3(_a) { + var c = _a[0], c = _a[1][0], c = _a[2][0][0]; +} +function f4(_a) { + var d = _a.d, d = _a.d.d; +} +function f5(_a, _b, _c) { + var e = _a.e, e = _a.e.e; + var e = _b.e; + var d = _c[0], e = _c[1], e = _c[2][0][0]; + var e = []; + for (var _i = 3; _i < arguments.length; _i++) { + e[_i - 3] = arguments[_i]; + } +} diff --git a/tests/baselines/reference/duplicateIdentifierBindingElementInParameterDeclaration2.errors.txt b/tests/baselines/reference/duplicateIdentifierBindingElementInParameterDeclaration2.errors.txt new file mode 100644 index 00000000000..8a6df4b075b --- /dev/null +++ b/tests/baselines/reference/duplicateIdentifierBindingElementInParameterDeclaration2.errors.txt @@ -0,0 +1,71 @@ +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration2.ts(3,13): error TS2300: Duplicate identifier 'a'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration2.ts(3,17): error TS2300: Duplicate identifier 'a'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration2.ts(3,21): error TS2300: Duplicate identifier 'b'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration2.ts(3,27): error TS2300: Duplicate identifier 'b'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration2.ts(4,14): error TS2300: Duplicate identifier 'a'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration2.ts(4,17): error TS2300: Duplicate identifier 'a'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration2.ts(5,14): error TS2300: Duplicate identifier 'b'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration2.ts(5,19): error TS2300: Duplicate identifier 'b'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration2.ts(6,14): error TS2300: Duplicate identifier 'c'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration2.ts(6,18): error TS2300: Duplicate identifier 'c'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration2.ts(6,24): error TS2300: Duplicate identifier 'c'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration2.ts(7,14): error TS2300: Duplicate identifier 'd'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration2.ts(7,21): error TS2300: Duplicate identifier 'd'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration2.ts(8,14): error TS2300: Duplicate identifier 'e'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration2.ts(8,21): error TS2300: Duplicate identifier 'e'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration2.ts(8,27): error TS2300: Duplicate identifier 'e'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration2.ts(8,35): error TS2300: Duplicate identifier 'e'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration2.ts(8,40): error TS2300: Duplicate identifier 'e'. +tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration2.ts(8,49): error TS2300: Duplicate identifier 'e'. + + +==== tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration2.ts (19 errors) ==== + + "use strict" + function f0(a, [a, [b]], {b}) { } + ~ +!!! error TS2300: Duplicate identifier 'a'. + ~ +!!! error TS2300: Duplicate identifier 'a'. + ~ +!!! error TS2300: Duplicate identifier 'b'. + ~ +!!! error TS2300: Duplicate identifier 'b'. + function f1([a, a]) { } + ~ +!!! error TS2300: Duplicate identifier 'a'. + ~ +!!! error TS2300: Duplicate identifier 'a'. + function f2({b}, {b}) { } + ~ +!!! error TS2300: Duplicate identifier 'b'. + ~ +!!! error TS2300: Duplicate identifier 'b'. + function f3([c, [c], [[c]]]) { } + ~ +!!! error TS2300: Duplicate identifier 'c'. + ~ +!!! error TS2300: Duplicate identifier 'c'. + ~ +!!! error TS2300: Duplicate identifier 'c'. + function f4({d, d: {d}}) { } + ~ +!!! error TS2300: Duplicate identifier 'd'. + ~ +!!! error TS2300: Duplicate identifier 'd'. + function f5({e, e: {e}}, {e}, [d, e, [[e]]], ...e) { } + ~ +!!! error TS2300: Duplicate identifier 'e'. + ~ +!!! error TS2300: Duplicate identifier 'e'. + ~ +!!! error TS2300: Duplicate identifier 'e'. + ~ +!!! error TS2300: Duplicate identifier 'e'. + ~ +!!! error TS2300: Duplicate identifier 'e'. + ~ +!!! error TS2300: Duplicate identifier 'e'. + + + \ No newline at end of file diff --git a/tests/baselines/reference/duplicateIdentifierBindingElementInParameterDeclaration2.js b/tests/baselines/reference/duplicateIdentifierBindingElementInParameterDeclaration2.js new file mode 100644 index 00000000000..46f59c6c96d --- /dev/null +++ b/tests/baselines/reference/duplicateIdentifierBindingElementInParameterDeclaration2.js @@ -0,0 +1,41 @@ +//// [duplicateIdentifierBindingElementInParameterDeclaration2.ts] + +"use strict" +function f0(a, [a, [b]], {b}) { } +function f1([a, a]) { } +function f2({b}, {b}) { } +function f3([c, [c], [[c]]]) { } +function f4({d, d: {d}}) { } +function f5({e, e: {e}}, {e}, [d, e, [[e]]], ...e) { } + + + + +//// [duplicateIdentifierBindingElementInParameterDeclaration2.js] +"use strict"; +function f0(a, _a, _b) { + var a = _a[0], b = _a[1][0]; + var b = _b.b; +} +function f1(_a) { + var a = _a[0], a = _a[1]; +} +function f2(_a, _b) { + var b = _a.b; + var b = _b.b; +} +function f3(_a) { + var c = _a[0], c = _a[1][0], c = _a[2][0][0]; +} +function f4(_a) { + var d = _a.d, d = _a.d.d; +} +function f5(_a, _b, _c) { + var e = _a.e, e = _a.e.e; + var e = _b.e; + var d = _c[0], e = _c[1], e = _c[2][0][0]; + var e = []; + for (var _i = 3; _i < arguments.length; _i++) { + e[_i - 3] = arguments[_i]; + } +} diff --git a/tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration1.ts b/tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration1.ts new file mode 100644 index 00000000000..e5c5965301e --- /dev/null +++ b/tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration1.ts @@ -0,0 +1,10 @@ +// @target: es5 + +function f0(a, [a, [b]], {b}) { } +function f1([a, a]) { } +function f2({b}, {b}) { } +function f3([c,[c],[[c]]]) { } +function f4({d, d:{d}}) { } +function f5({e, e: {e}}, {e}, [d,e, [[e]]], ...e) { } + + diff --git a/tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration2.ts b/tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration2.ts new file mode 100644 index 00000000000..0ed6100ca34 --- /dev/null +++ b/tests/cases/compiler/duplicateIdentifierBindingElementInParameterDeclaration2.ts @@ -0,0 +1,11 @@ +// @target: es5 + +"use strict" +function f0(a, [a, [b]], {b}) { } +function f1([a, a]) { } +function f2({b}, {b}) { } +function f3([c, [c], [[c]]]) { } +function f4({d, d: {d}}) { } +function f5({e, e: {e}}, {e}, [d, e, [[e]]], ...e) { } + +