Skip to content
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

Don't error on destructure of private property with computed property syntax #26360

Merged
1 commit merged into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24391,7 +24391,7 @@ namespace ts {
if (nameText) {
const property = getPropertyOfType(parentType!, nameText)!; // TODO: GH#18217
markPropertyAsReferenced(property, /*nodeForCheckWriteOnly*/ undefined, /*isThisAccess*/ false); // A destructuring is never a write-only reference.
if (parent.initializer && property) {
if (parent.initializer && property && !isComputedPropertyName(name)) {
checkPropertyAccessibility(parent, parent.initializer, parentType!, property);
}
}
Expand Down
13 changes: 9 additions & 4 deletions tests/baselines/reference/destructureComputedProperty.errors.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
tests/cases/compiler/destructureComputedProperty.ts(8,7): error TS2341: Property 'p' is private and only accessible within class 'C'.
tests/cases/compiler/destructureComputedProperty.ts(7,7): error TS2341: Property 'p' is private and only accessible within class 'C'.
tests/cases/compiler/destructureComputedProperty.ts(10,7): error TS2341: Property 'p' is private and only accessible within class 'C'.


==== tests/cases/compiler/destructureComputedProperty.ts (1 errors) ====
==== tests/cases/compiler/destructureComputedProperty.ts (2 errors) ====
declare const ab: { n: number } | { n: string };
const nameN = "n";
const { [nameN]: n } = ab;

class C { private p: number; }
const nameP = "p";
const { [nameP]: p } = new C();
const { p: p2 } = new C();
const { "p": p0 } = new C();
~~~~~~~~~~~
!!! error TS2341: Property 'p' is private and only accessible within class 'C'.
const { ["p"]: p1 } = new C();
const { [nameP]: p2 } = new C();
const { p: p3 } = new C();
~~~~~~~~~
!!! error TS2341: Property 'p' is private and only accessible within class 'C'.

12 changes: 8 additions & 4 deletions tests/baselines/reference/destructureComputedProperty.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ const { [nameN]: n } = ab;

class C { private p: number; }
const nameP = "p";
const { [nameP]: p } = new C();
const { p: p2 } = new C();
const { "p": p0 } = new C();
const { ["p"]: p1 } = new C();
const { [nameP]: p2 } = new C();
const { p: p3 } = new C();


//// [destructureComputedProperty.js]
Expand All @@ -18,5 +20,7 @@ var C = /** @class */ (function () {
return C;
}());
var nameP = "p";
var _b = nameP, p = new C()[_b];
var p2 = new C().p;
var p0 = new C()["p"];
var p1 = new C()["p"];
var _b = nameP, p2 = new C()[_b];
var p3 = new C().p;
17 changes: 13 additions & 4 deletions tests/baselines/reference/destructureComputedProperty.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,22 @@ class C { private p: number; }
const nameP = "p";
>nameP : Symbol(nameP, Decl(destructureComputedProperty.ts, 5, 5))

const { [nameP]: p } = new C();
const { "p": p0 } = new C();
>p0 : Symbol(p0, Decl(destructureComputedProperty.ts, 6, 7))
>C : Symbol(C, Decl(destructureComputedProperty.ts, 2, 26))

const { ["p"]: p1 } = new C();
>"p" : Symbol(p1, Decl(destructureComputedProperty.ts, 7, 7))
>p1 : Symbol(p1, Decl(destructureComputedProperty.ts, 7, 7))
>C : Symbol(C, Decl(destructureComputedProperty.ts, 2, 26))

const { [nameP]: p2 } = new C();
>nameP : Symbol(nameP, Decl(destructureComputedProperty.ts, 5, 5))
>p : Symbol(p, Decl(destructureComputedProperty.ts, 6, 7))
>p2 : Symbol(p2, Decl(destructureComputedProperty.ts, 8, 7))
>C : Symbol(C, Decl(destructureComputedProperty.ts, 2, 26))

const { p: p2 } = new C();
const { p: p3 } = new C();
>p : Symbol(C.p, Decl(destructureComputedProperty.ts, 4, 9))
>p2 : Symbol(p2, Decl(destructureComputedProperty.ts, 7, 7))
>p3 : Symbol(p3, Decl(destructureComputedProperty.ts, 9, 7))
>C : Symbol(C, Decl(destructureComputedProperty.ts, 2, 26))

19 changes: 15 additions & 4 deletions tests/baselines/reference/destructureComputedProperty.types
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,26 @@ const nameP = "p";
>nameP : "p"
>"p" : "p"

const { [nameP]: p } = new C();
const { "p": p0 } = new C();
>p0 : number
>new C() : C
>C : typeof C

const { ["p"]: p1 } = new C();
>"p" : "p"
>p1 : number
>new C() : C
>C : typeof C

const { [nameP]: p2 } = new C();
>nameP : "p"
>p : number
>p2 : number
>new C() : C
>C : typeof C

const { p: p2 } = new C();
const { p: p3 } = new C();
>p : any
>p2 : number
>p3 : number
>new C() : C
>C : typeof C

6 changes: 4 additions & 2 deletions tests/cases/compiler/destructureComputedProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ const { [nameN]: n } = ab;

class C { private p: number; }
const nameP = "p";
const { [nameP]: p } = new C();
const { p: p2 } = new C();
const { "p": p0 } = new C();
const { ["p"]: p1 } = new C();
const { [nameP]: p2 } = new C();
const { p: p3 } = new C();