Use for..of Object.keys instead of for..in (#9518)

In https://github.com/babel/babel/issues/9511 (and #9495 is another symptom), @PavelKastornyy reported a node crash becaue the JavaScript heap run out of memory. The problem was that their code was adding enumerable properties to `Object.prototype`: it is something that shouldn't be done, but Babel shouldn't make node crash if someone adds them.
I reduced down the problem to `for...in` loops in `@babel/traverse` that grew the memory consumption exponentially because of that unexpected properties.
This commit is contained in:
Nicolò Ribaudo
2019-02-26 20:09:02 +01:00
committed by GitHub
parent f1ab6120d2
commit 0345c1bc1d
24 changed files with 60 additions and 55 deletions

View File

@@ -113,7 +113,7 @@ export default declare((api, options) => {
if (arg.isObjectPattern() || arg.isArrayPattern()) {
const exprs = [path.node];
for (const name in arg.getBindingIdentifiers()) {
for (const name of Object.keys(arg.getBindingIdentifiers())) {
if (this.scope.getBinding(name) !== path.scope.getBinding(name)) {
return;
}
@@ -266,7 +266,7 @@ export default declare((api, options) => {
} else if (path.isImportDeclaration()) {
const source = path.node.source.value;
pushModule(source, "imports", path.node.specifiers);
for (const name in path.getBindingIdentifiers()) {
for (const name of Object.keys(path.getBindingIdentifiers())) {
path.scope.removeBinding(name);
variableIds.push(t.identifier(name));
}
@@ -320,7 +320,9 @@ export default declare((api, options) => {
addExportName(name, name);
path.insertAfter([buildExportCall(name, t.identifier(name))]);
} else {
for (const name in declar.getBindingIdentifiers()) {
for (const name of Object.keys(
declar.getBindingIdentifiers(),
)) {
addExportName(name, name);
}
}