Skip to content

Wire up 'writing' parameter through protected derived class detection #43455

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 1 commit into from
Apr 19, 2021
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
6 changes: 3 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19539,8 +19539,8 @@ namespace ts {

// Return true if the given class derives from each of the declaring classes of the protected
// constituents of the given property.
function isClassDerivedFromDeclaringClasses(checkClass: Type, prop: Symbol) {
return forEachProperty(prop, p => getDeclarationModifierFlagsFromSymbol(p) & ModifierFlags.Protected ?
function isClassDerivedFromDeclaringClasses(checkClass: Type, prop: Symbol, writing: boolean) {
return forEachProperty(prop, p => getDeclarationModifierFlagsFromSymbol(p, writing) & ModifierFlags.Protected ?
!hasBaseType(checkClass, getDeclaringClass(p)) : false) ? undefined : checkClass;
}

Expand Down Expand Up @@ -26753,7 +26753,7 @@ namespace ts {
// of the property as base classes
let enclosingClass = forEachEnclosingClass(node, enclosingDeclaration => {
const enclosingClass = <InterfaceType>getDeclaredTypeOfSymbol(getSymbolOfNode(enclosingDeclaration)!);
return isClassDerivedFromDeclaringClasses(enclosingClass, prop) ? enclosingClass : undefined;
return isClassDerivedFromDeclaringClasses(enclosingClass, prop, writing) ? enclosingClass : undefined;
});
// A protected property is accessible if the property is within the declaring class or classes derived from it
if (!enclosingClass) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
tests/cases/compiler/publicGetterProtectedSetterFromThisParameter.ts(33,7): error TS2446: Property 'q' is protected and only accessible through an instance of class 'A'. This is an instance of class 'B'.
tests/cases/compiler/publicGetterProtectedSetterFromThisParameter.ts(34,7): error TS2446: Property 'u' is protected and only accessible through an instance of class 'A'. This is an instance of class 'B'.


==== tests/cases/compiler/publicGetterProtectedSetterFromThisParameter.ts (2 errors) ====
class A {
get x() { return 0; }
protected set x(v: number) { }

public get y() { return 0; }
protected set y(v: number) { }
}

class B {
get q() { return 0; }
protected set q(v: number) { }

protected get u() { return 0; }
protected set u(v: number) { }

foo(this: A, a: A, b: B) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

highly recommend switching from foo/bar/baz to donkey/diddy/cranky

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bananas

// Should have no errors in this block
this.x = 0;
this.y = 0;
a.x = 0;
a.y = 0;
b.q = 0;
b.u = 0;
}
}

function bar(this: A, a: A, b: B) {
this.x = 0;
this.y = 0;
a.x = 0;
a.y = 0;
// These should error
b.q = 0;
~
!!! error TS2446: Property 'q' is protected and only accessible through an instance of class 'A'. This is an instance of class 'B'.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q and u are accessors on B, not A. This error message is wrong. Is that expected? (From the bug, it seems like it might be outside the scope of this fix.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'll file a new issue

b.u = 0;
~
!!! error TS2446: Property 'u' is protected and only accessible through an instance of class 'A'. This is an instance of class 'B'.
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//// [publicGetterProtectedSetterFromThisParameter.ts]
class A {
get x() { return 0; }
protected set x(v: number) { }

public get y() { return 0; }
protected set y(v: number) { }
}

class B {
get q() { return 0; }
protected set q(v: number) { }

protected get u() { return 0; }
protected set u(v: number) { }

foo(this: A, a: A, b: B) {
// Should have no errors in this block
this.x = 0;
this.y = 0;
a.x = 0;
a.y = 0;
b.q = 0;
b.u = 0;
}
}

function bar(this: A, a: A, b: B) {
this.x = 0;
this.y = 0;
a.x = 0;
a.y = 0;
// These should error
b.q = 0;
b.u = 0;
}


//// [publicGetterProtectedSetterFromThisParameter.js]
var A = /** @class */ (function () {
function A() {
}
Object.defineProperty(A.prototype, "x", {
get: function () { return 0; },
set: function (v) { },
enumerable: false,
configurable: true
});
Object.defineProperty(A.prototype, "y", {
get: function () { return 0; },
set: function (v) { },
enumerable: false,
configurable: true
});
return A;
}());
var B = /** @class */ (function () {
function B() {
}
Object.defineProperty(B.prototype, "q", {
get: function () { return 0; },
set: function (v) { },
enumerable: false,
configurable: true
});
Object.defineProperty(B.prototype, "u", {
get: function () { return 0; },
set: function (v) { },
enumerable: false,
configurable: true
});
B.prototype.foo = function (a, b) {
// Should have no errors in this block
this.x = 0;
this.y = 0;
a.x = 0;
a.y = 0;
b.q = 0;
b.u = 0;
};
return B;
}());
function bar(a, b) {
this.x = 0;
this.y = 0;
a.x = 0;
a.y = 0;
// These should error
b.q = 0;
b.u = 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
=== tests/cases/compiler/publicGetterProtectedSetterFromThisParameter.ts ===
class A {
>A : Symbol(A, Decl(publicGetterProtectedSetterFromThisParameter.ts, 0, 0))

get x() { return 0; }
>x : Symbol(A.x, Decl(publicGetterProtectedSetterFromThisParameter.ts, 0, 9), Decl(publicGetterProtectedSetterFromThisParameter.ts, 1, 23))

protected set x(v: number) { }
>x : Symbol(A.x, Decl(publicGetterProtectedSetterFromThisParameter.ts, 0, 9), Decl(publicGetterProtectedSetterFromThisParameter.ts, 1, 23))
>v : Symbol(v, Decl(publicGetterProtectedSetterFromThisParameter.ts, 2, 18))

public get y() { return 0; }
>y : Symbol(A.y, Decl(publicGetterProtectedSetterFromThisParameter.ts, 2, 32), Decl(publicGetterProtectedSetterFromThisParameter.ts, 4, 30))

protected set y(v: number) { }
>y : Symbol(A.y, Decl(publicGetterProtectedSetterFromThisParameter.ts, 2, 32), Decl(publicGetterProtectedSetterFromThisParameter.ts, 4, 30))
>v : Symbol(v, Decl(publicGetterProtectedSetterFromThisParameter.ts, 5, 18))
}

class B {
>B : Symbol(B, Decl(publicGetterProtectedSetterFromThisParameter.ts, 6, 1))

get q() { return 0; }
>q : Symbol(B.q, Decl(publicGetterProtectedSetterFromThisParameter.ts, 8, 9), Decl(publicGetterProtectedSetterFromThisParameter.ts, 9, 23))

protected set q(v: number) { }
>q : Symbol(B.q, Decl(publicGetterProtectedSetterFromThisParameter.ts, 8, 9), Decl(publicGetterProtectedSetterFromThisParameter.ts, 9, 23))
>v : Symbol(v, Decl(publicGetterProtectedSetterFromThisParameter.ts, 10, 18))

protected get u() { return 0; }
>u : Symbol(B.u, Decl(publicGetterProtectedSetterFromThisParameter.ts, 10, 32), Decl(publicGetterProtectedSetterFromThisParameter.ts, 12, 33))

protected set u(v: number) { }
>u : Symbol(B.u, Decl(publicGetterProtectedSetterFromThisParameter.ts, 10, 32), Decl(publicGetterProtectedSetterFromThisParameter.ts, 12, 33))
>v : Symbol(v, Decl(publicGetterProtectedSetterFromThisParameter.ts, 13, 18))

foo(this: A, a: A, b: B) {
>foo : Symbol(B.foo, Decl(publicGetterProtectedSetterFromThisParameter.ts, 13, 32))
>this : Symbol(this, Decl(publicGetterProtectedSetterFromThisParameter.ts, 15, 6))
>A : Symbol(A, Decl(publicGetterProtectedSetterFromThisParameter.ts, 0, 0))
>a : Symbol(a, Decl(publicGetterProtectedSetterFromThisParameter.ts, 15, 14))
>A : Symbol(A, Decl(publicGetterProtectedSetterFromThisParameter.ts, 0, 0))
>b : Symbol(b, Decl(publicGetterProtectedSetterFromThisParameter.ts, 15, 20))
>B : Symbol(B, Decl(publicGetterProtectedSetterFromThisParameter.ts, 6, 1))

// Should have no errors in this block
this.x = 0;
>this.x : Symbol(A.x, Decl(publicGetterProtectedSetterFromThisParameter.ts, 0, 9), Decl(publicGetterProtectedSetterFromThisParameter.ts, 1, 23))
>this : Symbol(this, Decl(publicGetterProtectedSetterFromThisParameter.ts, 15, 6))
>x : Symbol(A.x, Decl(publicGetterProtectedSetterFromThisParameter.ts, 0, 9), Decl(publicGetterProtectedSetterFromThisParameter.ts, 1, 23))

this.y = 0;
>this.y : Symbol(A.y, Decl(publicGetterProtectedSetterFromThisParameter.ts, 2, 32), Decl(publicGetterProtectedSetterFromThisParameter.ts, 4, 30))
>this : Symbol(this, Decl(publicGetterProtectedSetterFromThisParameter.ts, 15, 6))
>y : Symbol(A.y, Decl(publicGetterProtectedSetterFromThisParameter.ts, 2, 32), Decl(publicGetterProtectedSetterFromThisParameter.ts, 4, 30))

a.x = 0;
>a.x : Symbol(A.x, Decl(publicGetterProtectedSetterFromThisParameter.ts, 0, 9), Decl(publicGetterProtectedSetterFromThisParameter.ts, 1, 23))
>a : Symbol(a, Decl(publicGetterProtectedSetterFromThisParameter.ts, 15, 14))
>x : Symbol(A.x, Decl(publicGetterProtectedSetterFromThisParameter.ts, 0, 9), Decl(publicGetterProtectedSetterFromThisParameter.ts, 1, 23))

a.y = 0;
>a.y : Symbol(A.y, Decl(publicGetterProtectedSetterFromThisParameter.ts, 2, 32), Decl(publicGetterProtectedSetterFromThisParameter.ts, 4, 30))
>a : Symbol(a, Decl(publicGetterProtectedSetterFromThisParameter.ts, 15, 14))
>y : Symbol(A.y, Decl(publicGetterProtectedSetterFromThisParameter.ts, 2, 32), Decl(publicGetterProtectedSetterFromThisParameter.ts, 4, 30))

b.q = 0;
>b.q : Symbol(B.q, Decl(publicGetterProtectedSetterFromThisParameter.ts, 8, 9), Decl(publicGetterProtectedSetterFromThisParameter.ts, 9, 23))
>b : Symbol(b, Decl(publicGetterProtectedSetterFromThisParameter.ts, 15, 20))
>q : Symbol(B.q, Decl(publicGetterProtectedSetterFromThisParameter.ts, 8, 9), Decl(publicGetterProtectedSetterFromThisParameter.ts, 9, 23))

b.u = 0;
>b.u : Symbol(B.u, Decl(publicGetterProtectedSetterFromThisParameter.ts, 10, 32), Decl(publicGetterProtectedSetterFromThisParameter.ts, 12, 33))
>b : Symbol(b, Decl(publicGetterProtectedSetterFromThisParameter.ts, 15, 20))
>u : Symbol(B.u, Decl(publicGetterProtectedSetterFromThisParameter.ts, 10, 32), Decl(publicGetterProtectedSetterFromThisParameter.ts, 12, 33))
}
}

function bar(this: A, a: A, b: B) {
>bar : Symbol(bar, Decl(publicGetterProtectedSetterFromThisParameter.ts, 24, 1))
>this : Symbol(this, Decl(publicGetterProtectedSetterFromThisParameter.ts, 26, 13))
>A : Symbol(A, Decl(publicGetterProtectedSetterFromThisParameter.ts, 0, 0))
>a : Symbol(a, Decl(publicGetterProtectedSetterFromThisParameter.ts, 26, 21))
>A : Symbol(A, Decl(publicGetterProtectedSetterFromThisParameter.ts, 0, 0))
>b : Symbol(b, Decl(publicGetterProtectedSetterFromThisParameter.ts, 26, 27))
>B : Symbol(B, Decl(publicGetterProtectedSetterFromThisParameter.ts, 6, 1))

this.x = 0;
>this.x : Symbol(A.x, Decl(publicGetterProtectedSetterFromThisParameter.ts, 0, 9), Decl(publicGetterProtectedSetterFromThisParameter.ts, 1, 23))
>this : Symbol(this, Decl(publicGetterProtectedSetterFromThisParameter.ts, 26, 13))
>x : Symbol(A.x, Decl(publicGetterProtectedSetterFromThisParameter.ts, 0, 9), Decl(publicGetterProtectedSetterFromThisParameter.ts, 1, 23))

this.y = 0;
>this.y : Symbol(A.y, Decl(publicGetterProtectedSetterFromThisParameter.ts, 2, 32), Decl(publicGetterProtectedSetterFromThisParameter.ts, 4, 30))
>this : Symbol(this, Decl(publicGetterProtectedSetterFromThisParameter.ts, 26, 13))
>y : Symbol(A.y, Decl(publicGetterProtectedSetterFromThisParameter.ts, 2, 32), Decl(publicGetterProtectedSetterFromThisParameter.ts, 4, 30))

a.x = 0;
>a.x : Symbol(A.x, Decl(publicGetterProtectedSetterFromThisParameter.ts, 0, 9), Decl(publicGetterProtectedSetterFromThisParameter.ts, 1, 23))
>a : Symbol(a, Decl(publicGetterProtectedSetterFromThisParameter.ts, 26, 21))
>x : Symbol(A.x, Decl(publicGetterProtectedSetterFromThisParameter.ts, 0, 9), Decl(publicGetterProtectedSetterFromThisParameter.ts, 1, 23))

a.y = 0;
>a.y : Symbol(A.y, Decl(publicGetterProtectedSetterFromThisParameter.ts, 2, 32), Decl(publicGetterProtectedSetterFromThisParameter.ts, 4, 30))
>a : Symbol(a, Decl(publicGetterProtectedSetterFromThisParameter.ts, 26, 21))
>y : Symbol(A.y, Decl(publicGetterProtectedSetterFromThisParameter.ts, 2, 32), Decl(publicGetterProtectedSetterFromThisParameter.ts, 4, 30))

// These should error
b.q = 0;
>b.q : Symbol(B.q, Decl(publicGetterProtectedSetterFromThisParameter.ts, 8, 9), Decl(publicGetterProtectedSetterFromThisParameter.ts, 9, 23))
>b : Symbol(b, Decl(publicGetterProtectedSetterFromThisParameter.ts, 26, 27))
>q : Symbol(B.q, Decl(publicGetterProtectedSetterFromThisParameter.ts, 8, 9), Decl(publicGetterProtectedSetterFromThisParameter.ts, 9, 23))

b.u = 0;
>b.u : Symbol(B.u, Decl(publicGetterProtectedSetterFromThisParameter.ts, 10, 32), Decl(publicGetterProtectedSetterFromThisParameter.ts, 12, 33))
>b : Symbol(b, Decl(publicGetterProtectedSetterFromThisParameter.ts, 26, 27))
>u : Symbol(B.u, Decl(publicGetterProtectedSetterFromThisParameter.ts, 10, 32), Decl(publicGetterProtectedSetterFromThisParameter.ts, 12, 33))
}

Loading