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

Fix: fallback to typeof when toString is applied to incompatible object #16017

Merged
merged 1 commit into from Oct 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/babel-helpers/src/helpers.ts
Expand Up @@ -314,7 +314,12 @@ helpers.construct = helper("7.0.0-beta.0")`
helpers.isNativeFunction = helper("7.0.0-beta.0")`
export default function _isNativeFunction(fn) {
// Note: This function returns "true" for core-js functions.
return Function.toString.call(fn).indexOf("[native code]") !== -1;
try {
return Function.toString.call(fn).indexOf("[native code]") !== -1;
} catch (e) {
// Firefox 31 throws when "toString" is applied to an HTMLElement
return typeof fn === "function";
Copy link
Member

Choose a reason for hiding this comment

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

This is safe because on user-defined Function.toString never throws, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think so, Function.toString will throw a type error if the input is neither object nor function, unless there are more browser quirks we are unaware of.

According to this SO issue, Function.toString throws on HTMLElement because at that time they were not inherited from Object.

}
}
`;

Expand Down