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
Added stub for require.cache #4807
Conversation
|
Looks good to, @mischnic could you have a look to validate the changes to scope-hoisting are done properly? |
if (t.matchesPattern(path.node, 'module.require') && !isCommonJS) { | ||
path.replaceWith(t.identifier('null')); | ||
} | ||
|
||
if (t.matchesPattern(path.node, 'require.cache') && !isCommonJS) { | ||
path.replaceWith(t.objectExpression([])); | ||
} |
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.
{
let module = {
require() {
return 1;
},
};
const x = module.require("fs");
console.log(module, x);
}
Is transformed to ...; var x = null("fs"); ...
, you need && !path.scope.hasBinding("module")
(and same for require
).
Then this should work:
{
function require() {
return 1;
}
require.cache = [1, 2, 3];
let module = {
require,
};
const x = module.require("fs");
console.log(module, module.require("fs"), require.cache[0]);
}
- The following would throw a cryptic error (
Property left of AssignmentExpression expected node to be of a type ["LVal"] but instead got "ObjectExpression"
) even with thehasBinding
check.
{
require.cache = [1, 2, 3];
let module = {
require() {
return 1;
},
};
const x = module.require("fs");
console.log(module, module.require("fs"), require.cache[0]);
}
(And these should probably be integration tests as well)
Currently I'm overwhelmed by stuff at work, closing for now, will be back in a couple of weeks. |
↪️ Pull Request
Fixes: #4616
Following the previous #4621 discussion on the same issue, I've made the changes.
✔️ PR Todo