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

Using JSX inside a constructor before calling super will crash at runtime #231

Closed
7 tasks done
pauldijou opened this issue Oct 3, 2023 · 3 comments
Closed
7 tasks done

Comments

@pauldijou
Copy link

pauldijou commented Oct 3, 2023

Describe the bug

After upgrading from 2.2.0 to 4.1.0, we noticed that some of our components would crash at runtime. This seems to be due to vitejs/vite@f677b62 that changed the way to compile JSX. The new compiled version includes this as the last argument of the JSX function, which was not the case previously.

This is problematic if you have some JSX defined inside a class constructor before calling super because you cannot access this before that point.

  // Compiled code from the reproduction link
  super(/* @__PURE__ */ jsxDEV("div", { children: "Content" }, void 0, false, {
    fileName: "/home/projects/vitejs-vite-t1y2x7/src/App.jsx",
    lineNumber: 19,
    columnNumber: 11
  }, this));

Reproduction

https://stackblitz.com/edit/vitejs-vite-t1y2x7?file=src%2FApp.jsx

Steps to reproduce

  • Create a class component that uses JSX before calling super
  • Render the component
  • See the error in the console: ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor

System Info

-

Used Package Manager

pnpm

Logs

No response

Validations

@ArnaudBarre
Copy link
Member

I investigated a bit and found that a edge case that Babel support but not esbuild and SWC.

if you want this edge case to be supported you need to ask it to the lib that do the transformation.

My opinion is that this edge case is quite an anti-pattern of React (FB never needed this) and I think this fine for Vite to continue to default to esbuild for this transformation, given that it allows a nice build time boost when not using custom babel plugins.

@ArnaudBarre ArnaudBarre closed this as not planned Won't fix, can't repro, duplicate, stale Oct 5, 2023
@pauldijou
Copy link
Author

Thanks for looking into it @ArnaudBarre .

@skovhus
Copy link

skovhus commented Oct 17, 2023

Note that I created an issue for esbuild to consider fixing this – failing only during development seems like a bug to me (anti-pattern or not): evanw/esbuild#3454

@github-actions github-actions bot locked and limited conversation to collaborators Nov 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants