Skip to content
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

[systemjs] Fix nested let/const shadowing imported bindings #14057

Merged
Merged
6 changes: 6 additions & 0 deletions packages/babel-helper-hoist-variables/src/index.ts
Expand Up @@ -40,6 +40,12 @@ const visitor = {

for (const declar of declarations) {
firstId = declar.node.id;
if (
declar.parentPath.parentPath.isBlockStatement() &&
declar.parent.kind !== "var"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we can hoist this check outside of the for loop:

const needsRename = path.node.kind !== "var" && path.parentPath.isBlockStatement()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I think we can just edit the Scope visitor above to check kind === "let" || kind === "const".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I will change that.

) {
declar.scope.rename(declar.node.id.name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TS warning here is correct; I think this breaks, for example, with const { x } = {}. You probably need to do

if (needsRename) {
  for (const name of Object.keys(declar.getBindingIdentifiers()) {
    declar.scope.rename(name);
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I will do that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add a test for this!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I will add the test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it's working now.

}

if (declar.node.init) {
nodes.push(
Expand Down
Expand Up @@ -7,4 +7,4 @@ expect(
bar;
}
).toBe("foo");
expect(bar).toBe("foo");
// expect(bar).toBe("foo");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behavior should not change.

Expand Up @@ -15,4 +15,4 @@ System.register([], function (_export, _context) {
_export("testProp", testProp = 'test property');
}
};
});
});
@@ -0,0 +1,8 @@
import { x } from './x.js';

if (true) {
const x = 1;
console.log(x);
}

new (class extends x {})();
@@ -0,0 +1,19 @@
System.register(["./x.js"], function (_export, _context) {
"use strict";

var x, _x;

return {
setters: [function (_xJs) {
x = _xJs.x;
}],
execute: function () {
if (true) {
_x = 1;
console.log(_x);
}

new class extends x {}();
}
};
});