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

Allow protected members to be accessed within the enclosing class #60001

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -33868,7 +33868,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// get the original type -- represented as the type constraint of the 'this' type
containingType = (containingType as TypeParameter).isThisType ? getConstraintOfTypeParameter(containingType as TypeParameter)! : getBaseConstraintOfType(containingType as TypeParameter)!; // TODO: GH#18217 Use a different variable that's allowed to be undefined
}
if (!containingType || !hasBaseType(containingType, enclosingClass)) {
if (!containingType || !hasBaseType(containingType, enclosingClass) && !isNodeWithinClass(location, getClassLikeDeclarationOfSymbol(getParentOfSymbol(prop)!)!)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the added check is the same one as for private properties a couple of lines above this (in this function)

Copy link
Member

Choose a reason for hiding this comment

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

It does besides the null checking; is it really safe to ! all of these? The other case doesn't.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The other case does exactly the same. I copy-pasted this :p

        // Private property is accessible if the property is within the declaring class
        if (flags & ModifierFlags.Private) {
            const declaringClassDeclaration = getClassLikeDeclarationOfSymbol(getParentOfSymbol(prop)!)!;
            if (!isNodeWithinClass(location, declaringClassDeclaration)) {
                if (errorNode) {
                    error(errorNode, Diagnostics.Property_0_is_private_and_only_accessible_within_class_1, symbolToString(prop), typeToString(getDeclaringClass(prop)!));
                }
                return false;
            }
            return true;
        }

Copy link
Member

Choose a reason for hiding this comment

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

Oh, yeah, I can't read

Copy link
Member

Choose a reason for hiding this comment

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

Why don't we just do this check unconditionally at the top? It sure seems like the result of this code is that "you can access any property of the current class as long as you're within that class", such that we don't need to do all of this runaround.

if (errorNode) {
error(errorNode, Diagnostics.Property_0_is_protected_and_only_accessible_through_an_instance_of_class_1_This_is_an_instance_of_class_2, symbolToString(prop), typeToString(enclosingClass), typeToString(containingType));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//// [tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass2.ts] ////

=== protectedClassPropertyAccessibleWithinNestedSubclass2.ts ===
// https://github.com/microsoft/TypeScript/issues/59989

export class Foo {
>Foo : Symbol(Foo, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 0, 0))

protected thisIsProtected = 1;
>thisIsProtected : Symbol(Foo.thisIsProtected, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 2, 18))

private thisIsPrivate = 1;
>thisIsPrivate : Symbol(Foo.thisIsPrivate, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 3, 32))

bar(): Foo {
>bar : Symbol(Foo.bar, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 4, 28))
>Foo : Symbol(Foo, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 0, 0))

const that = this;
>that : Symbol(that, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 7, 9))
>this : Symbol(Foo, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 0, 0))

return new (class extends Foo {
>Foo : Symbol(Foo, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 0, 0))

something() {
>something : Symbol((Anonymous class).something, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 9, 35))

return that.thisIsPrivate + that.thisIsProtected; // ok
>that.thisIsPrivate : Symbol(Foo.thisIsPrivate, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 3, 32))
>that : Symbol(that, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 7, 9))
>thisIsPrivate : Symbol(Foo.thisIsPrivate, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 3, 32))
>that.thisIsProtected : Symbol(Foo.thisIsProtected, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 2, 18))
>that : Symbol(that, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 7, 9))
>thisIsProtected : Symbol(Foo.thisIsProtected, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 2, 18))
}
})();
}
}

export class Foo2 {
>Foo2 : Symbol(Foo2, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 15, 1))

protected thisIsProtected = 1;
>thisIsProtected : Symbol(Foo2.thisIsProtected, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 17, 19))

private thisIsPrivate = 1;
>thisIsPrivate : Symbol(Foo2.thisIsPrivate, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 18, 32))

bar() {
>bar : Symbol(Foo2.bar, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 19, 28))

const that = this;
>that : Symbol(that, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 22, 9))
>this : Symbol(Foo2, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 15, 1))

return new (class {
something() {
>something : Symbol((Anonymous class).something, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 24, 23))

return that.thisIsPrivate + that.thisIsProtected; // ok
>that.thisIsPrivate : Symbol(Foo2.thisIsPrivate, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 18, 32))
>that : Symbol(that, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 22, 9))
>thisIsPrivate : Symbol(Foo2.thisIsPrivate, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 18, 32))
>that.thisIsProtected : Symbol(Foo2.thisIsProtected, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 17, 19))
>that : Symbol(that, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 22, 9))
>thisIsProtected : Symbol(Foo2.thisIsProtected, Decl(protectedClassPropertyAccessibleWithinNestedSubclass2.ts, 17, 19))
}
})();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
//// [tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass2.ts] ////

=== protectedClassPropertyAccessibleWithinNestedSubclass2.ts ===
// https://github.com/microsoft/TypeScript/issues/59989

export class Foo {
>Foo : Foo
> : ^^^

protected thisIsProtected = 1;
>thisIsProtected : number
> : ^^^^^^
>1 : 1
> : ^

private thisIsPrivate = 1;
>thisIsPrivate : number
> : ^^^^^^
>1 : 1
> : ^

bar(): Foo {
>bar : () => Foo
> : ^^^^^^

const that = this;
>that : this
> : ^^^^
>this : this
> : ^^^^

return new (class extends Foo {
>new (class extends Foo { something() { return that.thisIsPrivate + that.thisIsProtected; // ok } })() : (Anonymous class)
> : ^^^^^^^^^^^^^^^^^
>(class extends Foo { something() { return that.thisIsPrivate + that.thisIsProtected; // ok } }) : typeof (Anonymous class)
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>class extends Foo { something() { return that.thisIsPrivate + that.thisIsProtected; // ok } } : typeof (Anonymous class)
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>Foo : Foo
> : ^^^

something() {
>something : () => number
> : ^^^^^^^^^^^^

return that.thisIsPrivate + that.thisIsProtected; // ok
>that.thisIsPrivate + that.thisIsProtected : number
> : ^^^^^^
>that.thisIsPrivate : number
> : ^^^^^^
>that : this
> : ^^^^
>thisIsPrivate : number
> : ^^^^^^
>that.thisIsProtected : number
> : ^^^^^^
>that : this
> : ^^^^
>thisIsProtected : number
> : ^^^^^^
}
})();
}
}

export class Foo2 {
>Foo2 : Foo2
> : ^^^^

protected thisIsProtected = 1;
>thisIsProtected : number
> : ^^^^^^
>1 : 1
> : ^

private thisIsPrivate = 1;
>thisIsPrivate : number
> : ^^^^^^
>1 : 1
> : ^

bar() {
>bar : () => (Anonymous class)
> : ^^^^^^^^^^^^^^^^^^^^^^^

const that = this;
>that : this
> : ^^^^
>this : this
> : ^^^^

return new (class {
>new (class { something() { return that.thisIsPrivate + that.thisIsProtected; // ok } })() : (Anonymous class)
> : ^^^^^^^^^^^^^^^^^
>(class { something() { return that.thisIsPrivate + that.thisIsProtected; // ok } }) : typeof (Anonymous class)
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>class { something() { return that.thisIsPrivate + that.thisIsProtected; // ok } } : typeof (Anonymous class)
> : ^^^^^^^^^^^^^^^^^^^^^^^^

something() {
>something : () => number
> : ^^^^^^^^^^^^

return that.thisIsPrivate + that.thisIsProtected; // ok
>that.thisIsPrivate + that.thisIsProtected : number
> : ^^^^^^
>that.thisIsPrivate : number
> : ^^^^^^
>that : this
> : ^^^^
>thisIsPrivate : number
> : ^^^^^^
>that.thisIsProtected : number
> : ^^^^^^
>that : this
> : ^^^^
>thisIsProtected : number
> : ^^^^^^
}
})();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// @strict: true
// @noEmit: true

// https://github.com/microsoft/TypeScript/issues/59989

export class Foo {
protected thisIsProtected = 1;
private thisIsPrivate = 1;

bar(): Foo {
const that = this;

return new (class extends Foo {
something() {
return that.thisIsPrivate + that.thisIsProtected; // ok
}
})();
}
}

export class Foo2 {
protected thisIsProtected = 1;
private thisIsPrivate = 1;

bar() {
const that = this;

return new (class {
something() {
return that.thisIsPrivate + that.thisIsProtected; // ok
}
})();
}
}