Skip to content

Allow private symbols to be control flow narrowed #39978

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 5 commits into from
Sep 5, 2020
Merged
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
3 changes: 2 additions & 1 deletion src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,7 @@ namespace ts {
function isNarrowingExpression(expr: Expression): boolean {
switch (expr.kind) {
case SyntaxKind.Identifier:
case SyntaxKind.PrivateIdentifier:
case SyntaxKind.ThisKeyword:
case SyntaxKind.PropertyAccessExpression:
case SyntaxKind.ElementAccessExpression:
Expand All @@ -850,7 +851,7 @@ namespace ts {
}

function isNarrowableReference(expr: Expression): boolean {
return expr.kind === SyntaxKind.Identifier || expr.kind === SyntaxKind.ThisKeyword || expr.kind === SyntaxKind.SuperKeyword ||
return expr.kind === SyntaxKind.Identifier || expr.kind === SyntaxKind.PrivateIdentifier || expr.kind === SyntaxKind.ThisKeyword || expr.kind === SyntaxKind.SuperKeyword ||
(isPropertyAccessExpression(expr) || isNonNullExpression(expr) || isParenthesizedExpression(expr)) && isNarrowableReference(expr.expression) ||
isElementAccessExpression(expr) && isStringOrNumericLiteralLike(expr.argumentExpression) && isNarrowableReference(expr.expression) ||
isAssignmentExpression(expr) && isNarrowableReference(expr.left);
Expand Down
6 changes: 5 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8023,7 +8023,10 @@ namespace ts {
}

function getFlowTypeInConstructor(symbol: Symbol, constructor: ConstructorDeclaration) {
const reference = factory.createPropertyAccessExpression(factory.createThis(), unescapeLeadingUnderscores(symbol.escapedName));
const accessName = startsWith(symbol.escapedName as string, "__#")
? factory.createPrivateIdentifier((symbol.escapedName as string).split("@")[1])
: unescapeLeadingUnderscores(symbol.escapedName);
const reference = factory.createPropertyAccessExpression(factory.createThis(), accessName);
setParent(reference.expression, reference);
setParent(reference, constructor);
reference.flowNode = constructor.returnFlowNode;
Expand Down Expand Up @@ -20232,6 +20235,7 @@ namespace ts {
}
switch (source.kind) {
case SyntaxKind.Identifier:
case SyntaxKind.PrivateIdentifier:
return target.kind === SyntaxKind.Identifier && getResolvedSymbol(<Identifier>source) === getResolvedSymbol(<Identifier>target) ||
(target.kind === SyntaxKind.VariableDeclaration || target.kind === SyntaxKind.BindingElement) &&
getExportSymbolOfValueSymbolIfExported(getResolvedSymbol(<Identifier>source)) === getSymbolOfNode(target);
Expand Down
51 changes: 51 additions & 0 deletions tests/baselines/reference/controlFlowPrivateClassField.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//// [controlFlowPrivateClassField.ts]
class Example {
#test;

constructor(test: number) {
this.#test = test;
}

get test() {
return this.#test
}
}

class Example2 {
#test;

constructor(test: number | undefined) {
this.#test = test;
}

get test() {
if (this.#test) {
return this.#test
}
return 0;
}
}

//// [controlFlowPrivateClassField.js]
"use strict";
class Example {
constructor(test) {
this.#test = test;
}
#test;
get test() {
return this.#test;
}
}
class Example2 {
constructor(test) {
this.#test = test;
}
#test;
get test() {
if (this.#test) {
return this.#test;
}
return 0;
}
}
54 changes: 54 additions & 0 deletions tests/baselines/reference/controlFlowPrivateClassField.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
=== tests/cases/compiler/controlFlowPrivateClassField.ts ===
class Example {
>Example : Symbol(Example, Decl(controlFlowPrivateClassField.ts, 0, 0))

#test;
>#test : Symbol(Example.#test, Decl(controlFlowPrivateClassField.ts, 0, 15))

constructor(test: number) {
>test : Symbol(test, Decl(controlFlowPrivateClassField.ts, 3, 16))

this.#test = test;
>this.#test : Symbol(Example.#test, Decl(controlFlowPrivateClassField.ts, 0, 15))
>this : Symbol(Example, Decl(controlFlowPrivateClassField.ts, 0, 0))
>test : Symbol(test, Decl(controlFlowPrivateClassField.ts, 3, 16))
}

get test() {
>test : Symbol(Example.test, Decl(controlFlowPrivateClassField.ts, 5, 5))

return this.#test
>this.#test : Symbol(Example.#test, Decl(controlFlowPrivateClassField.ts, 0, 15))
>this : Symbol(Example, Decl(controlFlowPrivateClassField.ts, 0, 0))
}
}

class Example2 {
>Example2 : Symbol(Example2, Decl(controlFlowPrivateClassField.ts, 10, 1))

#test;
>#test : Symbol(Example2.#test, Decl(controlFlowPrivateClassField.ts, 12, 16))

constructor(test: number | undefined) {
>test : Symbol(test, Decl(controlFlowPrivateClassField.ts, 15, 16))

this.#test = test;
>this.#test : Symbol(Example2.#test, Decl(controlFlowPrivateClassField.ts, 12, 16))
>this : Symbol(Example2, Decl(controlFlowPrivateClassField.ts, 10, 1))
>test : Symbol(test, Decl(controlFlowPrivateClassField.ts, 15, 16))
}

get test() {
>test : Symbol(Example2.test, Decl(controlFlowPrivateClassField.ts, 17, 5))

if (this.#test) {
>this.#test : Symbol(Example2.#test, Decl(controlFlowPrivateClassField.ts, 12, 16))
>this : Symbol(Example2, Decl(controlFlowPrivateClassField.ts, 10, 1))

return this.#test
>this.#test : Symbol(Example2.#test, Decl(controlFlowPrivateClassField.ts, 12, 16))
>this : Symbol(Example2, Decl(controlFlowPrivateClassField.ts, 10, 1))
}
return 0;
}
}
57 changes: 57 additions & 0 deletions tests/baselines/reference/controlFlowPrivateClassField.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
=== tests/cases/compiler/controlFlowPrivateClassField.ts ===
class Example {
>Example : Example

#test;
>#test : number

constructor(test: number) {
>test : number

this.#test = test;
>this.#test = test : number
>this.#test : number
>this : this
>test : number
}

get test() {
>test : number

return this.#test
>this.#test : number
>this : this
}
}

class Example2 {
>Example2 : Example2

#test;
>#test : number | undefined

constructor(test: number | undefined) {
>test : number | undefined

this.#test = test;
>this.#test = test : number | undefined
>this.#test : number | undefined
>this : this
>test : number | undefined
}

get test() {
>test : number

if (this.#test) {
>this.#test : number | undefined
>this : this

return this.#test
>this.#test : number
>this : this
}
return 0;
>0 : 0
}
}
28 changes: 28 additions & 0 deletions tests/cases/compiler/controlFlowPrivateClassField.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// @strict: true
// @target: esnext
class Example {
#test;

constructor(test: number) {
this.#test = test;
}

get test() {
return this.#test
}
}

class Example2 {
#test;

constructor(test: number | undefined) {
this.#test = test;
}

get test() {
if (this.#test) {
return this.#test
}
return 0;
}
}