Skip to content

Fix private properties not being allowed in optional chains #60263

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

Open
wants to merge 2 commits 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
4 changes: 0 additions & 4 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -8322,10 +8322,6 @@
"category": "Error",
"code": 18029
},
"An optional chain cannot contain private identifiers.": {
"category": "Error",
"code": 18030
},
"The intersection '{0}' was reduced to 'never' because property '{1}' has conflicting types in some constituents.": {
"category": "Error",
"code": 18031
Expand Down
4 changes: 0 additions & 4 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ import {
isMetaProperty,
isModifierKind,
isNonNullExpression,
isPrivateIdentifier,
isSetAccessorDeclaration,
isStringOrNumericLiteralLike,
isTaggedTemplateExpression,
Expand Down Expand Up @@ -6405,9 +6404,6 @@ namespace Parser {
const propertyAccess = isOptionalChain ?
factoryCreatePropertyAccessChain(expression, questionDotToken, name) :
factoryCreatePropertyAccessExpression(expression, name);
if (isOptionalChain && isPrivateIdentifier(propertyAccess.name)) {
parseErrorAtRange(propertyAccess.name, Diagnostics.An_optional_chain_cannot_contain_private_identifiers);
}
if (isExpressionWithTypeArguments(expression) && expression.typeArguments) {
const pos = expression.typeArguments.pos - 1;
const end = skipTrivia(sourceText, expression.typeArguments.end) + 1;
Expand Down
28 changes: 0 additions & 28 deletions tests/baselines/reference/privateIdentifierChain.1.errors.txt

This file was deleted.

16 changes: 10 additions & 6 deletions tests/baselines/reference/privateIdentifierChain.1.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ class A {
return new A();
}
constructor() {
this?.#b; // Error
this?.a.#b; // Error
this?.getA().#b; // Error
this.a = this;
// None of these should error
this?.#b;
this?.a.#b;
this?.getA().#b;
}
}

Expand All @@ -23,8 +25,10 @@ class A {
return new A();
}
constructor() {
this?.#b; // Error
this?.a.#b; // Error
this?.getA().#b; // Error
this.a = this;
// None of these should error
this?.#b;
this?.a.#b;
this?.getA().#b;
}
}
13 changes: 10 additions & 3 deletions tests/baselines/reference/privateIdentifierChain.1.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,24 @@ class A {
>A : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))
}
constructor() {
this?.#b; // Error
this.a = this;
>this.a : Symbol(A.a, Decl(privateIdentifierChain.1.ts, 0, 9))
>this : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))
>a : Symbol(A.a, Decl(privateIdentifierChain.1.ts, 0, 9))
>this : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))

// None of these should error
this?.#b;
>this?.#b : Symbol(A.#b, Decl(privateIdentifierChain.1.ts, 1, 9))
>this : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))

this?.a.#b; // Error
this?.a.#b;
>this?.a.#b : Symbol(A.#b, Decl(privateIdentifierChain.1.ts, 1, 9))
>this?.a : Symbol(A.a, Decl(privateIdentifierChain.1.ts, 0, 9))
>this : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))
>a : Symbol(A.a, Decl(privateIdentifierChain.1.ts, 0, 9))

this?.getA().#b; // Error
this?.getA().#b;
>this?.getA().#b : Symbol(A.#b, Decl(privateIdentifierChain.1.ts, 1, 9))
>this?.getA : Symbol(A.getA, Decl(privateIdentifierChain.1.ts, 2, 11))
>this : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))
Expand Down
27 changes: 20 additions & 7 deletions tests/baselines/reference/privateIdentifierChain.1.types
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,36 @@ class A {
> : ^^^^^^^^
}
constructor() {
this?.#b; // Error
this.a = this;
>this.a = this : this
> : ^^^^
>this.a : A | undefined
> : ^^^^^^^^^^^^^
>this : this
> : ^^^^
>a : A | undefined
> : ^^^^^^^^^^^^^
>this : this
> : ^^^^

// None of these should error
this?.#b;
>this?.#b : A | undefined
> : ^^^^^^^^^^^^^
>this : this
> : ^^^^

this?.a.#b; // Error
this?.a.#b;
>this?.a.#b : A | undefined
> : ^^^^^^^^^^^^^
>this?.a : A | undefined
> : ^^^^^^^^^^^^^
>this?.a : A
> : ^
>this : this
> : ^^^^
>a : A | undefined
> : ^^^^^^^^^^^^^
>a : A
> : ^

this?.getA().#b; // Error
this?.getA().#b;
>this?.getA().#b : A | undefined
> : ^^^^^^^^^^^^^
>this?.getA() : A
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@ class C {

#bar;
>#bar : any
> : ^^^

constructor () {
this?.#foo;
>this?.#foo : any
> : ^^^
>this?.#foo : error
>this : this
> : ^^^^

this?.#bar;
>this?.#bar : any
> : ^^^
>this : this
> : ^^^^
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ class A {
return new A();
}
constructor() {
this?.#b; // Error
this?.a.#b; // Error
this?.getA().#b; // Error
this.a = this;
// None of these should error
this?.#b;
this?.a.#b;
this?.getA().#b;
}
}