-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Disallow destructured parameter properties #1673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b434ee4
Added tests.
DanielRosenwasser 372b0a4
Updated parser lookahead for modifiers to anticipate object literals.
DanielRosenwasser e20b22c
Minor changes.
DanielRosenwasser f58e6fc
Fixed incorrect tests.
DanielRosenwasser 61e2eb6
Renamed tests.
DanielRosenwasser e19ebc6
Disallow binding patterns in parameter properties.
DanielRosenwasser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
tests/baselines/reference/destructuringParameterProperties1.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(2,17): error TS1187: A parameter property may not be a binding pattern. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(9,17): error TS1187: A parameter property may not be a binding pattern. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(16,17): error TS1187: A parameter property may not be a binding pattern. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(22,26): error TS2339: Property 'x' does not exist on type 'C1'. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(22,35): error TS2339: Property 'y' does not exist on type 'C1'. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(22,43): error TS2339: Property 'y' does not exist on type 'C1'. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(22,52): error TS2339: Property 'z' does not exist on type 'C1'. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(25,30): error TS2339: Property 'x' does not exist on type 'C2'. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(25,36): error TS2339: Property 'y' does not exist on type 'C2'. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(25,42): error TS2339: Property 'z' does not exist on type 'C2'. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(29,30): error TS2339: Property 'x' does not exist on type 'C3'. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(29,36): error TS2339: Property 'y' does not exist on type 'C3'. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(29,42): error TS2339: Property 'z' does not exist on type 'C3'. | ||
|
||
|
||
==== tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts (13 errors) ==== | ||
class C1 { | ||
constructor(public [x, y, z]: string[]) { | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS1187: A parameter property may not be a binding pattern. | ||
} | ||
} | ||
|
||
type TupleType1 = [string, number, boolean]; | ||
|
||
class C2 { | ||
constructor(public [x, y, z]: TupleType1) { | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS1187: A parameter property may not be a binding pattern. | ||
} | ||
} | ||
|
||
type ObjType1 = { x: number; y: string; z: boolean } | ||
|
||
class C3 { | ||
constructor(public { x, y, z }: ObjType1) { | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS1187: A parameter property may not be a binding pattern. | ||
} | ||
} | ||
|
||
var c1 = new C1([]); | ||
c1 = new C1(["larry", "{curly}", "moe"]); | ||
var useC1Properties = c1.x === c1.y && c1.y === c1.z; | ||
~ | ||
!!! error TS2339: Property 'x' does not exist on type 'C1'. | ||
~ | ||
!!! error TS2339: Property 'y' does not exist on type 'C1'. | ||
~ | ||
!!! error TS2339: Property 'y' does not exist on type 'C1'. | ||
~ | ||
!!! error TS2339: Property 'z' does not exist on type 'C1'. | ||
|
||
var c2 = new C2(["10", 10, !!10]); | ||
var [c2_x, c2_y, c2_z] = [c2.x, c2.y, c2.z]; | ||
~ | ||
!!! error TS2339: Property 'x' does not exist on type 'C2'. | ||
~ | ||
!!! error TS2339: Property 'y' does not exist on type 'C2'. | ||
~ | ||
!!! error TS2339: Property 'z' does not exist on type 'C2'. | ||
|
||
var c3 = new C3({x: 0, y: "", z: false}); | ||
c3 = new C3({x: 0, "y": "y", z: true}); | ||
var [c3_x, c3_y, c3_z] = [c3.x, c3.y, c3.z]; | ||
~ | ||
!!! error TS2339: Property 'x' does not exist on type 'C3'. | ||
~ | ||
!!! error TS2339: Property 'y' does not exist on type 'C3'. | ||
~ | ||
!!! error TS2339: Property 'z' does not exist on type 'C3'. |
61 changes: 61 additions & 0 deletions
61
tests/baselines/reference/destructuringParameterProperties1.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//// [destructuringParameterProperties1.ts] | ||
class C1 { | ||
constructor(public [x, y, z]: string[]) { | ||
} | ||
} | ||
|
||
type TupleType1 = [string, number, boolean]; | ||
|
||
class C2 { | ||
constructor(public [x, y, z]: TupleType1) { | ||
} | ||
} | ||
|
||
type ObjType1 = { x: number; y: string; z: boolean } | ||
|
||
class C3 { | ||
constructor(public { x, y, z }: ObjType1) { | ||
} | ||
} | ||
|
||
var c1 = new C1([]); | ||
c1 = new C1(["larry", "{curly}", "moe"]); | ||
var useC1Properties = c1.x === c1.y && c1.y === c1.z; | ||
|
||
var c2 = new C2(["10", 10, !!10]); | ||
var [c2_x, c2_y, c2_z] = [c2.x, c2.y, c2.z]; | ||
|
||
var c3 = new C3({x: 0, y: "", z: false}); | ||
c3 = new C3({x: 0, "y": "y", z: true}); | ||
var [c3_x, c3_y, c3_z] = [c3.x, c3.y, c3.z]; | ||
|
||
//// [destructuringParameterProperties1.js] | ||
var C1 = (function () { | ||
function C1(_a) { | ||
var x = _a[0], y = _a[1], z = _a[2]; | ||
this.[x, y, z] = [x, y, z]; | ||
} | ||
return C1; | ||
})(); | ||
var C2 = (function () { | ||
function C2(_a) { | ||
var x = _a[0], y = _a[1], z = _a[2]; | ||
this.[x, y, z] = [x, y, z]; | ||
} | ||
return C2; | ||
})(); | ||
var C3 = (function () { | ||
function C3(_a) { | ||
var x = _a.x, y = _a.y, z = _a.z; | ||
this.{ x, y, z } = { x, y, z }; | ||
} | ||
return C3; | ||
})(); | ||
var c1 = new C1([]); | ||
c1 = new C1(["larry", "{curly}", "moe"]); | ||
var useC1Properties = c1.x === c1.y && c1.y === c1.z; | ||
var c2 = new C2(["10", 10, !!10]); | ||
var _a = [c2.x, c2.y, c2.z], c2_x = _a[0], c2_y = _a[1], c2_z = _a[2]; | ||
var c3 = new C3({ x: 0, y: "", z: false }); | ||
c3 = new C3({ x: 0, "y": "y", z: true }); | ||
var _b = [c3.x, c3.y, c3.z], c3_x = _b[0], c3_y = _b[1], c3_z = _b[2]; |
56 changes: 56 additions & 0 deletions
56
tests/baselines/reference/destructuringParameterProperties2.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties2.ts(2,36): error TS1187: A parameter property may not be a binding pattern. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties2.ts(3,59): error TS2339: Property 'b' does not exist on type 'C1'. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties2.ts(3,83): error TS2339: Property 'c' does not exist on type 'C1'. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties2.ts(4,18): error TS2339: Property 'a' does not exist on type 'C1'. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties2.ts(9,21): error TS2339: Property 'a' does not exist on type 'C1'. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties2.ts(13,21): error TS2339: Property 'b' does not exist on type 'C1'. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties2.ts(17,21): error TS2339: Property 'c' does not exist on type 'C1'. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties2.ts(21,27): error TS2345: Argument of type '[number, undefined, string]' is not assignable to parameter of type '[number, string, boolean]'. | ||
|
||
|
||
==== tests/cases/conformance/es6/destructuring/destructuringParameterProperties2.ts (8 errors) ==== | ||
class C1 { | ||
constructor(private k: number, private [a, b, c]: [number, string, boolean]) { | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS1187: A parameter property may not be a binding pattern. | ||
if ((b === undefined && c === undefined) || (this.b === undefined && this.c === undefined)) { | ||
~ | ||
!!! error TS2339: Property 'b' does not exist on type 'C1'. | ||
~ | ||
!!! error TS2339: Property 'c' does not exist on type 'C1'. | ||
this.a = a || k; | ||
~ | ||
!!! error TS2339: Property 'a' does not exist on type 'C1'. | ||
} | ||
} | ||
|
||
public getA() { | ||
return this.a | ||
~ | ||
!!! error TS2339: Property 'a' does not exist on type 'C1'. | ||
} | ||
|
||
public getB() { | ||
return this.b | ||
~ | ||
!!! error TS2339: Property 'b' does not exist on type 'C1'. | ||
} | ||
|
||
public getC() { | ||
return this.c; | ||
~ | ||
!!! error TS2339: Property 'c' does not exist on type 'C1'. | ||
} | ||
} | ||
|
||
var x = new C1(undefined, [0, undefined, ""]); | ||
~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2345: Argument of type '[number, undefined, string]' is not assignable to parameter of type '[number, string, boolean]'. | ||
var [x_a, x_b, x_c] = [x.getA(), x.getB(), x.getC()]; | ||
|
||
var y = new C1(10, [0, "", true]); | ||
var [y_a, y_b, y_c] = [y.getA(), y.getB(), y.getC()]; | ||
|
||
var z = new C1(10, [undefined, "", null]); | ||
var [z_a, z_b, z_c] = [z.getA(), z.getB(), z.getC()]; | ||
|
58 changes: 58 additions & 0 deletions
58
tests/baselines/reference/destructuringParameterProperties2.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//// [destructuringParameterProperties2.ts] | ||
class C1 { | ||
constructor(private k: number, private [a, b, c]: [number, string, boolean]) { | ||
if ((b === undefined && c === undefined) || (this.b === undefined && this.c === undefined)) { | ||
this.a = a || k; | ||
} | ||
} | ||
|
||
public getA() { | ||
return this.a | ||
} | ||
|
||
public getB() { | ||
return this.b | ||
} | ||
|
||
public getC() { | ||
return this.c; | ||
} | ||
} | ||
|
||
var x = new C1(undefined, [0, undefined, ""]); | ||
var [x_a, x_b, x_c] = [x.getA(), x.getB(), x.getC()]; | ||
|
||
var y = new C1(10, [0, "", true]); | ||
var [y_a, y_b, y_c] = [y.getA(), y.getB(), y.getC()]; | ||
|
||
var z = new C1(10, [undefined, "", null]); | ||
var [z_a, z_b, z_c] = [z.getA(), z.getB(), z.getC()]; | ||
|
||
|
||
//// [destructuringParameterProperties2.js] | ||
var C1 = (function () { | ||
function C1(k, _a) { | ||
var a = _a[0], b = _a[1], c = _a[2]; | ||
this.k = k; | ||
this.[a, b, c] = [a, b, c]; | ||
if ((b === undefined && c === undefined) || (this.b === undefined && this.c === undefined)) { | ||
this.a = a || k; | ||
} | ||
} | ||
C1.prototype.getA = function () { | ||
return this.a; | ||
}; | ||
C1.prototype.getB = function () { | ||
return this.b; | ||
}; | ||
C1.prototype.getC = function () { | ||
return this.c; | ||
}; | ||
return C1; | ||
})(); | ||
var x = new C1(undefined, [0, undefined, ""]); | ||
var _a = [x.getA(), x.getB(), x.getC()], x_a = _a[0], x_b = _a[1], x_c = _a[2]; | ||
var y = new C1(10, [0, "", true]); | ||
var _b = [y.getA(), y.getB(), y.getC()], y_a = _b[0], y_b = _b[1], y_c = _b[2]; | ||
var z = new C1(10, [undefined, "", null]); | ||
var _c = [z.getA(), z.getB(), z.getC()], z_a = _c[0], z_b = _c[1], z_c = _c[2]; |
56 changes: 56 additions & 0 deletions
56
tests/baselines/reference/destructuringParameterProperties3.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties3.ts(2,31): error TS1187: A parameter property may not be a binding pattern. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties3.ts(3,59): error TS2339: Property 'b' does not exist on type 'C1<T, U, V>'. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties3.ts(3,83): error TS2339: Property 'c' does not exist on type 'C1<T, U, V>'. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties3.ts(4,18): error TS2339: Property 'a' does not exist on type 'C1<T, U, V>'. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties3.ts(9,21): error TS2339: Property 'a' does not exist on type 'C1<T, U, V>'. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties3.ts(13,21): error TS2339: Property 'b' does not exist on type 'C1<T, U, V>'. | ||
tests/cases/conformance/es6/destructuring/destructuringParameterProperties3.ts(17,21): error TS2339: Property 'c' does not exist on type 'C1<T, U, V>'. | ||
|
||
|
||
==== tests/cases/conformance/es6/destructuring/destructuringParameterProperties3.ts (7 errors) ==== | ||
class C1<T, U, V> { | ||
constructor(private k: T, private [a, b, c]: [T,U,V]) { | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS1187: A parameter property may not be a binding pattern. | ||
if ((b === undefined && c === undefined) || (this.b === undefined && this.c === undefined)) { | ||
~ | ||
!!! error TS2339: Property 'b' does not exist on type 'C1<T, U, V>'. | ||
~ | ||
!!! error TS2339: Property 'c' does not exist on type 'C1<T, U, V>'. | ||
this.a = a || k; | ||
~ | ||
!!! error TS2339: Property 'a' does not exist on type 'C1<T, U, V>'. | ||
} | ||
} | ||
|
||
public getA() { | ||
return this.a | ||
~ | ||
!!! error TS2339: Property 'a' does not exist on type 'C1<T, U, V>'. | ||
} | ||
|
||
public getB() { | ||
return this.b | ||
~ | ||
!!! error TS2339: Property 'b' does not exist on type 'C1<T, U, V>'. | ||
} | ||
|
||
public getC() { | ||
return this.c; | ||
~ | ||
!!! error TS2339: Property 'c' does not exist on type 'C1<T, U, V>'. | ||
} | ||
} | ||
|
||
var x = new C1(undefined, [0, true, ""]); | ||
var [x_a, x_b, x_c] = [x.getA(), x.getB(), x.getC()]; | ||
|
||
var y = new C1(10, [0, true, true]); | ||
var [y_a, y_b, y_c] = [y.getA(), y.getB(), y.getC()]; | ||
|
||
var z = new C1(10, [undefined, "", ""]); | ||
var [z_a, z_b, z_c] = [z.getA(), z.getB(), z.getC()]; | ||
|
||
var w = new C1(10, [undefined, undefined, undefined]); | ||
var [z_a, z_b, z_c] = [z.getA(), z.getB(), z.getC()]; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How can node.kind be Parameter if node is a SignatureDeclaration?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To clarify, it's not, GitHub diffs break after 10K lines.