babel-polyfills icon indicating copy to clipboard operation
babel-polyfills copied to clipboard

[corejs3] Avoid injecting `"".split` polyfill when it's not necessary

Open nicolo-ribaudo opened this issue 4 years ago • 10 comments

Fixes https://github.com/babel/babel/issues/9196.

  1. Since core-js@2 is deprecated, I'm only fixing this core core-js@3
  2. There are probably many more polyfills that can safely skipped with static analysis, but I don't have a list of them. This PR prepares the foundation, and PRs expanding the filter are welcome!

nicolo-ribaudo avatar Jan 24 '21 01:01 nicolo-ribaudo

Yes! I'll prepare a PR tomorrow

nicolo-ribaudo avatar Jan 24 '21 01:01 nicolo-ribaudo

Actually, i wonder if there’s a way to delegate this knowledge to the package - that way, if it needs to change, Babel doesn’t have to care?

ljharb avatar Jan 24 '21 02:01 ljharb

Well, those polyfill plugins are already package-specific and contain code specific to the polyfill (implicit dependencies between polyfills, support data, mapping from es features to import names)

nicolo-ribaudo avatar Jan 24 '21 10:01 nicolo-ribaudo

Right, but they’re still under Babel’s aegis. it’d be great if the conditions under which the polyfill were applied were under the control of the shim author - which is the point of the es-shims getPolyfill convention.

ljharb avatar Jan 24 '21 16:01 ljharb

When will this fix be released?

zhanxiaoge avatar Jan 26 '21 02:01 zhanxiaoge

I think this is a move in the right direction.

(Perhaps as phase 2,) I'd like to see calls like the following supported and covered by test(s) as well, because stuff is frequently passed around as variables rather than literals:

new String(string).split(".")

This would allow developers to easily tell babel it's a String, when Babel can't detect it for sure itself.

(Perhaps as phase 3,) I'd even suggest support for suppression by a comment like[1]: // NOPOLYFILL E.g.,

string.split(".") // NOPOLYFILL

or, inventing an expression, "comment-typing" ("comment-typedness"?) like

string.split(".") // String

P.S. [1] This could be considered as something inspired by unrelated libraries like NOPMD in: https://pmd.github.io/latest/pmd_userdocs_suppressing_warnings.html [2] This type of stuff isn't as good as what typescript provides, but could make JS stronger in competing with it...

haqer1 avatar Jan 28 '21 10:01 haqer1

Note that the polyfill is not for the "this", it's for the argument.

nicolo-ribaudo avatar Jan 28 '21 12:01 nicolo-ribaudo

Note that the polyfill is not for the "this", it's for the argument.

In any case, it would be nice if cases like those mentioned in the OP under #9196 are handled (if not in phase 1, perhaps in phase 2 or 3):

new String(string).split("."); // ex. 1
var sa = typeof str == "string" ? /* it's now obvious that str is a String */ str.split(".") : null; // ex. 2

It seems to me in most cases, there would be a variable on the left (this) side & a string on the right (param) side. If that is handled in most cases, that would be a good state of affairs.

However, fairly frequently i end up with code that uses Constants.SOMETHING as param as well. If that could also be handled using detection like in ex. 1, ex. 2 above, etc., that would be an even better state of affairs.

P.S. Ditto for the following enhancement suggestions, perhaps in phase 4 in addition to handling of ex. 1 & ex. 2 above (or perhaps instead of trying to deduce the type as in ex. 1 & ex. 2 above):

string.split(Constants.SOME_STRING) // NOPOLYFILL
string.split(Constants.SOME_STRING) // String

Anyway, you guys decide. I'm just pitching in a bit...

haqer1 avatar Jan 28 '21 13:01 haqer1

Without literals or type information i don’t know how babel could reliably determine that.

it would be a very bad idea to encourage developers to annotate their code merely to ensure accurate presence or absence of polyfills; it’s far better to overinclude than underinclude.

ljharb avatar Jan 28 '21 14:01 ljharb

The branch merge failed. Will this fix be released again?

zhanxiaoge avatar Feb 26 '21 02:02 zhanxiaoge