Skip to content

Commit

Permalink
Fix _isNaiveReflectConstruct helper
Browse files Browse the repository at this point in the history
Date#toString is generic in ES2015 [1] and it doesn't throw, but it returns "Invalid Date"

[1]: tc39/ecma262#1268 (comment)
  • Loading branch information
nicolo-ribaudo committed Feb 21, 2021
1 parent 8e7a164 commit 72fa863
Show file tree
Hide file tree
Showing 47 changed files with 58 additions and 52 deletions.
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;
// 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";
} 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

0 comments on commit 72fa863

Please sign in to comment.