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

feat: throw error when targets are used with regenerator #119

Merged

Conversation

agilgur5
Copy link
Contributor

@agilgur5 agilgur5 commented Mar 19, 2022

Summary

adds an error when polyfill-regenerator is supplied with targets, as those are unsupported for this polyfill per #36

Details

  • the polyfill-regenerator plugin actually does not support targets and
    does not use them

    • it instead detects whether the regeneratorRuntime variable has
      been injected by transform-regenerator, which already supports
      targets
    • so targets are not needed twice in that sense
  • instead of silently ignoring the targets option, error out when it is
    given as it is an unsupported option

    • this should hopefully make it easier for developers to realize
      what's going on when debugging issues with this (as I did)
  • add a test for this new error as well as a basic "harness" based on
    the one in define-polyfill-provider

    • remove targets from any fixtures that had them for this polyfill
      so that this test passes
      • and because they didn't do anything previously anyway

Review Notes

  • I had to create a basic test "structure"/"harness" to check for this error, so let me know if that needs stylistic/structural modification. I tried copying define-polyfill-provider's error testing with a similar options.js test file
  • Wording maybe isn't the best, so let me know if you'd like any changes to the error message

References

Fixes #36

@agilgur5 agilgur5 force-pushed the feat-regenerator-error-msg-if-targets branch from 1dc84e6 to ab4cd85 Compare March 20, 2022 16:33
@nicolo-ribaudo
Copy link
Member

@agilgur5 I was thinking that we could just completely remove the targets options from these plugins: users can specify it as a top-level option.

We would still need a way to pass it as an internal API to the corejs2 and corejs3 plugins (with an "hidden" option similar to "#__secret_key__@babel/runtime__compatibility") for compatibility with @babel/preset-env until Babel 8, but there is no other need to support the targets plugin option now that we have top-level targets.

@nicolo-ribaudo
Copy link
Member

As a shorter term solution (that one is a bigger breakig change), we can modify the check like this:

export default defineProvider(({ debug, targets, babel }, options) => {
  if (!shallowEqual(targets, babel.targets())) {
    throw new Error(
      "This plugin does not use the targets option. Only preset-env's targets" +
        " or top-level targets need to be configured for this plugin to work." +
        " See https://github.com/babel/babel-polyfills/issues/36 for more" +
        " details",
    );
  }

where shallowEqual checks if they have the same properties with the same values.

This doesn't catch the case where you explicitly specify the same top-level targets and plugin targets, but it catches the "common error" which is to specify only plugin-level targets (thus not using top-level targets).

@agilgur5 agilgur5 force-pushed the feat-regenerator-error-msg-if-targets branch from ab4cd85 to c279f77 Compare April 8, 2022 21:00
- the polyfill-regenerator plugin actually does not support targets and
  does not use them
  - it instead detects whether the `regeneratorRuntime` variable has
    been injected by `transform-regenerator`, which already supports
    targets
  - so targets are not needed twice in that sense

- instead of silently ignoring the targets option, error out when it is
  given as it is an unsupported option
  - this should hopefully make it easier for developers to realize
    what's going on when debugging issues with this (as I did)
  - but it shouldn't error if top-level targets are configured, as those
    aren't specific to this plugin
    - caveat: if a user specifies both and they are the same, there's
      currently no way to tell, so it won't error in that case
      - but will catch the common mistake

- add a test for this new error as well as a basic "harness" based on
  the one in `define-polyfill-provider`
  - remove `targets` from any fixtures that had them for this polyfill
    so that this test passes
    - and because they didn't do anything previously anyway
- also add a test that ensures that top-level targets still work
  - and change some fixtures to use top-level targets instead, ensuring
    that those fixture tests still pass as well
@agilgur5 agilgur5 force-pushed the feat-regenerator-error-msg-if-targets branch from c279f77 to e1760f0 Compare April 8, 2022 22:02
Copy link
Contributor Author

@agilgur5 agilgur5 left a comment

Choose a reason for hiding this comment

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

Rebased & updated with latest changes and added the shallow equality fix for top-level targets. Tests now pass!

@@ -39,3 +48,7 @@ export default defineProvider<Options>(({ debug }, options) => {

const isRegenerator = meta =>
meta.kind === "global" && meta.name === "regeneratorRuntime";

function shallowEqual(obj1: any, obj2: any) {
return JSON.stringify(obj1) === JSON.stringify(obj2);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is obviously a pretty naive implementation of shallow equality, but it has no deps and is very short. Let me know if that's okay or if you'd prefer something less naive

Copy link
Member

Choose a reason for hiding this comment

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

I don't like it, but it's ok in this case since it's comparing very small serializable objects.

Copy link
Contributor Author

@agilgur5 agilgur5 Apr 9, 2022

Choose a reason for hiding this comment

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

Yep that was kind of my thought process too 👍 Equality algos can get bulky real fast, so kinda went with "smallest viable option" here

@nicolo-ribaudo
Copy link
Member

Thanks!

@nicolo-ribaudo nicolo-ribaudo merged commit 0e16567 into babel:main Apr 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

polyfill-regenerator doesn't support targets
2 participants