Skip to content
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

[REPL] Cannot declare variables under strict-mode when used along --experimental-top-level-await #39259

Closed
ejose19 opened this issue Jul 4, 2021 · 5 comments

Comments

@ejose19
Copy link
Contributor

ejose19 commented Jul 4, 2021

  • Version: v16.4.1
  • Platform: Archlinux
  • Subsystem: N/A

What steps will reproduce the bug?

  • run node with: node --use_strict --experimental-repl-await
  • enter: const x = await 1;
  • receive:
Uncaught ReferenceError: x is not defined
    at REPL1:1:24
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

How often does it reproduce? Is there a required condition?

100%

What is the expected behavior?

It shouldn't throw error, or if this is not supported then it should error out before starting repl.

What do you see instead?

Error is thrown

Additional information

This is related to handling in

VariableDeclaration(node, state, c) {
if (node.kind === 'var' ||
state.ancestors[state.ancestors.length - 2] === state.body) {
if (node.declarations.length === 1) {
state.replace(node.start, node.start + node.kind.length, 'void');
} else {
state.replace(node.start, node.start + node.kind.length, 'void (');
}
ArrayPrototypeForEach(node.declarations, (decl) => {
state.prepend(decl, '(');
state.append(decl, decl.init ? ')' : '=undefined)');
});
if (node.declarations.length !== 1) {
state.append(node.declarations[node.declarations.length - 1], ')');
}
}
walk.base.VariableDeclaration(node, state, c);
}
};

as it's replacing const with void ($exp) without any flag to omit if running in strict mode.

This was noticed when trying to implement TLA in repl for ts-node.

@ejose19 ejose19 changed the title Cannot declare variables under strict-mode when used along --experimental-top-level-await [REPL] Cannot declare variables under strict-mode when used along --experimental-top-level-await Jul 4, 2021
@guybedford
Copy link
Contributor

guybedford commented Jul 4, 2021

We should be declaring let and const vars as top-level scope vars not globals. This can be accomplished by hoisting the variable definition outside of the async wrapper, separate from its assignment. In the process we should probably convert const into let, as that is likely the best we can do as well.

@ejose19 is this something you might be able to contribute to here? It is also a blocker for #39142.

@guybedford
Copy link
Contributor

The tests for this are in https://github.com/nodejs/node/blob/master/test/parallel/test-repl-preprocess-top-level-await.js#L62.

The suggested change is the following to the last test case:

  ['let o = await 1, p',
    '(async () => { void ( (o = await 1), (p=undefined)) })()' ],

->

  ['let o = await 1, p',
    'let o, p; (async () => { void ( (o = await 1), (p=undefined)) })()' ],

@ejose19
Copy link
Contributor Author

ejose19 commented Jul 5, 2021

@guybedford Sure, this is also needed for ts-node so I'll try to get it fixed.

@ejose19
Copy link
Contributor Author

ejose19 commented Jul 5, 2021

@guybedford I've submitted a PR, let me know what you think.

@guybedford
Copy link
Contributor

Fixed in #39265.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants