[_wrapNativeSuper] Don't wrap non-native constructors (#8501)
This commit is contained in:
parent
39fa102ee8
commit
1f9b2a5c41
@ -483,6 +483,8 @@ helpers.construct = helper("7.0.0-beta.0")`
|
||||
if (isNativeReflectConstruct()) {
|
||||
_construct = Reflect.construct;
|
||||
} else {
|
||||
// NOTE: If Parent !== Class, the correct __proto__ is set *after*
|
||||
// calling the constructor.
|
||||
_construct = function _construct(Parent, args, Class) {
|
||||
var a = [null];
|
||||
a.push.apply(a, args);
|
||||
@ -498,17 +500,25 @@ 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;
|
||||
}
|
||||
`;
|
||||
|
||||
// Based on https://github.com/WebReflection/babel-plugin-transform-builtin-classes
|
||||
helpers.wrapNativeSuper = helper("7.0.0-beta.0")`
|
||||
import getPrototypeOf from "getPrototypeOf";
|
||||
import setPrototypeOf from "setPrototypeOf";
|
||||
import isNativeFunction from "isNativeFunction";
|
||||
import construct from "construct";
|
||||
|
||||
export default function _wrapNativeSuper(Class) {
|
||||
var _cache = typeof Map === "function" ? new Map() : undefined;
|
||||
|
||||
_wrapNativeSuper = function _wrapNativeSuper(Class) {
|
||||
if (Class === null) return null;
|
||||
if (Class === null || !isNativeFunction(Class)) return Class;
|
||||
if (typeof Class !== "function") {
|
||||
throw new TypeError("Super expression must either be null or a function");
|
||||
}
|
||||
|
||||
@ -1,11 +1,13 @@
|
||||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
|
||||
|
||||
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null) return null; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
|
||||
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
|
||||
|
||||
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 _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 _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
|
||||
|
||||
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
||||
|
||||
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
||||
|
||||
@ -6,12 +6,14 @@ function _assertThisInitialized(self) { if (self === void 0) { throw new Referen
|
||||
|
||||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
||||
|
||||
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null) return null; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
|
||||
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
|
||||
|
||||
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 _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 _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
|
||||
|
||||
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
||||
|
||||
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
||||
|
||||
17
packages/babel-plugin-transform-classes/test/fixtures/regression/8499/exec.js
vendored
Normal file
17
packages/babel-plugin-transform-classes/test/fixtures/regression/8499/exec.js
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
// Pretend that `Reflect.construct` isn't supported.
|
||||
this.Reflect = undefined;
|
||||
|
||||
this.HTMLElement = function() {
|
||||
// Here, `this.HTMLElement` is this function, not the original HTMLElement
|
||||
// constructor. `this.constructor` should be this function too, but isn't.
|
||||
constructor = this.constructor;
|
||||
};
|
||||
|
||||
var constructor;
|
||||
|
||||
class CustomElement extends HTMLElement {};
|
||||
new CustomElement();
|
||||
|
||||
expect(constructor).toBe(CustomElement);
|
||||
|
||||
|
||||
17
packages/babel-plugin-transform-classes/test/fixtures/regression/8499/input.js
vendored
Normal file
17
packages/babel-plugin-transform-classes/test/fixtures/regression/8499/input.js
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
// Pretend that `Reflect.construct` isn't supported.
|
||||
this.Reflect = undefined;
|
||||
|
||||
this.HTMLElement = function() {
|
||||
// Here, `this.HTMLElement` is this function, not the original HTMLElement
|
||||
// constructor. `this.constructor` should be this function too, but isn't.
|
||||
constructor = this.constructor;
|
||||
};
|
||||
|
||||
var constructor;
|
||||
|
||||
class CustomElement extends HTMLElement {};
|
||||
new CustomElement();
|
||||
|
||||
expect(constructor).toBe(CustomElement);
|
||||
|
||||
|
||||
29
packages/babel-plugin-transform-classes/test/fixtures/regression/8499/output.js
vendored
Normal file
29
packages/babel-plugin-transform-classes/test/fixtures/regression/8499/output.js
vendored
Normal file
@ -0,0 +1,29 @@
|
||||
// Pretend that `Reflect.construct` isn't supported.
|
||||
this.Reflect = undefined;
|
||||
|
||||
this.HTMLElement = function () {
|
||||
// Here, `this.HTMLElement` is this function, not the original HTMLElement
|
||||
// constructor. `this.constructor` should be this function too, but isn't.
|
||||
constructor = this.constructor;
|
||||
};
|
||||
|
||||
var constructor;
|
||||
|
||||
var CustomElement =
|
||||
/*#__PURE__*/
|
||||
function (_HTMLElement) {
|
||||
"use strict";
|
||||
|
||||
babelHelpers.inherits(CustomElement, _HTMLElement);
|
||||
|
||||
function CustomElement() {
|
||||
babelHelpers.classCallCheck(this, CustomElement);
|
||||
return babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(CustomElement).apply(this, arguments));
|
||||
}
|
||||
|
||||
return CustomElement;
|
||||
}(babelHelpers.wrapNativeSuper(HTMLElement));
|
||||
|
||||
;
|
||||
new CustomElement();
|
||||
expect(constructor).toBe(CustomElement);
|
||||
@ -10,12 +10,14 @@ function _assertThisInitialized(self) { if (self === void 0) { throw new Referen
|
||||
|
||||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
||||
|
||||
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null) return null; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
|
||||
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
|
||||
|
||||
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 _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 _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
|
||||
|
||||
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
||||
|
||||
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user