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

fix(class-properties): replace new.target in static properties with undefined #13560

Merged
merged 7 commits into from Jul 16, 2021

Conversation

colinaaa
Copy link
Contributor

@colinaaa colinaaa commented Jul 15, 2021

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

When we meet a static class property, we traverse to visit all new.target with a MetaProperty visitor.

Then we find out if or not this new.target is associated with a class by:

const func = path.findParent((path) => {
  if (path.isClass()) return true;
  if (path.isFunction() && !path.isArrowFunctionExpression()) {
    return true;
  }
  return false;
});

Now, if func is a child of prop, we do nothing. If not, that means new.target now is leaked to upper new.target, we just replace it by undefined / void 0.

if (!func.findParent((path) => path === prop)) {
  // if func is not child of prop, which means
  // `new.target` is leaked to the upper `new.target` values.
  // thus we should replace it with `undefined` (`void 0` here)
  path.replaceWith(scope.buildUndefinedNode());
}

@babel-bot
Copy link
Collaborator

babel-bot commented Jul 15, 2021

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

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jul 15, 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 f5cbf58:

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

@JLHwung
Copy link
Contributor

JLHwung commented Jul 15, 2021

We can now enable tests packages/babel-plugin-proposal-class-static-block/test/fixtures/integration/.new-target/exec.js , once disabled in #12738. You can rename .new-target to new-target.

remove function visitor since environmentVisitor is skipping arrow function
@nicolo-ribaudo nicolo-ribaudo added PR: Spec Compliance 👓 A type of pull request used for our changelog categories Spec: Class Fields labels Jul 16, 2021
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Thanks!

var _class, _temp;

this.Foo = (_temp = _class = class {}, (() => {
// fixme: new.target should be undefined after transformed
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo Jul 16, 2021

Choose a reason for hiding this comment

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

Nit: you can delete this comment from input.js

Copy link
Contributor

@JLHwung JLHwung left a comment

Thanks!

@JLHwung JLHwung merged commit aa18da0 into babel:main Jul 16, 2021
24 of 27 checks passed
nicolo-ribaudo pushed a commit to nicolo-ribaudo/babel that referenced this pull request 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
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Spec Compliance 👓 A type of pull request used for our changelog categories Spec: Class Fields
Projects
None yet
Development

Successfully merging this pull request may close these issues.

class-property transform leaks new.target in initializer to upper level
5 participants