Skip to content

fix(replaceWith): _removeFromScope first if necessary#14883

Open
jedwards1211 wants to merge 1 commit intobabel:mainfrom
jedwards1211:fix-replaceWith-scope
Open

fix(replaceWith): _removeFromScope first if necessary#14883
jedwards1211 wants to merge 1 commit intobabel:mainfrom
jedwards1211:fix-replaceWith-scope

Conversation

@jedwards1211
Copy link
Contributor

@jedwards1211 jedwards1211 commented Aug 26, 2022

fix #14881

Q                       A
Fixed Issues? Fixes #14881
Patch: Bug Fix? Yes
Major: Breaking Change? No
Minor: New Feature? No
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes? No
License MIT

In replaceWith, I first _removeFromScope() (unless in noScope mode) so that dangling bindings won't be left over (and potentially conflict with the replacement).

We'll need to make the same change to other replacement methods, I suspect

@babel-bot
Copy link
Collaborator

babel-bot commented Aug 26, 2022

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

@jedwards1211 jedwards1211 force-pushed the fix-replaceWith-scope branch 2 times, most recently from 38fc929 to aed6ab3 Compare August 26, 2022 06:28
@liuxingbaoyu
Copy link
Member

CI errors are related.
This fix might be a little hard to do, as the current _removeFromScope also has issues, I have an open PR but it hasn't been updated in a long time, I think I can probably go ahead with it.

@jedwards1211
Copy link
Contributor Author

jedwards1211 commented Aug 26, 2022

@liuxingbaoyu what are the current issues with _removeFromScope? I looked through that whole code path and it seems pretty straightforward.

One problem I see with my current PR is assertions in _replaceWith could fail after I've removed the scope bindings. (_replaceWith seems to have no reason for separate existence; it's only called by replaceWith according to TS...)

Do you have any hunch why removing bindings from the scope is causing regressions like this? I would think the extraneous variable _temp would get generated if a binding is dangling, not the other way around...

    - Expected
    + Received
    
    @@ -1,9 +1,9 @@
      function classFactory() {
    -   var _class, _foo, _bar;
    +   var _class, _foo, _bar, _temp;
    
    -   return _foo = /*#__PURE__*/babelHelpers.classPrivateFieldLooseKey("foo"), _bar = /*#__PURE__*/babelHelpers.classPrivateFieldLooseKey("bar"), (_class = class Foo {
    +   return _foo = /*#__PURE__*/babelHelpers.classPrivateFieldLooseKey("foo"), _bar = /*#__PURE__*/babelHelpers.classPrivateFieldLooseKey("bar"), (_temp = _class = class Foo {

@liuxingbaoyu
Copy link
Member

#14430
Perhaps because _removeFromScope does not perform reference analysis, it will cause the Binding to be removed unexpectedly or not removed.

@jedwards1211
Copy link
Contributor Author

Yeah I was just going through in the debugger and saw with my own eyes how getBindingIdentifiers() was returning the lhs of an AssignmentExpression 👍

@jedwards1211
Copy link
Contributor Author

I'm surprised the dangling bindings after replacement apparently aren't causing loads of other problems

@liuxingbaoyu
Copy link
Member

There are some reports of this, but most circumvent the problem by doing it manually.
It might be harder to fix it at the root, so I've been wanting to fix it, but haven't actually worked on it.

@liuxingbaoyu liuxingbaoyu force-pushed the fix-replaceWith-scope branch from aed6ab3 to 6579dee Compare July 12, 2024 18:04
@liuxingbaoyu liuxingbaoyu added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: traverse (scope) labels Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: traverse (scope) PR: Bug Fix 🐛 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: replaceWith doesn't remove any replaced name bindings from scope

3 participants