-
-
Notifications
You must be signed in to change notification settings - Fork 536
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
feat: add REPL top level await support #1383
Conversation
@cspotcode Could you please review if this is the right approach? |
154c118
to
ec5df21
Compare
Ok, upon inspecting I know why issue 1 was happening, it's due: Lines 233 to 235 in 5643ad6
For TLA, where the code it's wrapped in async IIFE, a single await cannot be exec without its wrapper or it will be return "await is only valid in async functions and the top level bodies of modules" error. So what I adjusted is to For 2nd issue, I've checked the output (compiled) code: await 1;
const x = await 2; output: "use strict";
(async () => {
await 1;
void (x = await 2);
})(); this is a bit more puzzling, I don't know why the compiler is removing the EDIT: I found the cause with 2nd issue, and it's an upstream nodejs bug, it can be reproduced with: node --use_strict --experimental-repl-await
> const x = await 1;
Uncaught ReferenceError: x is not defined
at REPL1:1:24
at processTicksAndRejections (node:internal/process/task_queues:96:5) Problematic code is here: Since |
c451326
to
76437f0
Compare
This PR is now in a fully working state, but we may want until nodejs/node#39265 is merged upstream since this PR is using the same fix. Meanwhile, it's still needed to decide how to enable this feature (i'm leaning more towards a Also, I'd like to add this test suite https://github.com/nodejs/node/blob/master/test/parallel/test-repl-preprocess-top-level-await.js, but don't know where exactly should I add it. |
7a70799
to
435c4fc
Compare
Since there are many changes in upstream node PR, I'll mark this as draft until it's merged, so I can update this PR based on final version. |
435c4fc
to
c0c979e
Compare
@ejose19 thanks for writing this! I've been on vacation so it will be a few more days till I have time to review this. If node has an upstream bugfix, I would say it's ok for us to release a version of ts-node with that bugfix ahead of node. |
Codecov Report
|
You said that node enables it via
How long will it take to run? Will it slow down our tests much? All our tests are declared in Our tests currently pack |
Yes, if that's the case it should be documented that the flag doesn't rely on node own TLA mechanism, but instead a based-mechanism, also I think an alias would be very useful since TLA has a lot of relevancy when used in REPL (as writting
If we're upstreaming the file, I doubt the tests will be relevant anymore, as at most we only will be changing imports. However these tests are very brief, doubt we will have any slowdown if including that and this other relevant test suite. |
Ok, yeah. I have been putting the word We can call this flag EDIT: the node flag is
I'll leave it up to you whether or not you want to add them to our test suite. I see what you mean, they should be fast to run. Are you thinking you can copy-paste them into |
We should keep in mind that This is one of the reasons I feel good keeping the feature labelled as "experimental" so that we can make breaking changes to it in non-major releases. |
I mean, I'm ok with just |
You're talking about the difference between |
Yes, this one, after all await usage in |
Sure, I agree to remove diagnostics that are not specific to TLA (like that one), could be done inside |
Let me know if I should take care of remaining issues, so we don't conflict on changes. |
…de's runtime effectively does the same thing
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.
Same way to suppress output as "use strict"
.
Sounds good. I'm still pushing changes; I'll tell you when I'm done. |
Co-authored-by: ejose19 <8742215+ejose19@users.noreply.github.com>
For the test that is failing with: |
This should be covered by
I'm done for today. Feel free to push fixes for those issues. |
Not right now, as EDIT: I've opted for the 2nd option, as we're only interested in doing this for interactive repl due TLA support (no longer need to set |
I have finally merged this. Thanks again for the great work on this pull request @ejose19 ! |
@cspotcode You're welcome! Glad I could contribute back to this great project, and thanks for your time to review and co-author this PR |
This is the PR of the decade! Thanks to everyone involved. |
EDIT from @cspotcode:
linking to the issues being closed by this PR
Implements and closes #245
For my records, was added to node here
Opt-out flag documented here
This is a port of node own REPL top level await mechanism, however it's still not fully working:
SyntaxError: await is only valid in async functions and the top level bodies of modules
even if the second statement is not using await, example:Other notes:
compile
comment, it's necessary to decide if this functionability should be enabled using ats-node
flag or check if node--experimental-repl-await
is set