Skip to content

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

Merged
JLHwung merged 1 commit intobabel:mainfrom
nicolo-ribaudo:fix-14363
May 9, 2022
Merged

Never hoist JSX elts referencing vars from the current scope#14536
JLHwung merged 1 commit intobabel:mainfrom
nicolo-ribaudo:fix-14363

Conversation

@nicolo-ribaudo
Copy link
Copy Markdown
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 🐛 A type of pull request used for our changelog categories i: regression area: jsx labels May 6, 2022
@@ -0,0 +1,10 @@
function RoutesComponent() {
Copy link
Copy Markdown
Member Author

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>
  );
}


// Only hoist if it would give us an advantage.
if (targetScope === jsxScope) return;
for (let currentScope = jsxScope; ; ) {
Copy link
Copy Markdown
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
Copy Markdown
Collaborator

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

@JLHwung JLHwung merged commit 871ed89 into babel:main May 9, 2022
@nicolo-ribaudo nicolo-ribaudo deleted the fix-14363 branch May 9, 2022 14:01
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Aug 9, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area: jsx i: regression 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: After upgrading to babel-plugin-transform-react-constant-elements v7.17.6 our application has stopped working correctly

4 participants