mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 07:45:18 -06:00
Don't crash on computed property in destructure (#26334)
This commit is contained in:
parent
fce3d9f34d
commit
55a620c433
@ -24379,10 +24379,13 @@ namespace ts {
|
||||
const parentType = getTypeForBindingElementParent(parent);
|
||||
const name = node.propertyName || node.name;
|
||||
if (!isBindingPattern(name)) {
|
||||
const property = getPropertyOfType(parentType!, getTextOfPropertyName(name))!; // TODO: GH#18217
|
||||
markPropertyAsReferenced(property, /*nodeForCheckWriteOnly*/ undefined, /*isThisAccess*/ false); // A destructuring is never a write-only reference.
|
||||
if (parent.initializer && property) {
|
||||
checkPropertyAccessibility(parent, parent.initializer, parentType!, property);
|
||||
const nameText = getTextOfPropertyName(name);
|
||||
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) {
|
||||
checkPropertyAccessibility(parent, parent.initializer, parentType!, property);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,15 @@
|
||||
tests/cases/compiler/destructureComputedProperty.ts(8,7): error TS2341: Property 'p' is private and only accessible within class 'C'.
|
||||
|
||||
|
||||
==== tests/cases/compiler/destructureComputedProperty.ts (1 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();
|
||||
~~~~~~~~~
|
||||
!!! error TS2341: Property 'p' is private and only accessible within class 'C'.
|
||||
|
||||
22
tests/baselines/reference/destructureComputedProperty.js
Normal file
22
tests/baselines/reference/destructureComputedProperty.js
Normal file
@ -0,0 +1,22 @@
|
||||
//// [destructureComputedProperty.ts]
|
||||
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();
|
||||
|
||||
|
||||
//// [destructureComputedProperty.js]
|
||||
var nameN = "n";
|
||||
var _a = nameN, n = ab[_a];
|
||||
var C = /** @class */ (function () {
|
||||
function C() {
|
||||
}
|
||||
return C;
|
||||
}());
|
||||
var nameP = "p";
|
||||
var _b = nameP, p = new C()[_b];
|
||||
var p2 = new C().p;
|
||||
@ -0,0 +1,31 @@
|
||||
=== tests/cases/compiler/destructureComputedProperty.ts ===
|
||||
declare const ab: { n: number } | { n: string };
|
||||
>ab : Symbol(ab, Decl(destructureComputedProperty.ts, 0, 13))
|
||||
>n : Symbol(n, Decl(destructureComputedProperty.ts, 0, 19))
|
||||
>n : Symbol(n, Decl(destructureComputedProperty.ts, 0, 35))
|
||||
|
||||
const nameN = "n";
|
||||
>nameN : Symbol(nameN, Decl(destructureComputedProperty.ts, 1, 5))
|
||||
|
||||
const { [nameN]: n } = ab;
|
||||
>nameN : Symbol(nameN, Decl(destructureComputedProperty.ts, 1, 5))
|
||||
>n : Symbol(n, Decl(destructureComputedProperty.ts, 2, 7))
|
||||
>ab : Symbol(ab, Decl(destructureComputedProperty.ts, 0, 13))
|
||||
|
||||
class C { private p: number; }
|
||||
>C : Symbol(C, Decl(destructureComputedProperty.ts, 2, 26))
|
||||
>p : Symbol(C.p, Decl(destructureComputedProperty.ts, 4, 9))
|
||||
|
||||
const nameP = "p";
|
||||
>nameP : Symbol(nameP, Decl(destructureComputedProperty.ts, 5, 5))
|
||||
|
||||
const { [nameP]: p } = new C();
|
||||
>nameP : Symbol(nameP, Decl(destructureComputedProperty.ts, 5, 5))
|
||||
>p : Symbol(p, Decl(destructureComputedProperty.ts, 6, 7))
|
||||
>C : Symbol(C, Decl(destructureComputedProperty.ts, 2, 26))
|
||||
|
||||
const { p: p2 } = new C();
|
||||
>p : Symbol(C.p, Decl(destructureComputedProperty.ts, 4, 9))
|
||||
>p2 : Symbol(p2, Decl(destructureComputedProperty.ts, 7, 7))
|
||||
>C : Symbol(C, Decl(destructureComputedProperty.ts, 2, 26))
|
||||
|
||||
35
tests/baselines/reference/destructureComputedProperty.types
Normal file
35
tests/baselines/reference/destructureComputedProperty.types
Normal file
@ -0,0 +1,35 @@
|
||||
=== tests/cases/compiler/destructureComputedProperty.ts ===
|
||||
declare const ab: { n: number } | { n: string };
|
||||
>ab : { n: number; } | { n: string; }
|
||||
>n : number
|
||||
>n : string
|
||||
|
||||
const nameN = "n";
|
||||
>nameN : "n"
|
||||
>"n" : "n"
|
||||
|
||||
const { [nameN]: n } = ab;
|
||||
>nameN : "n"
|
||||
>n : string | number
|
||||
>ab : { n: number; } | { n: string; }
|
||||
|
||||
class C { private p: number; }
|
||||
>C : C
|
||||
>p : number
|
||||
|
||||
const nameP = "p";
|
||||
>nameP : "p"
|
||||
>"p" : "p"
|
||||
|
||||
const { [nameP]: p } = new C();
|
||||
>nameP : "p"
|
||||
>p : number
|
||||
>new C() : C
|
||||
>C : typeof C
|
||||
|
||||
const { p: p2 } = new C();
|
||||
>p : any
|
||||
>p2 : number
|
||||
>new C() : C
|
||||
>C : typeof C
|
||||
|
||||
8
tests/cases/compiler/destructureComputedProperty.ts
Normal file
8
tests/cases/compiler/destructureComputedProperty.ts
Normal file
@ -0,0 +1,8 @@
|
||||
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();
|
||||
Loading…
x
Reference in New Issue
Block a user