-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
Andarist
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
Andarist:fix/protected-members-within-class
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
tests/baselines/reference/protectedClassPropertyAccessibleWithinNestedSubclass2.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
})(); | ||
} | ||
} | ||
|
123 changes: 123 additions & 0 deletions
123
tests/baselines/reference/protectedClassPropertyAccessibleWithinNestedSubclass2.types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
> : ^^^^^^ | ||
} | ||
})(); | ||
} | ||
} | ||
|
34 changes: 34 additions & 0 deletions
34
...ce/classes/members/accessibility/protectedClassPropertyAccessibleWithinNestedSubclass2.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
})(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.