-
Notifications
You must be signed in to change notification settings - Fork 13k
Fix private identifier fields generating errors in class expression declarations #62155
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
49bfe02
Initial plan
Copilot d30b0df
Fix private fields appearing in class expression type literals
Copilot 95a3539
Update baselines and format fixes for private field declaration fix
Copilot b8ca24c
Apply @weswigham's suggestion: Generate errors for private fields in …
Copilot d3ed886
Merge branch 'main' into copilot/fix-62153
weswigham 2543e40
Update Baselines, Applied Lint Fixes, and/or Formatted
typescript-bot 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 hidden or 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
31 changes: 31 additions & 0 deletions
31
tests/baselines/reference/privateFieldsInClassExpressionDeclaration.errors.txt
This file contains hidden or 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,31 @@ | ||
privateFieldsInClassExpressionDeclaration.ts(1,14): error TS4094: Property '#context' of exported anonymous class type may not be private or protected. | ||
privateFieldsInClassExpressionDeclaration.ts(1,14): error TS4094: Property '#method' of exported anonymous class type may not be private or protected. | ||
privateFieldsInClassExpressionDeclaration.ts(8,14): error TS4094: Property '#instancePrivate' of exported anonymous class type may not be private or protected. | ||
privateFieldsInClassExpressionDeclaration.ts(8,14): error TS4094: Property '#staticPrivate' of exported anonymous class type may not be private or protected. | ||
|
||
|
||
==== privateFieldsInClassExpressionDeclaration.ts (4 errors) ==== | ||
export const ClassExpression = class { | ||
~~~~~~~~~~~~~~~ | ||
!!! error TS4094: Property '#context' of exported anonymous class type may not be private or protected. | ||
!!! related TS9027 privateFieldsInClassExpressionDeclaration.ts:1:14: Add a type annotation to the variable ClassExpression. | ||
~~~~~~~~~~~~~~~ | ||
!!! error TS4094: Property '#method' of exported anonymous class type may not be private or protected. | ||
!!! related TS9027 privateFieldsInClassExpressionDeclaration.ts:1:14: Add a type annotation to the variable ClassExpression. | ||
#context = 0; | ||
#method() { return 42; } | ||
public value = 1; | ||
}; | ||
|
||
// Additional test with static private fields | ||
export const ClassExpressionStatic = class { | ||
~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS4094: Property '#instancePrivate' of exported anonymous class type may not be private or protected. | ||
!!! related TS9027 privateFieldsInClassExpressionDeclaration.ts:8:14: Add a type annotation to the variable ClassExpressionStatic. | ||
~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS4094: Property '#staticPrivate' of exported anonymous class type may not be private or protected. | ||
!!! related TS9027 privateFieldsInClassExpressionDeclaration.ts:8:14: Add a type annotation to the variable ClassExpressionStatic. | ||
static #staticPrivate = "hidden"; | ||
#instancePrivate = true; | ||
public exposed = "visible"; | ||
}; |
44 changes: 44 additions & 0 deletions
44
tests/baselines/reference/privateFieldsInClassExpressionDeclaration.js
This file contains hidden or 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,44 @@ | ||
//// [tests/cases/compiler/privateFieldsInClassExpressionDeclaration.ts] //// | ||
|
||
//// [privateFieldsInClassExpressionDeclaration.ts] | ||
export const ClassExpression = class { | ||
#context = 0; | ||
#method() { return 42; } | ||
public value = 1; | ||
}; | ||
|
||
// Additional test with static private fields | ||
export const ClassExpressionStatic = class { | ||
static #staticPrivate = "hidden"; | ||
#instancePrivate = true; | ||
public exposed = "visible"; | ||
}; | ||
|
||
//// [privateFieldsInClassExpressionDeclaration.js] | ||
var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) { | ||
if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : ""; | ||
return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name }); | ||
}; | ||
var _instances, _context, _method, _a, _b, _ClassExpressionStatic_staticPrivate, _ClassExpressionStatic_instancePrivate; | ||
export const ClassExpression = (_a = class { | ||
constructor() { | ||
_instances.add(this); | ||
_context.set(this, 0); | ||
this.value = 1; | ||
} | ||
}, | ||
_context = new WeakMap(), | ||
_instances = new WeakSet(), | ||
_method = function _method() { return 42; }, | ||
_a); | ||
// Additional test with static private fields | ||
export const ClassExpressionStatic = (_b = class { | ||
constructor() { | ||
_ClassExpressionStatic_instancePrivate.set(this, true); | ||
this.exposed = "visible"; | ||
} | ||
}, | ||
_ClassExpressionStatic_instancePrivate = new WeakMap(), | ||
__setFunctionName(_b, "ClassExpressionStatic"), | ||
_ClassExpressionStatic_staticPrivate = { value: "hidden" }, | ||
_b); |
31 changes: 31 additions & 0 deletions
31
tests/baselines/reference/privateFieldsInClassExpressionDeclaration.symbols
This file contains hidden or 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,31 @@ | ||
//// [tests/cases/compiler/privateFieldsInClassExpressionDeclaration.ts] //// | ||
|
||
=== privateFieldsInClassExpressionDeclaration.ts === | ||
export const ClassExpression = class { | ||
>ClassExpression : Symbol(ClassExpression, Decl(privateFieldsInClassExpressionDeclaration.ts, 0, 12)) | ||
|
||
#context = 0; | ||
>#context : Symbol(ClassExpression.#context, Decl(privateFieldsInClassExpressionDeclaration.ts, 0, 38)) | ||
|
||
#method() { return 42; } | ||
>#method : Symbol(ClassExpression.#method, Decl(privateFieldsInClassExpressionDeclaration.ts, 1, 17)) | ||
|
||
public value = 1; | ||
>value : Symbol(ClassExpression.value, Decl(privateFieldsInClassExpressionDeclaration.ts, 2, 28)) | ||
|
||
}; | ||
|
||
// Additional test with static private fields | ||
export const ClassExpressionStatic = class { | ||
>ClassExpressionStatic : Symbol(ClassExpressionStatic, Decl(privateFieldsInClassExpressionDeclaration.ts, 7, 12)) | ||
|
||
static #staticPrivate = "hidden"; | ||
>#staticPrivate : Symbol(ClassExpressionStatic.#staticPrivate, Decl(privateFieldsInClassExpressionDeclaration.ts, 7, 44)) | ||
|
||
#instancePrivate = true; | ||
>#instancePrivate : Symbol(ClassExpressionStatic.#instancePrivate, Decl(privateFieldsInClassExpressionDeclaration.ts, 8, 37)) | ||
|
||
public exposed = "visible"; | ||
>exposed : Symbol(ClassExpressionStatic.exposed, Decl(privateFieldsInClassExpressionDeclaration.ts, 9, 28)) | ||
|
||
}; |
55 changes: 55 additions & 0 deletions
55
tests/baselines/reference/privateFieldsInClassExpressionDeclaration.types
This file contains hidden or 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,55 @@ | ||
//// [tests/cases/compiler/privateFieldsInClassExpressionDeclaration.ts] //// | ||
|
||
=== privateFieldsInClassExpressionDeclaration.ts === | ||
export const ClassExpression = class { | ||
>ClassExpression : typeof ClassExpression | ||
> : ^^^^^^^^^^^^^^^^^^^^^^ | ||
>class { #context = 0; #method() { return 42; } public value = 1;} : typeof ClassExpression | ||
> : ^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
#context = 0; | ||
>#context : number | ||
> : ^^^^^^ | ||
>0 : 0 | ||
> : ^ | ||
|
||
#method() { return 42; } | ||
>#method : () => number | ||
> : ^^^^^^^^^^^^ | ||
>42 : 42 | ||
> : ^^ | ||
|
||
public value = 1; | ||
>value : number | ||
> : ^^^^^^ | ||
>1 : 1 | ||
> : ^ | ||
|
||
}; | ||
|
||
// Additional test with static private fields | ||
export const ClassExpressionStatic = class { | ||
>ClassExpressionStatic : typeof ClassExpressionStatic | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
>class { static #staticPrivate = "hidden"; #instancePrivate = true; public exposed = "visible";} : typeof ClassExpressionStatic | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
static #staticPrivate = "hidden"; | ||
>#staticPrivate : string | ||
> : ^^^^^^ | ||
>"hidden" : "hidden" | ||
> : ^^^^^^^^ | ||
|
||
#instancePrivate = true; | ||
>#instancePrivate : boolean | ||
> : ^^^^^^^ | ||
>true : true | ||
> : ^^^^ | ||
|
||
public exposed = "visible"; | ||
>exposed : string | ||
> : ^^^^^^ | ||
>"visible" : "visible" | ||
> : ^^^^^^^^^ | ||
|
||
}; |
15 changes: 15 additions & 0 deletions
15
tests/cases/compiler/privateFieldsInClassExpressionDeclaration.ts
This file contains hidden or 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,15 @@ | ||
// @target: ES2015 | ||
// @declaration: true | ||
|
||
export const ClassExpression = class { | ||
#context = 0; | ||
#method() { return 42; } | ||
public value = 1; | ||
}; | ||
|
||
// Additional test with static private fields | ||
export const ClassExpressionStatic = class { | ||
static #staticPrivate = "hidden"; | ||
#instancePrivate = true; | ||
public exposed = "visible"; | ||
}; |
Oops, something went wrong.
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.
Nope. This should be an error here, like a
private
. This is also much more verbose than needed. Try this instead: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.
Applied your suggestion! The implementation now uses
isPrivateIdentifierSymbol
and generates the same error as traditional private/protected fields instead of silently skipping them. This makes the behavior much more consistent and provides proper error reporting. Commit: b8ca24c