-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Add logical assignment operator #37727
Add logical assignment operator #37727
Conversation
Resolved |
} | ||
|
||
function foo2 (f?: (a: number) => void) { | ||
f ||= (a => a) |
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.
Great test!
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.
😉
if (thing &&= thing.original) { | ||
console.log(thing.name); | ||
~~~~~ | ||
!!! error TS2532: Object is possibly 'undefined'. |
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.
Glad the test caught this - that was part of my intent when I wrote it. As you probably know, this needs to be fixed because thing.original
should be defined. Let @ahejlsberg or @rbuckton know if you need help with that.
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.
I spent all afternoon And evening in this one😂
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.
Yeah, the code can be hard to get through, and I've honestly never wired up the CFG. Also, binary expressions recently changed to use a "trampolining" approach to avoid stack overflows on deep traversals. You can ask @weswigham about that.
If you want to play around to see if you can get it working, I'll provide a few tips and @weswigham and @ahejlsberg can weigh in case I'm totally off.
First take a look at some of the logic at
TypeScript/src/compiler/binder.ts
Line 1418 in b58a29b
const preRightLabel = createBranchLabel(); |
and
TypeScript/src/compiler/binder.ts
Line 1513 in b58a29b
if (operator === SyntaxKind.AmpersandAmpersandToken || operator === SyntaxKind.BarBarToken || operator === SyntaxKind.QuestionQuestionToken) { |
The binder needs to create branch labels for these new compound assignment operators (similar to in bindLogicalExpression
) while creating at least one flow node that's considered an assignment after the expression (as in bindAssignmentTargetFlow
). At a glance, I think that a lot of the logic can be reused and the changes can be added around here
Eventually in getTypeAtFlowNode
in the type-checker, you need to try to find a way to combine the logic when flags & FlowFlags.Assignment
and flags & FlowFlags.Label
both apply. To start, I'd just get something working, then worry about how to share the code.
Feel free to ask some questions if you need help. If you'd prefer, I'm also sure we could send a PR if you'd like, but it's up to you.
src/compiler/utilities.ts
Outdated
@@ -4355,6 +4365,12 @@ namespace ts { | |||
|| token === SyntaxKind.ExclamationToken; | |||
} | |||
|
|||
export function isLogicalAssignmentOperator(token: SyntaxKind): boolean { |
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.
Consider isShortCircuitingAssignmentOperator
or isLogicalOrCoalescingAssignmentOperator
src/compiler/utilities.ts
Outdated
@@ -3334,6 +3340,10 @@ namespace ts { | |||
return 14; | |||
case SyntaxKind.AsteriskAsteriskToken: | |||
return 15; | |||
case SyntaxKind.BarBarEqualsToken: |
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.
This section doesn't seem to cover any of the assignment or compound assignment operators. I'd get clarity from whoever originally wrote this function whether this should be here, because I think these 3 cases don't belong here.
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.
Yeah, it seems to be caught by getOperatorPrecedence
above.
2f6dfd7
to
1c92781
Compare
It seems works, but i don't know why.🤷🏻♂️ |
@typescript-bot pack this |
Heya @DanielRosenwasser, I've started to run the tarball bundle task on this PR at 1c92781. You can monitor the build here. |
Current CFG is
the same as downlevel result
|
@typescript-bot pack this. |
Hey @Kingwl, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running |
17b858c
to
1ed6a3e
Compare
@typescript-bot pack this. |
Hey @Kingwl, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running |
Does the playground not supported pull request build now?🤷🏻♂️ |
@weswigham or @orta might know |
Thanks for the ping, looks like I need to dig into the playground builder - been red for a week |
Made a PR: microsoft/monaco-typescript#59 |
and with orta/make-monaco-builds@f17ca4a in I can request a playground again - @typescript-bot pack this. |
Hey @orta, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build. |
f17eb88
to
39ae072
Compare
Only one pipeline failed with ECONNRESET. |
Looks like this needs to be rebased onto the latest |
ad70d78
to
b59e4c5
Compare
One more review? |
tests/cases/conformance/esnext/logicalAssignment/logicalAssignment2.ts
Outdated
Show resolved
Hide resolved
tests/baselines/reference/logicalAssignment5(target=es2015).errors.txt
Outdated
Show resolved
Hide resolved
One more peek @rbuckton? |
up👆 |
Glad to see things moved since #20378 😀 |
What version of typescript will this be available in? |
@g-patel This shipped in TypeScript 4.0. |
Fixes #37255