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

[Bug]: toSorted is not polyfilled on optional chains without plugin-transform-optional-chaining #16845

Open
1 task
epmatsw opened this issue Sep 19, 2024 · 1 comment · May be fixed by babel/babel-polyfills#221
Labels

Comments

@epmatsw
Copy link

epmatsw commented Sep 19, 2024

💻

  • Would you like to work on a fix?

How are you using Babel?

babel-loader (webpack)

Input code

c?.toSorted();

Configuration file name

.babelrc.json

Configuration

{
	"presets": [
		[
			"@babel/preset-env",
			{
				"modules": false,
				"loose": true,
				"useBuiltIns": "usage",
				"shippedProposals": false,
				"corejs": "3.38",
				"debug": true,
				"bugfixes": true,
				"targets": "safari 14.1, chrome 87"
			}
		]
	]
}

Current and expected behavior

Given the code above, you would expect babel to inject import "core-js/modules/es.array.to-sorted.js" since toSorted is not supported in those browser versions. However, with debug logging on, you can see:

Using targets:
{
  "chrome": "87",
  "safari": "14.1"
}

Using modules transform: false

Using plugins:
  transform-duplicate-named-capturing-groups-regex { chrome < 126, safari < 17.4 }
  transform-unicode-sets-regex { chrome < 112, safari < 17 }
  bugfix/transform-v8-static-class-fields-redefine-readonly { chrome < 98, safari < 15 }
  bugfix/transform-firefox-class-in-computed-class-key { safari < 16 }
  bugfix/transform-safari-class-field-initializer-scope { safari < 16 }
  proposal-class-static-block { chrome < 94, safari < 16.4 }
  proposal-private-property-in-object { chrome < 91, safari < 15 }
  syntax-class-properties
  proposal-private-methods { safari < 15 }
  syntax-numeric-separator
  syntax-nullish-coalescing-operator
  syntax-optional-chaining
  syntax-json-strings
  syntax-optional-catch-binding
  syntax-async-generators
  syntax-object-rest-spread
  syntax-export-namespace-from
  bugfix/transform-safari-id-destructuring-collision-in-function-expression { safari < 16.3 }
  bugfix/transform-v8-spread-parameters-in-optional-chaining { chrome < 91 }
  syntax-dynamic-import
  syntax-top-level-await
  syntax-import-meta
corejs3: `DEBUG` option

Using targets: {
  "chrome": "87",
  "safari": "14.1"
}

Using polyfills with `usage-global` method:

[/Volumes/Code/tinker/babel-bug-maybe/test2.js]
Based on your code and targets, the corejs3 polyfill did not add any polyfill.
c?.toSorted();

If you remove the optional chain, you instead see (truncated):

The corejs3 polyfill added the following polyfills:
  es.array.to-sorted { "chrome":"87", "safari":"14.1" }
import "core-js/modules/es.array.to-sorted.js";
c.toSorted();

Similarly, if you force @babel/plugin-transform-optional-chaining, you get:

The corejs3 polyfill added the following polyfills:
  es.array.to-sorted { "chrome":"87", "safari":"14.1" }
var _c;
import "core-js/modules/es.array.to-sorted.js";
(_c = c) === null || _c === void 0 ? void 0 : _c.toSorted();

It seems like whatever mechanism is detecting the usage of toSorted for polyfill purposes is unable to see it on that optional chain.

Environment

System:
OS: macOS 15.0
Binaries:
Node: 22.9.0 - ~/.nvm/versions/node/v22.9.0/bin/node
Yarn: 1.22.22 - ~/.nvm/versions/node/v22.9.0/bin/yarn
npm: 10.8.3 - ~/.nvm/versions/node/v22.9.0/bin/npm
bun: 1.1.10 - ~/.bun/bin/bun
npmPackages:
@babel/cli: ^7 => 7.25.6
@babel/core: ^7 => 7.25.2
@babel/preset-env: ^7 => 7.25.4

Possible solution

No response

Additional context

No response

@babel-bot
Copy link
Collaborator

Hey @epmatsw! 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants