Skip to content
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);
}
Comment on lines -6408 to -6410
Copy link
Member

Choose a reason for hiding this comment

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

I suspect we need sort of version check to verify that the syntax is available to the syntax we're emitting, like a new grammar check in the checker to verify this.

Unless you're saying that this syntax has always been allowed since feature was standardized?

if (isExpressionWithTypeArguments(expression) && expression.typeArguments) {
const pos = expression.typeArguments.pos - 1;
const end = skipTrivia(sourceText, expression.typeArguments.end) + 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
privateIdentifierChain.1.ts(23,17): error TS18013: Property '#b' is not accessible outside class 'A' because it has a private identifier.
privateIdentifierChain.1.ts(24,17): error TS18013: Property '#b' is not accessible outside class 'A' because it has a private identifier.
privateIdentifierChain.1.ts(25,22): error TS18013: Property '#b' is not accessible outside class 'A' because it has a private identifier.


==== privateIdentifierChain.1.ts (3 errors) ====
class A {
a?: A
#b?: A;
getA(): A {
return new A();
}
constructor() {
this.a = this;
// None of these should error
this?.#b;
this?.a.#b;
this?.getA().#b;
}
}

class B {
a?: A
getA(): A {
return new A();
}
constructor() {
this.a = new A();
this.a?.#b; // Error
~~
!!! error TS18013: Property '#b' is not accessible outside class 'A' because it has a private identifier.
this?.a.#b; // Error
~~
!!! error TS18013: Property '#b' is not accessible outside class 'A' because it has a private identifier.
this?.getA().#b; // Error
~~
!!! error TS18013: Property '#b' is not accessible outside class 'A' because it has a private identifier.
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//// [tests/cases/conformance/expressions/optionalChaining/privateIdentifierChain/privateIdentifierChain.1.ts] ////

//// [privateIdentifierChain.1.ts]
class A {
a?: A
#b?: A;
getA(): A {
return new A();
}
constructor() {
this.a = this;
// None of these should error
this?.#b;
this?.a.#b;
this?.getA().#b;
}
}

class B {
a?: A
getA(): A {
return new A();
}
constructor() {
this.a = new A();
this.a?.#b; // Error
this?.a.#b; // Error
this?.getA().#b; // Error
}
}


//// [privateIdentifierChain.1.js]
"use strict";
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _A_b;
class A {
getA() {
return new A();
}
constructor() {
_A_b.set(this, void 0);
this.a = this;
// None of these should error
__classPrivateFieldGet(this, _A_b, "f");
__classPrivateFieldGet(this === null || this === void 0 ? void 0 : this.a, _A_b, "f");
__classPrivateFieldGet(this === null || this === void 0 ? void 0 : this.getA(), _A_b, "f");
}
}
_A_b = new WeakMap();
class B {
getA() {
return new A();
}
constructor() {
var _a;
this.a = new A();
(_a = this.a) === null || _a === void 0 ? void 0 : _a.; // Error
this === null || this === void 0 ? void 0 : this.a.; // Error
this === null || this === void 0 ? void 0 : this.getA().; // Error
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//// [tests/cases/conformance/expressions/optionalChaining/privateIdentifierChain/privateIdentifierChain.1.ts] ////

=== privateIdentifierChain.1.ts ===
class A {
>A : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))

a?: A
>a : Symbol(A.a, Decl(privateIdentifierChain.1.ts, 0, 9))
>A : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))

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

getA(): A {
>getA : Symbol(A.getA, Decl(privateIdentifierChain.1.ts, 2, 11))
>A : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))

return new A();
>A : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))
}
constructor() {
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;
>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;
>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))
>getA : Symbol(A.getA, Decl(privateIdentifierChain.1.ts, 2, 11))
}
}

class B {
>B : Symbol(B, Decl(privateIdentifierChain.1.ts, 13, 1))

a?: A
>a : Symbol(B.a, Decl(privateIdentifierChain.1.ts, 15, 9))
>A : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))

getA(): A {
>getA : Symbol(B.getA, Decl(privateIdentifierChain.1.ts, 16, 9))
>A : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))

return new A();
>A : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))
}
constructor() {
this.a = new A();
>this.a : Symbol(B.a, Decl(privateIdentifierChain.1.ts, 15, 9))
>this : Symbol(B, Decl(privateIdentifierChain.1.ts, 13, 1))
>a : Symbol(B.a, Decl(privateIdentifierChain.1.ts, 15, 9))
>A : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))

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

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

this?.getA().#b; // Error
>this?.getA : Symbol(B.getA, Decl(privateIdentifierChain.1.ts, 16, 9))
>this : Symbol(B, Decl(privateIdentifierChain.1.ts, 13, 1))
>getA : Symbol(B.getA, Decl(privateIdentifierChain.1.ts, 16, 9))
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
//// [tests/cases/conformance/expressions/optionalChaining/privateIdentifierChain/privateIdentifierChain.1.ts] ////

=== privateIdentifierChain.1.ts ===
class A {
>A : A
> : ^

a?: A
>a : A | undefined
> : ^^^^^^^^^^^^^

#b?: A;
>#b : A | undefined
> : ^^^^^^^^^^^^^

getA(): A {
>getA : () => A
> : ^^^^^^

return new A();
>new A() : A
> : ^
>A : typeof A
> : ^^^^^^^^
}
constructor() {
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;
>this?.a.#b : A | undefined
> : ^^^^^^^^^^^^^
>this?.a : A
> : ^
>this : this
> : ^^^^
>a : A
> : ^

this?.getA().#b;
>this?.getA().#b : A | undefined
> : ^^^^^^^^^^^^^
>this?.getA() : A
> : ^
>this?.getA : () => A
> : ^^^^^^
>this : this
> : ^^^^
>getA : () => A
> : ^^^^^^
}
}

class B {
>B : B
> : ^

a?: A
>a : A | undefined
> : ^^^^^^^^^^^^^

getA(): A {
>getA : () => A
> : ^^^^^^

return new A();
>new A() : A
> : ^
>A : typeof A
> : ^^^^^^^^
}
constructor() {
this.a = new A();
>this.a = new A() : A
> : ^
>this.a : A | undefined
> : ^^^^^^^^^^^^^
>this : this
> : ^^^^
>a : A | undefined
> : ^^^^^^^^^^^^^
>new A() : A
> : ^
>A : typeof A
> : ^^^^^^^^

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

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

this?.getA().#b; // Error
>this?.getA().#b : any
> : ^^^
>this?.getA() : A
> : ^
>this?.getA : () => A
> : ^^^^^^
>this : this
> : ^^^^
>getA : () => A
> : ^^^^^^
}
}

Loading
Loading