Use construct helper in New Spread (#7677)

* Use construct helper in New Spread

* CircleCI
This commit is contained in:
Justin Ridgewell 2018-04-06 10:40:38 +01:00 committed by GitHub
parent 450a1678f2
commit d17adf40df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 32 additions and 36 deletions

View File

@ -474,13 +474,17 @@ helpers.construct = () => template.program.ast`
_construct = Reflect.construct; _construct = Reflect.construct;
} else { } else {
_construct = function _construct(Parent, args, Class) { _construct = function _construct(Parent, args, Class) {
var Constructor, a = [null]; var a = [null];
a.push.apply(a, args); a.push.apply(a, args);
Constructor = Parent.bind.apply(Parent, a); var Constructor = Parent.bind.apply(Parent, a);
return setPrototypeOf(new Constructor, Class.prototype); var instance = new Constructor();
if (Class) setPrototypeOf(instance, Class.prototype);
return instance;
}; };
} }
return _construct(Parent, args, Class); // Avoid issues with Class being present but undefined when it wasn't
// present in the original call.
return _construct.apply(null, arguments);
} }
`; `;

View File

@ -2,7 +2,7 @@ function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.crea
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(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() {} Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, _setPrototypeOf(function Super() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); }, Class)); }; return _wrapNativeSuper(Class); } function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(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() {} Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, _setPrototypeOf(function Super() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); }, Class)); }; return _wrapNativeSuper(Class); }
function _construct(Parent, args, Class) { if (typeof Reflect === "object" && Reflect.construct) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var Constructor, a = [null]; a.push.apply(a, args); Constructor = Parent.bind.apply(Parent, a); return _setPrototypeOf(new Constructor(), Class.prototype); }; } return _construct(Parent, args, Class); } function _construct(Parent, args, Class) { if (typeof Reflect === "object" && Reflect.construct) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Parent.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

View File

@ -8,7 +8,7 @@ function _inherits(subClass, superClass) { if (typeof superClass !== "function"
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(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() {} Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, _setPrototypeOf(function Super() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); }, Class)); }; return _wrapNativeSuper(Class); } function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(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() {} Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, _setPrototypeOf(function Super() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); }, Class)); }; return _wrapNativeSuper(Class); }
function _construct(Parent, args, Class) { if (typeof Reflect === "object" && Reflect.construct) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var Constructor, a = [null]; a.push.apply(a, args); Constructor = Parent.bind.apply(Parent, a); return _setPrototypeOf(new Constructor(), Class.prototype); }; } return _construct(Parent, args, Class); } function _construct(Parent, args, Class) { if (typeof Reflect === "object" && Reflect.construct) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Parent.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

View File

@ -49,12 +49,12 @@ export default declare((api, options) => {
return { return {
visitor: { visitor: {
ArrayExpression(path, state) { ArrayExpression(path) {
const { node, scope } = path; const { node, scope } = path;
const elements = node.elements; const elements = node.elements;
if (!hasSpread(elements)) return; if (!hasSpread(elements)) return;
const nodes = build(elements, scope, state); const nodes = build(elements, scope);
const first = nodes.shift(); const first = nodes.shift();
if (nodes.length === 0 && first !== elements[0].argument) { if (nodes.length === 0 && first !== elements[0].argument) {
@ -70,7 +70,7 @@ export default declare((api, options) => {
); );
}, },
CallExpression(path, state) { CallExpression(path) {
const { node, scope } = path; const { node, scope } = path;
const args = node.arguments; const args = node.arguments;
@ -87,7 +87,7 @@ export default declare((api, options) => {
if (args.length === 1 && args[0].argument.name === "arguments") { if (args.length === 1 && args[0].argument.name === "arguments") {
nodes = [args[0].argument]; nodes = [args[0].argument];
} else { } else {
nodes = build(args, scope, state); nodes = build(args, scope);
} }
const first = nodes.shift(); const first = nodes.shift();
@ -124,37 +124,29 @@ export default declare((api, options) => {
node.arguments.unshift(t.cloneNode(contextLiteral)); node.arguments.unshift(t.cloneNode(contextLiteral));
}, },
NewExpression(path, state) { NewExpression(path) {
const { node, scope } = path; const { node, scope } = path;
let args = node.arguments; let args = node.arguments;
if (!hasSpread(args)) return; if (!hasSpread(args)) return;
const nodes = build(args, scope, state); const nodes = build(args, scope);
const context = t.arrayExpression([t.nullLiteral()]); const first = nodes.shift();
args = t.callExpression( if (nodes.length) {
t.memberExpression(context, t.identifier("concat")), args = t.callExpression(
nodes, t.memberExpression(first, t.identifier("concat")),
); nodes,
);
} else {
args = first;
}
path.replaceWith( path.replaceWith(
t.newExpression( t.callExpression(path.hub.file.addHelper("construct"), [
t.callExpression( node.callee,
t.memberExpression( args,
t.memberExpression( ]),
t.memberExpression(
t.identifier("Function"),
t.identifier("prototype"),
),
t.identifier("bind"),
),
t.identifier("apply"),
),
[node.callee, args],
),
[],
),
); );
}, },
}, },

View File

@ -1,2 +1,2 @@
new (Function.prototype.bind.apply(Numbers, [null].concat(babelHelpers.toConsumableArray(nums))))(); babelHelpers.construct(Numbers, babelHelpers.toConsumableArray(nums));
new (Function.prototype.bind.apply(Numbers, [null].concat([1], babelHelpers.toConsumableArray(nums))))(); babelHelpers.construct(Numbers, [1].concat(babelHelpers.toConsumableArray(nums)));

View File

@ -12,7 +12,7 @@ function _inherits(subClass, superClass) { if (typeof superClass !== "function"
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(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() {} Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, _setPrototypeOf(function Super() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); }, Class)); }; return _wrapNativeSuper(Class); } function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(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() {} Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, _setPrototypeOf(function Super() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); }, Class)); }; return _wrapNativeSuper(Class); }
function _construct(Parent, args, Class) { if ((typeof Reflect === "undefined" ? "undefined" : _typeof(Reflect)) === "object" && Reflect.construct) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var Constructor, a = [null]; a.push.apply(a, args); Constructor = Parent.bind.apply(Parent, a); return _setPrototypeOf(new Constructor(), Class.prototype); }; } return _construct(Parent, args, Class); } function _construct(Parent, args, Class) { if ((typeof Reflect === "undefined" ? "undefined" : _typeof(Reflect)) === "object" && Reflect.construct) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Parent.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }