Skip to content

fix each block destructuring can't have default values refer to destructured params #5230

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

Closed
wants to merge 3 commits into from

Conversation

habibrosyad
Copy link
Contributor

Fix #5066

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR relates to an outstanding issue, so please reference it in your PR, or create an explanatory one for discussion. In many cases, features are absent for a reason.
  • This message body should clearly illustrate what problems it solves. If there are related issues, remember to reference them.
  • Ideally, include a test that fails without this PR but passes with it. PRs will only be merged once they pass CI. (Remember to npm run lint!)
    This REPL as mentioned in {#each} block destructuring can't have default values refer to destructured params #5066 failed without this PR.

Tests

  • Run the tests with npm test or yarn test)

@habibrosyad habibrosyad changed the title fix each block destructuring can't have default values referred to destructured params fix each block destructuring can't have default values refer to destructured params Aug 3, 2020
const n = contexts.length;

unpack_destructuring(contexts, value.left, node => {
const alternate = JSON.parse(JSON.stringify(value.right));
Copy link
Member

Choose a reason for hiding this comment

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

why cloning the node?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because the node will be altered in update_reference and I cannot get it right without cloning it. I noticed that modifier will be called several times in the process, if the node changes during the process (as value.right is basically a closure to modifier), then the conditions for finding reference in update_reference will be failed as the node has been replaced for the next call.

update_reference(contexts, node, current_node, 'callee');
} else if (current_node.type === 'MemberExpression') {
update_reference(contexts, node, current_node, 'object');
update_reference(contexts, node, current_node, 'property');
Copy link
Member

Choose a reason for hiding this comment

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

I dont think you would want to update reference for MemberExpression#property,

you might be updating the reference of foo in a.foo below.

{#each arrays as { foo, bar = a.foo } }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah, you're right, missed that one...


if (!current_node || !contexts.length) return;

if (current_node.type === 'Identifier') {
Copy link
Member

Choose a reason for hiding this comment

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

probably can use estree-walker to traverse down the node.

Copy link
Member

Choose a reason for hiding this comment

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

you can use is-reference to determine whether the Identifier is a reference or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will try to use estree-walker, I've seen it to be used in some codes there but hesitated since doing recursive looks more compelling...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@Conduitry
Copy link
Member

Thanks for the PR, and sorry it took so long to get in! I'm closing this as these changes were incorporated into #5986, which has now been merged and released in 3.33.0.

@Conduitry Conduitry closed this Feb 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

{#each} block destructuring can't have default values refer to destructured params
4 participants