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

Fixed computed keys for class expression #10029

Merged
merged 6 commits into from May 28, 2019

Conversation

tanhauhau
Copy link
Member

@tanhauhau tanhauhau commented May 26, 2019

Q                       A
Fixed Issues? Fixes #8882
Patch: Bug Fix? 👍
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT
  • used let instead of var for declaring computedKeys variable, so that the value is correctly scoped.
  • declare the let at the top of current block, so that it wont get bailed out when converting the declaration to expression sequence.

@tanhauhau tanhauhau force-pushed the tanhauhau/fix/computed-keys branch from e9cdda3 to e20517e Compare May 26, 2019 17:33
@tanhauhau tanhauhau force-pushed the tanhauhau/fix/computed-keys branch from e20517e to aaf48aa Compare May 26, 2019 17:55
@babel-bot
Copy link
Collaborator

babel-bot commented May 26, 2019

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/10878/

@@ -85,6 +85,9 @@ export function injectInitialization(path, constructor, nodes, renamer) {
export function extractComputedKeys(ref, path, computedPaths, file) {
const declarations = [];

const nearestBlock = path.find(
parentPath => parentPath.isBlockStatement() || parentPath.isProgram(),
Copy link
Member

Choose a reason for hiding this comment

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

Sometimes the variable needs to be inserted where a block doesn't exist. Do we have a test for this code?

() => class { [k] = 2 }

Copy link
Member Author

Choose a reason for hiding this comment

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

@nicolo-ribaudo nicolo-ribaudo added PR: Bug Fix 🐛 A type of pull request used for our changelog categories Spec: Class Fields labels May 27, 2019
@@ -98,10 +98,14 @@ export function extractComputedKeys(ref, path, computedPaths, file) {
const ident = path.scope.generateUidIdentifierBasedOnNode(
computedNode.key,
);
path.scope.push({
id: ident,
kind: "let",
Copy link
Member

Choose a reason for hiding this comment

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

I don't see any advantage in using let here? Usually we try to generate an output as low-level as possible, if it isn't harder to do.

Copy link
Member Author

@tanhauhau tanhauhau May 27, 2019

Choose a reason for hiding this comment

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

It is for this scenario: microsoft/TypeScript#27864

if it is set as var, it will behave the same as pointed out in the issue link above, which will break this newly added test case (https://github.com/babel/babel/pull/10029/files#diff-fbbdd83e7a9c998721c1484529c2ce92)

maybe it should be determined by whether the computedKeys original declaration kind, to preserve the original scoping intention?

Copy link
Member

Choose a reason for hiding this comment

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

Oh ok; it's ok like this then.

Copy link
Member

Choose a reason for hiding this comment

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

Seems like adding a comment explaining this might be useful?

@nicolo-ribaudo nicolo-ribaudo merged commit b4c9cb0 into babel:master May 28, 2019
@nicolo-ribaudo
Copy link
Member

Thank you!

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 3, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Bug Fix 🐛 A type of pull request used for our changelog categories Spec: Class Fields
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid code emitted for computed properties
5 participants