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

Global this can be incorrect depending on input code context #353

Open
overlookmotel opened this issue Feb 13, 2022 · 0 comments
Open

Global this can be incorrect depending on input code context #353

overlookmotel opened this issue Feb 13, 2022 · 0 comments
Labels
bug Something isn't working

Comments

@overlookmotel
Copy link
Owner

overlookmotel commented Feb 13, 2022

The value of global this depends on the code context.

  • In ESM code, this is undefined.
  • In CommonJS code, this is module.exports.
  • In script context, this is globalThis.
  • In indirect eval (0,eval)(...), this is globalThis.

this which has no local binding is currently treated as a global, so () => this is output unchanged as () => this.

This is mostly correct as long as output format is same as input format. But:

  1. If CommonJS input is output as CommonJS, this in output will refer to the wrong module.exports.
  2. If e.g. CommonJS input is being output as ESM, the value of this in output will be different from what it was in input.

@babel/plugin-transform-modules-commonjs which is used to transform ESM to CommonJS does substitute void 0 for this, so output is correct for ESM input. But the other cases don't work correctly.

@overlookmotel overlookmotel added the bug Something isn't working label Feb 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant