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

This commit is contained in:
Andy 2018-08-10 15:11:04 -07:00 committed by GitHub
parent c8e10a9a66
commit 46d3caab7c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 51 additions and 20 deletions

View file

@ -24389,7 +24389,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);
}
}

View file

@ -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'.

View file

@ -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]
@ -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;

View file

@ -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))

View file

@ -21,15 +21,26 @@ const nameP = "p";
>nameP : "p"
>"p" : "p"
const { [nameP]: p } = new C();
>nameP : "p"
>p : number
const { "p": p0 } = new C();
>p0 : number
>new C() : C
>C : typeof C
const { p: p2 } = new C();
>p : any
const { ["p"]: p1 } = new C();
>"p" : "p"
>p1 : number
>new C() : C
>C : typeof C
const { [nameP]: p2 } = new C();
>nameP : "p"
>p2 : number
>new C() : C
>C : typeof C
const { p: p3 } = new C();
>p : any
>p3 : number
>new C() : C
>C : typeof C

View file

@ -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();