babel / babel-polyfills

A set of Babel plugins that enable injecting different polyfills with different strategies in your compiled code.
MIT License
331 stars 60 forks source link

Function 'finally' on prototype shouldn't polyfill 'promise' #96

Open chenjianzhonglol opened 3 years ago

chenjianzhonglol commented 3 years ago

When i define a 'finally' function on other function's prototype, why dist code includes "core-js/modules/es.promise.finally.js", "core-js/modules/es.promise.js".

ex:

image

JLHwung commented 3 years ago

I think we can skip the MemberExpression path if the it is in the left of an AssignmentExpression whose operator is =?

https://github.com/babel/babel-polyfills/blob/615e5cd57a64eca434f9129e2a73d46af9223801/packages/babel-helper-define-polyfill-provider/src/visitors/usage.js#L27

nicolo-ribaudo commented 3 years ago

It would work, even if somewhere that .finally method is probably used so the es.promise.finally polyfill will be added anyway.

I wonder why we add the polyfill for

function Test() {}

Test.prototype.finally = function() {}

but not for

Test.prototype.finally = function() {}
zloirock commented 3 years ago

I think we can skip the MemberExpression path

It will break the monkey patching of built-in methods in some cases, for example testing.