-
Notifications
You must be signed in to change notification settings - Fork 1
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
Functions defined in nested blocks in sloppy mode not recognised as in scope #224
Comments
There's a further complication. Even in sloppy mode, a function will be bound within its own block if there's an existing var in the scope it would otherwise be hoisted to defined with In these cases, the inner // Sloppy mode
const x = () => 1;
{
{
function x() { return 2; }
console.log(x()); // Logs 2
}
console.log(x()); // Logs 1
} // Sloppy mode
let x = () => 1;
{
{
function x() { return 2; }
console.log(x()); // Logs 2
}
console.log(x()); // Logs 1
} // Sloppy mode
class x {
constructor() { this.y = 1; }
}
{
{
function x() { return 2; }
console.log(x()); // Logs 2
}
console.log(new x().y); // Logs 1
} function outer(x) {
{
function x() { return 2; }
console.log(x()); // Logs 2
}
console.log(x()); // Logs 1
}
outer(() => 1); Whereas in these cases it's hoisted to the outer block: // Sloppy mode
var x = () => 1;
{
{
function x() { return 2; }
console.log(x()); // Logs 2
}
console.log(x()); // Logs 2
} // Sloppy mode
function x() { return 1; }
{
{
function x() { return 2; }
console.log(x()); // Logs 2
}
console.log(x()); // Logs 2
} These observations made on Node v16.10.0. Have not checked behavior on previous Node versions. |
A // Sloppy mode
{
const x = () => 1;
{
function x() { return 2; }
console.log(x()); // Logs 2
}
console.log(x()); // Logs 1
}
console.log(typeof x); // Logs undefined // Sloppy mode
{
let x = () => 1;
{
function x() { return 2; }
console.log(x()); // Logs 2
}
console.log(x()); // Logs 1
}
console.log(typeof x); // Logs undefined // Sloppy mode
{
class x {
constructor() { this.y = 1; }
}
{
function x() { return 2; }
console.log(x()); // Logs 2
}
console.log(new x().y); // Logs 1
}
console.log(typeof x); // Logs undefined In all cases, whether the other var is in TDZ when function is defined makes no difference: // Sloppy mode
{
{
function x() { return 2; }
console.log(x()); // Logs 2
}
const x = () => 1;
console.log(x()); // Logs 1
}
console.log(typeof x); // Logs undefined |
I think fixed in b2a0a09. Needs tests. |
Binding location of functions nested in blocks is dependent on strict/sloppy mode.
Function declarations are bound:
Livepack does not currently handle the sloppy mode case. Babel plugin does not recognise that
f
is in scope of the exported function and sof
does not get captured.This is at least in part down to a bug in Babel. babel/babel#13549
Probably also a bug in Livepack.
The text was updated successfully, but these errors were encountered: