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

class-property transform leaks new.target in initializer to upper level #12737

Closed
1 task
JLHwung opened this issue Feb 2, 2021 · 5 comments · Fixed by #13560
Closed
1 task

class-property transform leaks new.target in initializer to upper level #12737

JLHwung opened this issue Feb 2, 2021 · 5 comments · Fixed by #13560
Assignees
Labels
good first issue i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue Spec: Class Fields

Comments

@JLHwung
Copy link
Contributor

JLHwung commented Feb 2, 2021

Bug Report

  • I would like to work on a fix!

Current behavior

(new C).Foo.p evaluates to C.

Input Code

class C {
  constructor() {
    this.Foo = class {
      static p = new.target
    }
  }
}

Expected behavior
(new C).Foo.p should evaluate to undefined.

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)
See REPL configs

Environment
REPL

Possible Solution
When we are converting class properties into defineProperty calls that are appended to the class, we should replace new.target in class field initializers to undefined if it is associated to the class scope, otherwise new.target is leaked to the upper new.target values.

The replacing step can be inserted after super() is replaced in initializers:

const replaced = replaceThisContext(prop, ref, superRef, state, loose);

More test examples:

class C {
  constructor() {
    this.Foo = class {
      static p1 = class { constructor() { new.target } } // should not replace
      static p2 = new function () { new.target } // should not replace
      static p3 = () => { new.target } // should replace
    }
  }
}
@babel-bot
Copy link
Collaborator

Hey @JLHwung! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@nicolo-ribaudo
Copy link
Member

If it is the first time that you contribute to Babel, follow these steps: (you need to have make and yarn available on your machine)

  1. Write a comment there to let other possible contributors know that you are working on this bug.
  2. Fork the repo
  3. Run git clone https://github.com/<YOUR_USERNAME>/babel.git && cd babel
  4. Run yarn && make bootstrap
  5. Wait ⏳
  6. Run make watch (or make build whenever you change a file)
  7. Add a test (only input.js and exec.js; output.js will be automatically generated)
  8. Update the code!
  9. yarn jest class-properties to run the tests
    • If some test outputs don't match but the new results are correct, you can delete the bad output.js files and run the tests again
    • If you prefer, you can run OVERWRITE=true yarn jest [name-of-the-package-to-test] and they will be automatically updated.
  10. If it is working, run make test to run all the tests
  11. Run git push and open a PR!

@iamfotx
Copy link
Contributor

iamfotx commented Jun 18, 2021

@nicolo-ribaudo I would love to work around this one...

@nicolo-ribaudo
Copy link
Member

It's yours!

If you need any help feel free to ask.

@colinaaa
Copy link
Contributor

Any progress on this? I can help on this!

colinaaa added a commit to colinaaa/babel that referenced this issue Jul 15, 2021
JLHwung pushed a commit that referenced this issue Jul 16, 2021
… `undefined` (#13560)

* fix(class-properties): replace `new.target` in static properties with `undefined`

non-static prop is not affected

fix #12737

* Update packages/babel-helper-create-class-features-plugin/src/fields.ts

fix typo

Co-authored-by: Brian Ng <bng412@gmail.com>

* fix: add loose test case and fix replace condition

* test: add new.target tests for static block

* feat: move new-target replace into thisContextVisitor

defaults to replace new.target, do not replace within function

* feat: simplify thisContextVisitor

remove function visitor since environmentVisitor is skipping arrow function

* test: remove unused fixme comments

Co-authored-by: Brian Ng <bng412@gmail.com>
nicolo-ribaudo pushed a commit to nicolo-ribaudo/babel that referenced this issue Jul 30, 2021
… `undefined` (babel#13560)

* fix(class-properties): replace `new.target` in static properties with `undefined`

non-static prop is not affected

fix babel#12737

* Update packages/babel-helper-create-class-features-plugin/src/fields.ts

fix typo

Co-authored-by: Brian Ng <bng412@gmail.com>

* fix: add loose test case and fix replace condition

* test: add new.target tests for static block

* feat: move new-target replace into thisContextVisitor

defaults to replace new.target, do not replace within function

* feat: simplify thisContextVisitor

remove function visitor since environmentVisitor is skipping arrow function

* test: remove unused fixme comments

Co-authored-by: Brian Ng <bng412@gmail.com>
@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 16, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue Spec: Class Fields
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants