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

Never hoist JSX elts referencing vars from the current scope #14536

Merged
merged 1 commit into from May 9, 2022

Conversation

nicolo-ribaudo
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented May 6, 2022

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

@nicolo-ribaudo nicolo-ribaudo added PR: Bug Fix 🐛 i: regression area: jsx labels May 6, 2022
@@ -0,0 +1,10 @@
function RoutesComponent() {
Copy link
Member Author

@nicolo-ribaudo nicolo-ribaudo May 6, 2022

Choose a reason for hiding this comment

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

The old output was

var _Component;

function RoutesComponent() {
  return (
    <Routes>
      {(c) => {
        {
          const Component = c;
          return _Component || (_Component = <Component />);
        }
      }}
    </Routes>
  );
}

@@ -212,7 +212,19 @@ export default declare((api, options: Options) => {
HOISTED.set(path.node, targetScope);

// Only hoist if it would give us an advantage.
if (targetScope === jsxScope) return;
for (let currentScope = jsxScope; ; ) {
Copy link
Member Author

@nicolo-ribaudo nicolo-ribaudo May 6, 2022

Choose a reason for hiding this comment

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

This is now more complex, because in cases like

function f() {
  let Component;
  {
    return <Component />;
  }
}

jsxScope is the block statement while targetScope is the function: we still do not want to hoist the element, because it's unnecessary (there are already 2-3 tests covering this).

@babel-bot
Copy link
Collaborator

@babel-bot babel-bot commented May 6, 2022

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

JLHwung
JLHwung approved these changes May 9, 2022
@JLHwung JLHwung merged commit 871ed89 into babel:main May 9, 2022
35 checks passed
@nicolo-ribaudo nicolo-ribaudo deleted the fix-14363 branch May 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: jsx i: regression PR: Bug Fix 🐛
Projects
None yet
4 participants