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 _isNativeReflectConstruct helper #8461

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 12 additions & 6 deletions packages/babel-helpers/src/helpers.js
Expand Up @@ -506,10 +506,12 @@ helpers.setPrototypeOf = helper("7.0.0-beta.0")`

helpers.isNativeReflectConstruct = helper("7.9.0")`
export default function _isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;

// core-js@3
if (Reflect.construct.sham) return false;
if (
typeof Reflect === "undefined" ||
!Reflect.construct ||
// core-js@3
Reflect.construct.sham
) return false;
Copy link
Member

Choose a reason for hiding this comment

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

Weak Nit: I actually liked the previous style better.

Copy link
Member Author

Choose a reason for hiding this comment

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

Me too, but I noticed that merging them minifies better (terser doesn't merge the two if statements).

Copy link
Member

Choose a reason for hiding this comment

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

It should, I made the PR myself 😉 : terser/terser#555

Copy link
Member Author

Choose a reason for hiding this comment

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

Not with this code 🤔

// write or paste code here

export function f() {
  if (!a) return false;
  if (!b) return false;
  try {
    foo();
    return true;
  } catch {
    return false;
  }
}

(tested at https://try.terser.org/)

Anyway, I'm happy to revert this change since it will be eventually handled by terser.

Copy link
Member

Choose a reason for hiding this comment

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

Ahh, it's because it can't collapse the try-catch statement into an expression, so the if-statements exist only as a series of if-statements and not a nested ternary. Open an issue?


// Proxy can't be polyfilled. Every browser implemented
// proxies before or at the same time as Reflect.construct,
Expand All @@ -523,8 +525,12 @@ helpers.isNativeReflectConstruct = helper("7.9.0")`
try {
// If the internal slots aren't set, this throws an error similar to
// TypeError: this is not a Date object.
Date.prototype.toString.call(Reflect.construct(Date, [], function() {}));
return true;
// Date#toString is generic in ES2015 [1] and it doesn't throw, but it
// returns "Invalid Date" if the receiver is not a properly initialized
// Date object.
// [1]: https://github.com/tc39/ecma262/issues/1268#issuecomment-410104832

return Date.prototype.toString.call(Reflect.construct(Date, [], function() {})) !== "Invalid Date";
Copy link
Member

Choose a reason for hiding this comment

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

} catch (e) {
return false;
}
Expand Down
@@ -1,6 +1,6 @@
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = babelHelpers.getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = babelHelpers.getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return babelHelpers.possibleConstructorReturn(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct || Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { return Date.prototype.toString.call(Reflect.construct(Date, [], function () {})) !== "Invalid Date"; } catch (e) { return false; } }

var Child = /*#__PURE__*/function (_Parent) {
"use strict";
Expand Down
Expand Up @@ -10,7 +10,7 @@ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) ===

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct || Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { return Date.prototype.toString.call(Reflect.construct(Date, [], function () {})) !== "Invalid Date"; } catch (e) { return false; } }

function _get(target, property, receiver) { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(receiver); } return desc.value; }; } return _get(target, property, receiver || target); }

Expand Down
Expand Up @@ -12,7 +12,7 @@ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) ===

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct || Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { return Date.prototype.toString.call(Reflect.construct(Date, [], function () {})) !== "Invalid Date"; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

Expand Down
Expand Up @@ -4,7 +4,7 @@ function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new

function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct || Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { return Date.prototype.toString.call(Reflect.construct(Date, [], function () {})) !== "Invalid Date"; } catch (e) { return false; } }

function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }

Expand Down
@@ -1,6 +1,6 @@
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = babelHelpers.getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = babelHelpers.getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return babelHelpers.possibleConstructorReturn(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct || Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { return Date.prototype.toString.call(Reflect.construct(Date, [], function () {})) !== "Invalid Date"; } catch (e) { return false; } }

let Array = function Array() {
"use strict";
Expand Down
Expand Up @@ -12,7 +12,7 @@ function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new

function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct || Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { return Date.prototype.toString.call(Reflect.construct(Date, [], function () {})) !== "Invalid Date"; } catch (e) { return false; } }

function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }

Expand Down
Expand Up @@ -14,7 +14,7 @@ function _possibleConstructorReturn(self, call) { if (call && (typeof call === "

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct || Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { return Date.prototype.toString.call(Reflect.construct(Date, [], function () {})) !== "Invalid Date"; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

Expand Down
Expand Up @@ -14,7 +14,7 @@ function _possibleConstructorReturn(self, call) { if (call && (typeof call === "

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct || Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { return Date.prototype.toString.call(Reflect.construct(Date, [], function () {})) !== "Invalid Date"; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

Expand Down
Expand Up @@ -18,7 +18,7 @@ function _possibleConstructorReturn(self, call) { if (call && (typeof call === "

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct || Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { return Date.prototype.toString.call(Reflect.construct(Date, [], function () {})) !== "Invalid Date"; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

Expand Down
Expand Up @@ -14,7 +14,7 @@ function _possibleConstructorReturn(self, call) { if (call && (typeof call === "

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct || Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { return Date.prototype.toString.call(Reflect.construct(Date, [], function () {})) !== "Invalid Date"; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

Expand Down
Expand Up @@ -18,7 +18,7 @@ function _possibleConstructorReturn(self, call) { if (call && (typeof call === "

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct || Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { return Date.prototype.toString.call(Reflect.construct(Date, [], function () {})) !== "Invalid Date"; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

Expand Down
Expand Up @@ -14,7 +14,7 @@ function _possibleConstructorReturn(self, call) { if (call && (typeof call === "

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct || Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { return Date.prototype.toString.call(Reflect.construct(Date, [], function () {})) !== "Invalid Date"; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

Expand Down
Expand Up @@ -18,7 +18,7 @@ function _possibleConstructorReturn(self, call) { if (call && (typeof call === "

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct || Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { return Date.prototype.toString.call(Reflect.construct(Date, [], function () {})) !== "Invalid Date"; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

Expand Down
Expand Up @@ -14,7 +14,7 @@ function _possibleConstructorReturn(self, call) { if (call && (typeof call === "

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct || Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { return Date.prototype.toString.call(Reflect.construct(Date, [], function () {})) !== "Invalid Date"; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

Expand Down
Expand Up @@ -24,7 +24,7 @@ function _possibleConstructorReturn(self, call) { if (call && (typeof call === "

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct || Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { return Date.prototype.toString.call(Reflect.construct(Date, [], function () {})) !== "Invalid Date"; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

Expand Down
Expand Up @@ -24,7 +24,7 @@ function _possibleConstructorReturn(self, call) { if (call && (typeof call === "

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct || Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { return Date.prototype.toString.call(Reflect.construct(Date, [], function () {})) !== "Invalid Date"; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

Expand Down