Skip to content

Bind RHS of comma expressions too #47049

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
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 6 additions & 1 deletion src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1365,7 +1365,7 @@ namespace ts {
}

function maybeBindExpressionFlowIfCall(node: Expression) {
// A top level or LHS of comma expression call expression with a dotted function name and at least one argument
// A top level or comma expression call expression with a dotted function name and at least one argument
// is potentially an assertion and is therefore included in the control flow.
if (node.kind === SyntaxKind.CallExpression) {
const call = node as CallExpression;
Expand Down Expand Up @@ -1574,6 +1574,11 @@ namespace ts {
function onExit(node: BinaryExpression, state: WorkArea) {
if (!state.skip) {
const operator = node.operatorToken.kind;

if (operator === SyntaxKind.CommaToken) {
Copy link
Member

Choose a reason for hiding this comment

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

Why would this be in onExit and not onRight?

Copy link
Member Author

@jakebailey jakebailey Dec 7, 2021

Choose a reason for hiding this comment

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

If you look up a bit, the node.left is processed in onOperator, presumably because there's some work that is completed after onLeft is completed. That's how it was added in #44487.

I tried putting it into onRight first and hit infinite recursion, then realized that the other call wasn't in onLeft and moved it down.

Copy link
Member Author

Choose a reason for hiding this comment

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

Perhaps I can get away with moving these around, though, and try running them after maybeBind in onLeft and onRight instead. I'll try it. It'd sure look less weird.

This comment was marked as outdated.

This comment was marked as outdated.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've moved things around to be a little less surprising.

maybeBindExpressionFlowIfCall(node.right);
}

if (isAssignmentOperator(operator) && !isAssignmentTarget(node)) {
bindAssignmentTargetFlow(node.left);
if (operator === SyntaxKind.EqualsToken && node.left.kind === SyntaxKind.ElementAccessExpression) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//// [controlFlowCommaExpressionAssertionMultiple.ts]
function Narrow<T>(value: any): asserts value is T {}

function func(foo: any, bar: any) {
Narrow<number>(foo), Narrow<string>(bar);
foo;
bar;
}

function func2(foo: any, bar: any, baz: any) {
Narrow<number>(foo), Narrow<string>(bar), Narrow<boolean>(baz);
foo;
bar;
baz;
}


//// [controlFlowCommaExpressionAssertionMultiple.js]
function Narrow(value) { }
function func(foo, bar) {
Narrow(foo), Narrow(bar);
foo;
bar;
}
function func2(foo, bar, baz) {
Narrow(foo), Narrow(bar), Narrow(baz);
foo;
bar;
baz;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
=== tests/cases/compiler/controlFlowCommaExpressionAssertionMultiple.ts ===
function Narrow<T>(value: any): asserts value is T {}
>Narrow : Symbol(Narrow, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 0, 0))
>T : Symbol(T, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 0, 16))
>value : Symbol(value, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 0, 19))
>value : Symbol(value, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 0, 19))
>T : Symbol(T, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 0, 16))

function func(foo: any, bar: any) {
>func : Symbol(func, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 0, 53))
>foo : Symbol(foo, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 2, 14))
>bar : Symbol(bar, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 2, 23))

Narrow<number>(foo), Narrow<string>(bar);
>Narrow : Symbol(Narrow, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 0, 0))
>foo : Symbol(foo, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 2, 14))
>Narrow : Symbol(Narrow, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 0, 0))
>bar : Symbol(bar, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 2, 23))

foo;
>foo : Symbol(foo, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 2, 14))

bar;
>bar : Symbol(bar, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 2, 23))
}

function func2(foo: any, bar: any, baz: any) {
>func2 : Symbol(func2, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 6, 1))
>foo : Symbol(foo, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 8, 15))
>bar : Symbol(bar, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 8, 24))
>baz : Symbol(baz, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 8, 34))

Narrow<number>(foo), Narrow<string>(bar), Narrow<boolean>(baz);
>Narrow : Symbol(Narrow, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 0, 0))
>foo : Symbol(foo, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 8, 15))
>Narrow : Symbol(Narrow, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 0, 0))
>bar : Symbol(bar, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 8, 24))
>Narrow : Symbol(Narrow, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 0, 0))
>baz : Symbol(baz, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 8, 34))

foo;
>foo : Symbol(foo, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 8, 15))

bar;
>bar : Symbol(bar, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 8, 24))

baz;
>baz : Symbol(baz, Decl(controlFlowCommaExpressionAssertionMultiple.ts, 8, 34))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
=== tests/cases/compiler/controlFlowCommaExpressionAssertionMultiple.ts ===
function Narrow<T>(value: any): asserts value is T {}
>Narrow : <T>(value: any) => asserts value is T
>value : any

function func(foo: any, bar: any) {
>func : (foo: any, bar: any) => void
>foo : any
>bar : any

Narrow<number>(foo), Narrow<string>(bar);
>Narrow<number>(foo), Narrow<string>(bar) : void
>Narrow<number>(foo) : void
>Narrow : <T>(value: any) => asserts value is T
>foo : any
>Narrow<string>(bar) : void
>Narrow : <T>(value: any) => asserts value is T
>bar : any

foo;
>foo : number

bar;
>bar : string
}

function func2(foo: any, bar: any, baz: any) {
>func2 : (foo: any, bar: any, baz: any) => void
>foo : any
>bar : any
>baz : any

Narrow<number>(foo), Narrow<string>(bar), Narrow<boolean>(baz);
>Narrow<number>(foo), Narrow<string>(bar), Narrow<boolean>(baz) : void
>Narrow<number>(foo), Narrow<string>(bar) : void
>Narrow<number>(foo) : void
>Narrow : <T>(value: any) => asserts value is T
>foo : any
>Narrow<string>(bar) : void
>Narrow : <T>(value: any) => asserts value is T
>bar : any
>Narrow<boolean>(baz) : void
>Narrow : <T>(value: any) => asserts value is T
>baz : any

foo;
>foo : number

bar;
>bar : string

baz;
>baz : boolean
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function Narrow<T>(value: any): asserts value is T {}

function func(foo: any, bar: any) {
Narrow<number>(foo), Narrow<string>(bar);
foo;
bar;
}

function func2(foo: any, bar: any, baz: any) {
Narrow<number>(foo), Narrow<string>(bar), Narrow<boolean>(baz);
foo;
bar;
baz;
}