gnode
gnode copied to clipboard
Scope issue for function expressions
There's a weird issue I'm trying to resolve that shows up when dealing with a hoisted function declaration vs a function expression. I've trimmed my code down to the bare minimum to reproduce the problem.
index.js
require('gnode');
var fn = require('./es6');
fn().next();
es6.js
module.exports = function *named() {};
When executing node index.js
(using node 0.11+ without the --harmony-generators
flag) I get the following error:
/Users/dominic/Desktop/test/es6.js:8
}, named, this);
^
ReferenceError: named is not defined
at named (/Users/dominic/Desktop/test/es6.js:8:6)
at Object.<anonymous> (/Users/dominic/Desktop/test/index.js:3:1)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
Changing es6.js
to the following fixes the issue:
module.exports = named;
function *named() {}
So, it appears like there's some sort of weird scope issue regarding the exported function. Oddly enough, using a name with 2 or fewer characters works regardless.
This could be an issue with regenerator itself, but for some reason I can't reproduce this bug using regenerator directly, only when using require('gnode');
.
fwiw, I came across this while investigating duojs/duo#433