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

… `undefined`

non-static prop is not affected

fix #12737
@babel-bot
Copy link
Collaborator

@babel-bot babel-bot commented Jul 15, 2021

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

@codesandbox
Copy link

@codesandbox codesandbox 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 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
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

This comment has been minimized.

@nicolo-ribaudo

nicolo-ribaudo Jul 16, 2021
Member

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
24 of 27 checks passed
@github-actions
Prepare Cache
Details
@github-actions
Validate Yarn dependencies and constraints
Details
@github-actions
Test on Node.js Latest
Details
@github-actions
Build Babel Artifacts
Details
@github-actions
Test Babel 8 breaking changes
Details
@github-actions
Publish to local Verdaccio registry
Details
@github-actions
Lint
Details
@github-actions
Test on Node.js (14)
Details
@github-actions
Test on Node.js (12)
Details
@github-actions
Test on Node.js (10)
Details
@github-actions
Test on Node.js (8)
Details
@github-actions
Test on Node.js (6)
Details
@github-actions
Test on Windows
Details
@github-actions
Third-party Parser Tests
Details
@github-actions
Test @babel/runtime integrations
Details
@github-actions
E2E (babel)
Details
@github-actions
E2E (babel-old-version)
Details
@github-actions
E2E (create-react-app) E2E (create-react-app)
Details
@github-actions
E2E (vue-cli)
Details
@github-actions
E2E (jest)
Details
@circleci-checks
e2e-breaking-pr Workflow: e2e-breaking-pr
Details
@circleci-checks
test262-pr Workflow: test262-pr
Details
@gitpod-io
Gitpod Open an online workspace in Gitpod
Details
@babel-bot
babel/repl REPL preview is available
Details
@circleci-checks
build-standalone Workflow: build-standalone
Details
@codesandbox
ci/codesandbox Building packages succeeded.
Details
@codecov
codecov/project 92.04% (target 90.00%)
Details
nicolo-ribaudo added 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

5 participants