You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Badly hoisted variable declaration on tslib import #28849
export default async function foo() {
const out = [];
for await (const item of [1, 2]) {
out.push(item);
}
return out;
}
foo().then(console.log);
Expected behavior:
TS places variable declaration at correct scope.
Actual behavior:
Variable _b is used in a sibling scope, although it was not declared in any parent scope. This only happens when importHelpers is enabled. When it's disabled, the variable will be declared at the correct scope.
import * as tslib_1 from "tslib";
export default async function foo() {
var e_1, _a;
const out = [];
try {
// _b is declared in wrong scope, because it is later used inside
// the finally block
for (var _b = tslib_1.__asyncValues([1, 2]), _c; _c = await _b.next(), !_c.done;) {
const item = _c.value;
out.push(item);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) await _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
return out;
}
foo().then(console.log);
Because this is declared as a var, the declaration is hoisted to the top of the function and should continue to work fine. Is there a reason this emit is causing an issue?
I've stumbled on this via a babel plugin. Instead of telling each plugin which runs into this issues that they need to implement bar hoisting themselves is not so nice. I figured that it's a lot less work for the ecosystem as a whole, when it's fixed here.
Activity
rbuckton commentedon Jan 15, 2019
Because this is declared as a
var, the declaration is hoisted to the top of the function and should continue to work fine. Is there a reason this emit is causing an issue?Jessidhia commentedon Jan 15, 2019
Blind guess: Google Closure Compiler is complaining about it
marvinhagemeister commentedon Jan 15, 2019
I've stumbled on this via a babel plugin. Instead of telling each plugin which runs into this issues that they need to implement bar hoisting themselves is not so nice. I figured that it's a lot less work for the ecosystem as a whole, when it's fixed here.
RyanCavanaugh commentedon Jul 24, 2025
This doesn't seem to be very disruptive, and isn't wrong.