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

Don't insert __self: this within constructors of derived classes (#13550) #13552

Merged
merged 8 commits into from Jul 26, 2021

Conversation

SCLeoX
Copy link
Contributor

@SCLeoX SCLeoX commented Jul 12, 2021

Q                       A
Fixed Issues? Fixes #13550
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 plugin the "babel-plugin-transform-react-jsx-self", __self: this is inserted for debugging purposes. However, it will cause a runtime error if it is inserted prior to a super() call in a constructor. This PR will prevent __self: this from inserted when inside a constructor of a derived class.

@babel-bot
Copy link
Collaborator

babel-bot commented Jul 12, 2021

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

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jul 12, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit e277fc5:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@SCLeoX SCLeoX changed the title Don't insert __self: this prior to super() calls (#13550) Don't insert __self: this within constructors that contain super() calls (#13550) Jul 12, 2021
@nicolo-ribaudo nicolo-ribaudo added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Jul 12, 2021
@SCLeoX SCLeoX changed the title Don't insert __self: this within constructors that contain super() calls (#13550) Don't insert __self: this within constructors of derived classes (#13550) Jul 12, 2021
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

We also have a new JSX transform (https://it.reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html), and it also needs to be patched.

You can test it with this config:

{
  plugins: [
    ["@babel/plugin-transform-react-jsx/lib/development, { runtime: "automatic" }]
  ],
}

And with this input code:

class A extends B {
  constructor() {
    <div />;
    super();
  }
}

it produces this:

var _jsxFileName = "";
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";

class A extends B {
  constructor() {
    /*#__PURE__*/
    _jsxDEV("div", {}, void 0, false, {
      fileName: _jsxFileName,
      lineNumber: 4,
      columnNumber: 5
    }, this);

    super();
  }

}

@SCLeoX
Copy link
Contributor Author

SCLeoX commented Jul 17, 2021

@nicolo-ribaudo I was not able to find a place to put the utility methods (isThisAllowed, etc.) so I just duplicated the same code. Other than that, I think it is working now.

@SCLeoX
Copy link
Contributor Author

SCLeoX commented Jul 24, 2021

So... What is the next step for moving this forward?

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

I was not able to find a place to put the utility methods (isThisAllowed, etc.) so I just duplicated the same code. Other than that, I think it is working now.

The old plugin will eventually fade away in the future, so this duplication should be ok.

Could you rebase this branch on main? It should make CI pass.

@SCLeoX
Copy link
Contributor Author

SCLeoX commented Jul 25, 2021

Could you rebase this branch on main? It should make CI pass.

Ok I did that. Hopefully I didn't botch it...

Copy link
Contributor

@JLHwung JLHwung left a comment

Choose a reason for hiding this comment

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

Thanks!

@JLHwung JLHwung merged commit 224a35c into babel:main Jul 26, 2021
nicolo-ribaudo pushed a commit to nicolo-ribaudo/babel that referenced this pull request Jul 30, 2021
…abel#13552)

* Don't insert `__self: this` prior to `super()` calls (babel#13550)

`__self: this` is inserted for debugging purposes. However, it will cause a runtime error if it is inserted prior to a `super()` call in a constructor. This commit will prevent `__self: this` from inserted when there is a following `super()` call.

* Prevent adding `__self` within a constructor that has `super()` altogether.

* Fix 2 typos in the comments.

* Add an additional test case for constructors that do not have a `super()` call.

* Detect `super()` call by testing whether the class has a superclass.

* Update method name and corresponding comments

* Add an additional test for the case where the derived class do not have a `super()` call.

* Apply the same changes to babel-plugin-transform-react-jsx
@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 Oct 26, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 26, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: jsx 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]: plugin-transform-react-jsx-self should not access this prior to the super() call in a constructor
4 participants