Represent helpers as simple modules.
This commit is contained in:
@@ -5,64 +5,75 @@ import template from "babel-template";
|
||||
const helpers = {};
|
||||
export default helpers;
|
||||
|
||||
helpers.typeof = template(`
|
||||
(typeof Symbol === "function" && typeof Symbol.iterator === "symbol")
|
||||
? function (obj) { return typeof obj; }
|
||||
: function (obj) {
|
||||
function defineHelper(str) {
|
||||
return template(str, { sourceType: "module" });
|
||||
}
|
||||
|
||||
helpers.typeof = defineHelper(`
|
||||
export default function _typeof(obj) {
|
||||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
|
||||
_typeof = function (obj) { return typeof obj; };
|
||||
} else {
|
||||
_typeof = function (obj) {
|
||||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype
|
||||
? "symbol"
|
||||
: typeof obj;
|
||||
};
|
||||
}
|
||||
|
||||
return _typeof(obj);
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.jsx = template(`
|
||||
(function () {
|
||||
var REACT_ELEMENT_TYPE = (typeof Symbol === "function" && Symbol.for && Symbol.for("react.element")) || 0xeac7;
|
||||
helpers.jsx = defineHelper(`
|
||||
var REACT_ELEMENT_TYPE;
|
||||
|
||||
return function createRawReactElement (type, props, key, children) {
|
||||
var defaultProps = type && type.defaultProps;
|
||||
var childrenLength = arguments.length - 3;
|
||||
export default function _createRawReactElement(type, props, key, children) {
|
||||
if (!REACT_ELEMENT_TYPE) {
|
||||
REACT_ELEMENT_TYPE = (typeof Symbol === "function" && Symbol.for && Symbol.for("react.element")) || 0xeac7;
|
||||
}
|
||||
|
||||
if (!props && childrenLength !== 0) {
|
||||
// If we're going to assign props.children, we create a new object now
|
||||
// to avoid mutating defaultProps.
|
||||
props = {};
|
||||
}
|
||||
if (props && defaultProps) {
|
||||
for (var propName in defaultProps) {
|
||||
if (props[propName] === void 0) {
|
||||
props[propName] = defaultProps[propName];
|
||||
}
|
||||
var defaultProps = type && type.defaultProps;
|
||||
var childrenLength = arguments.length - 3;
|
||||
|
||||
if (!props && childrenLength !== 0) {
|
||||
// If we're going to assign props.children, we create a new object now
|
||||
// to avoid mutating defaultProps.
|
||||
props = {};
|
||||
}
|
||||
if (props && defaultProps) {
|
||||
for (var propName in defaultProps) {
|
||||
if (props[propName] === void 0) {
|
||||
props[propName] = defaultProps[propName];
|
||||
}
|
||||
} else if (!props) {
|
||||
props = defaultProps || {};
|
||||
}
|
||||
} else if (!props) {
|
||||
props = defaultProps || {};
|
||||
}
|
||||
|
||||
if (childrenLength === 1) {
|
||||
props.children = children;
|
||||
} else if (childrenLength > 1) {
|
||||
var childArray = Array(childrenLength);
|
||||
for (var i = 0; i < childrenLength; i++) {
|
||||
childArray[i] = arguments[i + 3];
|
||||
}
|
||||
props.children = childArray;
|
||||
if (childrenLength === 1) {
|
||||
props.children = children;
|
||||
} else if (childrenLength > 1) {
|
||||
var childArray = Array(childrenLength);
|
||||
for (var i = 0; i < childrenLength; i++) {
|
||||
childArray[i] = arguments[i + 3];
|
||||
}
|
||||
props.children = childArray;
|
||||
}
|
||||
|
||||
return {
|
||||
$$typeof: REACT_ELEMENT_TYPE,
|
||||
type: type,
|
||||
key: key === undefined ? null : '' + key,
|
||||
ref: null,
|
||||
props: props,
|
||||
_owner: null,
|
||||
};
|
||||
return {
|
||||
$$typeof: REACT_ELEMENT_TYPE,
|
||||
type: type,
|
||||
key: key === undefined ? null : '' + key,
|
||||
ref: null,
|
||||
props: props,
|
||||
_owner: null,
|
||||
};
|
||||
|
||||
})()
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.asyncIterator = template(`
|
||||
(function (iterable) {
|
||||
helpers.asyncIterator = defineHelper(`
|
||||
export default function _asyncIterator(iterable) {
|
||||
if (typeof Symbol === "function") {
|
||||
if (Symbol.asyncIterator) {
|
||||
var method = iterable[Symbol.asyncIterator];
|
||||
@@ -73,106 +84,103 @@ helpers.asyncIterator = template(`
|
||||
}
|
||||
}
|
||||
throw new TypeError("Object is not async iterable");
|
||||
})
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.asyncGenerator = template(`
|
||||
(function () {
|
||||
function AwaitValue(value) {
|
||||
this.value = value;
|
||||
}
|
||||
helpers.asyncGenerator = defineHelper(`
|
||||
function AwaitValue(value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
function AsyncGenerator(gen) {
|
||||
var front, back;
|
||||
function AsyncGenerator(gen) {
|
||||
var front, back;
|
||||
|
||||
function send(key, arg) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
var request = {
|
||||
key: key,
|
||||
arg: arg,
|
||||
resolve: resolve,
|
||||
reject: reject,
|
||||
next: null
|
||||
};
|
||||
|
||||
if (back) {
|
||||
back = back.next = request;
|
||||
} else {
|
||||
front = back = request;
|
||||
resume(key, arg);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function resume(key, arg) {
|
||||
try {
|
||||
var result = gen[key](arg)
|
||||
var value = result.value;
|
||||
if (value instanceof AwaitValue) {
|
||||
Promise.resolve(value.value).then(
|
||||
function (arg) { resume("next", arg); },
|
||||
function (arg) { resume("throw", arg); });
|
||||
} else {
|
||||
settle(result.done ? "return" : "normal", result.value);
|
||||
}
|
||||
} catch (err) {
|
||||
settle("throw", err);
|
||||
}
|
||||
}
|
||||
|
||||
function settle(type, value) {
|
||||
switch (type) {
|
||||
case "return":
|
||||
front.resolve({ value: value, done: true });
|
||||
break;
|
||||
case "throw":
|
||||
front.reject(value);
|
||||
break;
|
||||
default:
|
||||
front.resolve({ value: value, done: false });
|
||||
break;
|
||||
}
|
||||
|
||||
front = front.next;
|
||||
if (front) {
|
||||
resume(front.key, front.arg);
|
||||
} else {
|
||||
back = null;
|
||||
}
|
||||
}
|
||||
|
||||
this._invoke = send;
|
||||
|
||||
// Hide "return" method if generator return is not supported
|
||||
if (typeof gen.return !== "function") {
|
||||
this.return = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof Symbol === "function" && Symbol.asyncIterator) {
|
||||
AsyncGenerator.prototype[Symbol.asyncIterator] = function () { return this; };
|
||||
}
|
||||
|
||||
AsyncGenerator.prototype.next = function (arg) { return this._invoke("next", arg); };
|
||||
AsyncGenerator.prototype.throw = function (arg) { return this._invoke("throw", arg); };
|
||||
AsyncGenerator.prototype.return = function (arg) { return this._invoke("return", arg); };
|
||||
|
||||
return {
|
||||
wrap: function (fn) {
|
||||
return function () {
|
||||
return new AsyncGenerator(fn.apply(this, arguments));
|
||||
function send(key, arg) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
var request = {
|
||||
key: key,
|
||||
arg: arg,
|
||||
resolve: resolve,
|
||||
reject: reject,
|
||||
next: null
|
||||
};
|
||||
},
|
||||
await: function (value) {
|
||||
return new AwaitValue(value);
|
||||
}
|
||||
};
|
||||
|
||||
})()
|
||||
if (back) {
|
||||
back = back.next = request;
|
||||
} else {
|
||||
front = back = request;
|
||||
resume(key, arg);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function resume(key, arg) {
|
||||
try {
|
||||
var result = gen[key](arg)
|
||||
var value = result.value;
|
||||
if (value instanceof AwaitValue) {
|
||||
Promise.resolve(value.value).then(
|
||||
function (arg) { resume("next", arg); },
|
||||
function (arg) { resume("throw", arg); });
|
||||
} else {
|
||||
settle(result.done ? "return" : "normal", result.value);
|
||||
}
|
||||
} catch (err) {
|
||||
settle("throw", err);
|
||||
}
|
||||
}
|
||||
|
||||
function settle(type, value) {
|
||||
switch (type) {
|
||||
case "return":
|
||||
front.resolve({ value: value, done: true });
|
||||
break;
|
||||
case "throw":
|
||||
front.reject(value);
|
||||
break;
|
||||
default:
|
||||
front.resolve({ value: value, done: false });
|
||||
break;
|
||||
}
|
||||
|
||||
front = front.next;
|
||||
if (front) {
|
||||
resume(front.key, front.arg);
|
||||
} else {
|
||||
back = null;
|
||||
}
|
||||
}
|
||||
|
||||
this._invoke = send;
|
||||
|
||||
// Hide "return" method if generator return is not supported
|
||||
if (typeof gen.return !== "function") {
|
||||
this.return = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof Symbol === "function" && Symbol.asyncIterator) {
|
||||
AsyncGenerator.prototype[Symbol.asyncIterator] = function () { return this; };
|
||||
}
|
||||
|
||||
AsyncGenerator.prototype.next = function (arg) { return this._invoke("next", arg); };
|
||||
AsyncGenerator.prototype.throw = function (arg) { return this._invoke("throw", arg); };
|
||||
AsyncGenerator.prototype.return = function (arg) { return this._invoke("return", arg); };
|
||||
|
||||
export default {
|
||||
wrap: function (fn) {
|
||||
return function () {
|
||||
return new AsyncGenerator(fn.apply(this, arguments));
|
||||
};
|
||||
},
|
||||
await: function (value) {
|
||||
return new AwaitValue(value);
|
||||
}
|
||||
};
|
||||
`);
|
||||
|
||||
helpers.asyncGeneratorDelegate = template(`
|
||||
(function (inner, awaitWrap) {
|
||||
helpers.asyncGeneratorDelegate = defineHelper(`
|
||||
export default function _asyncGeneratorDelegate(inner, awaitWrap) {
|
||||
var iter = {}, waiting = false;
|
||||
|
||||
function pump(key, value) {
|
||||
@@ -210,11 +218,11 @@ helpers.asyncGeneratorDelegate = template(`
|
||||
}
|
||||
|
||||
return iter;
|
||||
})
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.asyncToGenerator = template(`
|
||||
(function (fn) {
|
||||
helpers.asyncToGenerator = defineHelper(`
|
||||
export default function _asyncToGenerator(fn) {
|
||||
return function () {
|
||||
return new Promise((resolve, reject) => {
|
||||
var gen = fn.apply(this, arguments);
|
||||
@@ -239,39 +247,37 @@ helpers.asyncToGenerator = template(`
|
||||
_next();
|
||||
});
|
||||
};
|
||||
})
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.classCallCheck = template(`
|
||||
(function (instance, Constructor) {
|
||||
helpers.classCallCheck = defineHelper(`
|
||||
export default function _classCallCheck(instance, Constructor) {
|
||||
if (!(instance instanceof Constructor)) {
|
||||
throw new TypeError("Cannot call a class as a function");
|
||||
}
|
||||
});
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.createClass = template(`
|
||||
(function() {
|
||||
function defineProperties(target, props) {
|
||||
for (var i = 0; i < props.length; i ++) {
|
||||
var descriptor = props[i];
|
||||
descriptor.enumerable = descriptor.enumerable || false;
|
||||
descriptor.configurable = true;
|
||||
if ("value" in descriptor) descriptor.writable = true;
|
||||
Object.defineProperty(target, descriptor.key, descriptor);
|
||||
}
|
||||
helpers.createClass = defineHelper(`
|
||||
function _defineProperties(target, props) {
|
||||
for (var i = 0; i < props.length; i ++) {
|
||||
var descriptor = props[i];
|
||||
descriptor.enumerable = descriptor.enumerable || false;
|
||||
descriptor.configurable = true;
|
||||
if ("value" in descriptor) descriptor.writable = true;
|
||||
Object.defineProperty(target, descriptor.key, descriptor);
|
||||
}
|
||||
}
|
||||
|
||||
return function (Constructor, protoProps, staticProps) {
|
||||
if (protoProps) defineProperties(Constructor.prototype, protoProps);
|
||||
if (staticProps) defineProperties(Constructor, staticProps);
|
||||
return Constructor;
|
||||
};
|
||||
})()
|
||||
export default function _createClass(Constructor, protoProps, staticProps) {
|
||||
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
||||
if (staticProps) _defineProperties(Constructor, staticProps);
|
||||
return Constructor;
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.defineEnumerableProperties = template(`
|
||||
(function (obj, descs) {
|
||||
helpers.defineEnumerableProperties = defineHelper(`
|
||||
export default function _defineEnumerableProperties(obj, descs) {
|
||||
for (var key in descs) {
|
||||
var desc = descs[key];
|
||||
desc.configurable = desc.enumerable = true;
|
||||
@@ -293,11 +299,11 @@ helpers.defineEnumerableProperties = template(`
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
})
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.defaults = template(`
|
||||
(function (obj, defaults) {
|
||||
helpers.defaults = defineHelper(`
|
||||
export default function _defaults(obj, defaults) {
|
||||
var keys = Object.getOwnPropertyNames(defaults);
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
var key = keys[i];
|
||||
@@ -307,11 +313,11 @@ helpers.defaults = template(`
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
})
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.defineProperty = template(`
|
||||
(function (obj, key, value) {
|
||||
helpers.defineProperty = defineHelper(`
|
||||
export default function _defineProperty(obj, key, value) {
|
||||
// Shortcircuit the slow defineProperty path when possible.
|
||||
// We are trying to avoid issues where setters defined on the
|
||||
// prototype cause side effects under the fast path of simple
|
||||
@@ -328,25 +334,29 @@ helpers.defineProperty = template(`
|
||||
obj[key] = value;
|
||||
}
|
||||
return obj;
|
||||
});
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.extends = template(`
|
||||
Object.assign || (function (target) {
|
||||
for (var i = 1; i < arguments.length; i++) {
|
||||
var source = arguments[i];
|
||||
for (var key in source) {
|
||||
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
||||
target[key] = source[key];
|
||||
helpers.extends = defineHelper(`
|
||||
export default function _extends() {
|
||||
_extends = Object.assign || function (target) {
|
||||
for (var i = 1; i < arguments.length; i++) {
|
||||
var source = arguments[i];
|
||||
for (var key in source) {
|
||||
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
||||
target[key] = source[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return target;
|
||||
})
|
||||
return target;
|
||||
};
|
||||
|
||||
return _extends.apply(this, arguments);
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.get = template(`
|
||||
(function get(object, property, receiver) {
|
||||
helpers.get = defineHelper(`
|
||||
export default function _get(object, property, receiver) {
|
||||
if (object === null) object = Function.prototype;
|
||||
|
||||
var desc = Object.getOwnPropertyDescriptor(object, property);
|
||||
@@ -357,7 +367,7 @@ helpers.get = template(`
|
||||
if (parent === null) {
|
||||
return undefined;
|
||||
} else {
|
||||
return get(parent, property, receiver);
|
||||
return _get(parent, property, receiver);
|
||||
}
|
||||
} else if ("value" in desc) {
|
||||
return desc.value;
|
||||
@@ -370,11 +380,11 @@ helpers.get = template(`
|
||||
|
||||
return getter.call(receiver);
|
||||
}
|
||||
});
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.inherits = template(`
|
||||
(function (subClass, superClass) {
|
||||
helpers.inherits = defineHelper(`
|
||||
export default function _inherits(subClass, superClass) {
|
||||
if (typeof superClass !== "function" && superClass !== null) {
|
||||
throw new TypeError("Super expression must either be null or a function");
|
||||
}
|
||||
@@ -387,35 +397,35 @@ helpers.inherits = template(`
|
||||
}
|
||||
});
|
||||
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
|
||||
})
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.inheritsLoose = template(`
|
||||
(function (subClass, superClass) {
|
||||
helpers.inheritsLoose = defineHelper(`
|
||||
export default function _inheritsLoose(subClass, superClass) {
|
||||
subClass.prototype = Object.create(superClass.prototype);
|
||||
subClass.prototype.constructor = subClass;
|
||||
subClass.__proto__ = superClass;
|
||||
})
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.instanceof = template(`
|
||||
(function (left, right) {
|
||||
helpers.instanceof = defineHelper(`
|
||||
export default function _instanceof(left, right) {
|
||||
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
||||
return right[Symbol.hasInstance](left);
|
||||
} else {
|
||||
return left instanceof right;
|
||||
}
|
||||
});
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.interopRequireDefault = template(`
|
||||
(function (obj) {
|
||||
helpers.interopRequireDefault = defineHelper(`
|
||||
export default function _interopRequireDefault(obj) {
|
||||
return obj && obj.__esModule ? obj : { default: obj };
|
||||
})
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.interopRequireWildcard = template(`
|
||||
(function (obj) {
|
||||
helpers.interopRequireWildcard = defineHelper(`
|
||||
export default function _interopRequireWildcard(obj) {
|
||||
if (obj && obj.__esModule) {
|
||||
return obj;
|
||||
} else {
|
||||
@@ -428,25 +438,25 @@ helpers.interopRequireWildcard = template(`
|
||||
newObj.default = obj;
|
||||
return newObj;
|
||||
}
|
||||
})
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.newArrowCheck = template(`
|
||||
(function (innerThis, boundThis) {
|
||||
helpers.newArrowCheck = defineHelper(`
|
||||
export default function _newArrowCheck(innerThis, boundThis) {
|
||||
if (innerThis !== boundThis) {
|
||||
throw new TypeError("Cannot instantiate an arrow function");
|
||||
}
|
||||
});
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.objectDestructuringEmpty = template(`
|
||||
(function (obj) {
|
||||
helpers.objectDestructuringEmpty = defineHelper(`
|
||||
export default function _objectDestructuringEmpty(obj) {
|
||||
if (obj == null) throw new TypeError("Cannot destructure undefined");
|
||||
});
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.objectWithoutProperties = template(`
|
||||
(function (source, excluded) {
|
||||
helpers.objectWithoutProperties = defineHelper(`
|
||||
export default function _objectWithoutProperties(source, excluded) {
|
||||
if (source == null) return {};
|
||||
|
||||
var target = {};
|
||||
@@ -470,11 +480,11 @@ helpers.objectWithoutProperties = template(`
|
||||
}
|
||||
|
||||
return target;
|
||||
})
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.possibleConstructorReturn = template(`
|
||||
(function (self, call) {
|
||||
helpers.possibleConstructorReturn = defineHelper(`
|
||||
export default function _possibleConstructorReturn(self, call) {
|
||||
if (call && (typeof call === "object" || typeof call === "function")) {
|
||||
return call;
|
||||
}
|
||||
@@ -482,22 +492,18 @@ helpers.possibleConstructorReturn = template(`
|
||||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
||||
}
|
||||
return self;
|
||||
});
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.selfGlobal = template(`
|
||||
typeof global === "undefined" ? self : global
|
||||
`);
|
||||
|
||||
helpers.set = template(`
|
||||
(function set(object, property, value, receiver) {
|
||||
helpers.set = defineHelper(`
|
||||
export default function _set(object, property, value, receiver) {
|
||||
var desc = Object.getOwnPropertyDescriptor(object, property);
|
||||
|
||||
if (desc === undefined) {
|
||||
var parent = Object.getPrototypeOf(object);
|
||||
|
||||
if (parent !== null) {
|
||||
set(parent, property, value, receiver);
|
||||
_set(parent, property, value, receiver);
|
||||
}
|
||||
} else if ("value" in desc && desc.writable) {
|
||||
desc.value = value;
|
||||
@@ -510,60 +516,58 @@ helpers.set = template(`
|
||||
}
|
||||
|
||||
return value;
|
||||
});
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.slicedToArray = template(`
|
||||
(function () {
|
||||
// Broken out into a separate function to avoid deoptimizations due to the try/catch for the
|
||||
// array iterator case.
|
||||
function sliceIterator(arr, i) {
|
||||
// this is an expanded form of \`for...of\` that properly supports abrupt completions of
|
||||
// iterators etc. variable names have been minimised to reduce the size of this massive
|
||||
// helper. sometimes spec compliancy is annoying :(
|
||||
//
|
||||
// _n = _iteratorNormalCompletion
|
||||
// _d = _didIteratorError
|
||||
// _e = _iteratorError
|
||||
// _i = _iterator
|
||||
// _s = _step
|
||||
helpers.slicedToArray = defineHelper(`
|
||||
// Broken out into a separate function to avoid deoptimizations due to the try/catch for the
|
||||
// array iterator case.
|
||||
function _sliceIterator(arr, i) {
|
||||
// this is an expanded form of \`for...of\` that properly supports abrupt completions of
|
||||
// iterators etc. variable names have been minimised to reduce the size of this massive
|
||||
// helper. sometimes spec compliancy is annoying :(
|
||||
//
|
||||
// _n = _iteratorNormalCompletion
|
||||
// _d = _didIteratorError
|
||||
// _e = _iteratorError
|
||||
// _i = _iterator
|
||||
// _s = _step
|
||||
|
||||
var _arr = [];
|
||||
var _n = true;
|
||||
var _d = false;
|
||||
var _e = undefined;
|
||||
var _arr = [];
|
||||
var _n = true;
|
||||
var _d = false;
|
||||
var _e = undefined;
|
||||
try {
|
||||
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
|
||||
_arr.push(_s.value);
|
||||
if (i && _arr.length === i) break;
|
||||
}
|
||||
} catch (err) {
|
||||
_d = true;
|
||||
_e = err;
|
||||
} finally {
|
||||
try {
|
||||
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
|
||||
_arr.push(_s.value);
|
||||
if (i && _arr.length === i) break;
|
||||
}
|
||||
} catch (err) {
|
||||
_d = true;
|
||||
_e = err;
|
||||
if (!_n && _i["return"] != null) _i["return"]();
|
||||
} finally {
|
||||
try {
|
||||
if (!_n && _i["return"] != null) _i["return"]();
|
||||
} finally {
|
||||
if (_d) throw _e;
|
||||
}
|
||||
if (_d) throw _e;
|
||||
}
|
||||
return _arr;
|
||||
}
|
||||
return _arr;
|
||||
}
|
||||
|
||||
return function (arr, i) {
|
||||
if (Array.isArray(arr)) {
|
||||
return arr;
|
||||
} else if (Symbol.iterator in Object(arr)) {
|
||||
return sliceIterator(arr, i);
|
||||
} else {
|
||||
throw new TypeError("Invalid attempt to destructure non-iterable instance");
|
||||
}
|
||||
};
|
||||
})();
|
||||
export default function _slicedToArray(arr, i) {
|
||||
if (Array.isArray(arr)) {
|
||||
return arr;
|
||||
} else if (Symbol.iterator in Object(arr)) {
|
||||
return _sliceIterator(arr, i);
|
||||
} else {
|
||||
throw new TypeError("Invalid attempt to destructure non-iterable instance");
|
||||
}
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.slicedToArrayLoose = template(`
|
||||
(function (arr, i) {
|
||||
helpers.slicedToArrayLoose = defineHelper(`
|
||||
export default function _slicedToArrayLoose(arr, i) {
|
||||
if (Array.isArray(arr)) {
|
||||
return arr;
|
||||
} else if (Symbol.iterator in Object(arr)) {
|
||||
@@ -576,71 +580,71 @@ helpers.slicedToArrayLoose = template(`
|
||||
} else {
|
||||
throw new TypeError("Invalid attempt to destructure non-iterable instance");
|
||||
}
|
||||
});
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.taggedTemplateLiteral = template(`
|
||||
(function (strings, raw) {
|
||||
helpers.taggedTemplateLiteral = defineHelper(`
|
||||
export default function _taggedTemplateLiteral(strings, raw) {
|
||||
return Object.freeze(Object.defineProperties(strings, {
|
||||
raw: { value: Object.freeze(raw) }
|
||||
}));
|
||||
});
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.taggedTemplateLiteralLoose = template(`
|
||||
(function (strings, raw) {
|
||||
helpers.taggedTemplateLiteralLoose = defineHelper(`
|
||||
export default function _taggedTemplateLiteralLoose(strings, raw) {
|
||||
strings.raw = raw;
|
||||
return strings;
|
||||
});
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.temporalRef = template(`
|
||||
(function (val, name, undef) {
|
||||
helpers.temporalRef = defineHelper(`
|
||||
export default function _temporalRef(val, name, undef) {
|
||||
if (val === undef) {
|
||||
throw new ReferenceError(name + " is not defined - temporal dead zone");
|
||||
} else {
|
||||
return val;
|
||||
}
|
||||
})
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.temporalUndefined = template(`
|
||||
({})
|
||||
helpers.temporalUndefined = defineHelper(`
|
||||
export default {};
|
||||
`);
|
||||
|
||||
helpers.toArray = template(`
|
||||
(function (arr) {
|
||||
helpers.toArray = defineHelper(`
|
||||
export default function _toArray(arr) {
|
||||
return Array.isArray(arr) ? arr : Array.from(arr);
|
||||
});
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.toConsumableArray = template(`
|
||||
(function (arr) {
|
||||
helpers.toConsumableArray = defineHelper(`
|
||||
export default function _toConsumableArray(arr) {
|
||||
if (Array.isArray(arr)) {
|
||||
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
|
||||
return arr2;
|
||||
} else {
|
||||
return Array.from(arr);
|
||||
}
|
||||
});
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.skipFirstGeneratorNext = template(`
|
||||
(function (fn) {
|
||||
helpers.skipFirstGeneratorNext = defineHelper(`
|
||||
export default function _skipFirstGeneratorNext(fn) {
|
||||
return function () {
|
||||
var it = fn.apply(this, arguments);
|
||||
it.next();
|
||||
return it;
|
||||
}
|
||||
});
|
||||
}
|
||||
`);
|
||||
|
||||
helpers.toPropertyKey = template(`
|
||||
(function (key) {
|
||||
helpers.toPropertyKey = defineHelper(`
|
||||
export default function _toPropertyKey(key) {
|
||||
if (typeof key === "symbol") {
|
||||
return key;
|
||||
} else {
|
||||
return String(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
`);
|
||||
|
||||
@@ -1,10 +1,202 @@
|
||||
import traverse from "babel-traverse";
|
||||
import * as t from "babel-types";
|
||||
import helpers from "./helpers";
|
||||
|
||||
export function get(name) {
|
||||
const fn = helpers[name];
|
||||
if (!fn) throw new ReferenceError(`Unknown helper ${name}`);
|
||||
function makePath(path) {
|
||||
const parts = [];
|
||||
|
||||
return fn().expression;
|
||||
for (; path.parentPath; path = path.parentPath) {
|
||||
parts.push(path.key);
|
||||
if (path.inList) parts.push(path.listKey);
|
||||
}
|
||||
|
||||
return parts.reverse().join(".");
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a file AST for a given helper, get a bunch of metadata about it so that Babel can quickly render
|
||||
* the helper is whatever context it is needed in.
|
||||
*/
|
||||
function getHelperMetadata(file) {
|
||||
const localBindingNames = new Set();
|
||||
|
||||
let exportName;
|
||||
let exportPath;
|
||||
const exportBindingAssignments = [];
|
||||
|
||||
traverse(file, {
|
||||
ImportDeclaration(child) {
|
||||
throw child.buildCodeFrameError("Helpers may import anything.");
|
||||
},
|
||||
ExportDefaultDeclaration(child) {
|
||||
const decl = child.get("declaration");
|
||||
|
||||
if (decl.isFunctionDeclaration()) {
|
||||
if (!decl.node.id) {
|
||||
throw decl.buildCodeFrameError(
|
||||
"Helpers should give names to their exported func declaration",
|
||||
);
|
||||
}
|
||||
|
||||
exportName = decl.node.id.name;
|
||||
}
|
||||
exportPath = makePath(child);
|
||||
},
|
||||
ExportAllDeclaration(child) {
|
||||
throw child.buildCodeFrameError("Helpers can only export default");
|
||||
},
|
||||
ExportNamedDeclaration(child) {
|
||||
throw child.buildCodeFrameError("Helpers can only export default");
|
||||
},
|
||||
Statement(child) {
|
||||
if (child.isModuleDeclaration()) return;
|
||||
|
||||
child.skip();
|
||||
},
|
||||
});
|
||||
|
||||
traverse(file, {
|
||||
Program(path) {
|
||||
const bindings = path.scope.getAllBindings();
|
||||
|
||||
Object.keys(bindings).forEach(name => {
|
||||
if (name === exportName) return;
|
||||
|
||||
localBindingNames.add(name);
|
||||
});
|
||||
},
|
||||
AssignmentExpression(child) {
|
||||
const left = child.get("left");
|
||||
|
||||
if (!(exportName in left.getBindingIdentifiers())) return;
|
||||
|
||||
if (!left.isIdentifier()) {
|
||||
throw left.buildCodeFrameError(
|
||||
"Only simple assignments to exports are allowed in helpers",
|
||||
);
|
||||
}
|
||||
|
||||
const binding = child.scope.getBinding(exportName);
|
||||
|
||||
if (binding && binding.scope.path.isProgram()) {
|
||||
exportBindingAssignments.push(makePath(child));
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
if (!exportPath) throw new Error("Helpers must default-export something.");
|
||||
|
||||
// Process these in reverse so that mutating the references does not invalidate any later paths in
|
||||
// the list.
|
||||
exportBindingAssignments.reverse();
|
||||
|
||||
return {
|
||||
localBindingNames: Array.from(localBindingNames),
|
||||
exportBindingAssignments,
|
||||
exportPath,
|
||||
exportName,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a helper AST and information about how it will be used, update the AST to match the usage.
|
||||
*/
|
||||
function permuteHelperAST(file, metadata, id, localBindings) {
|
||||
if (localBindings && !id) {
|
||||
throw new Error("Unexpected local bindings for module-based helpers.");
|
||||
}
|
||||
|
||||
if (!id) return;
|
||||
|
||||
const {
|
||||
localBindingNames,
|
||||
exportBindingAssignments,
|
||||
exportPath,
|
||||
exportName,
|
||||
} = metadata;
|
||||
|
||||
const toRename = {};
|
||||
const bindings = new Set(localBindings || []);
|
||||
localBindingNames.forEach(name => {
|
||||
let newName = name;
|
||||
while (bindings.has(newName)) newName = "_" + newName;
|
||||
|
||||
if (newName !== name) toRename[name] = newName;
|
||||
});
|
||||
|
||||
if (id.type === "Identifier" && exportName !== id.name) {
|
||||
toRename[exportName] = id.name;
|
||||
}
|
||||
|
||||
traverse(file, {
|
||||
Program(path) {
|
||||
const exp = path.get(exportPath);
|
||||
const decl = exp.get("declaration");
|
||||
if (id.type === "Identifier") {
|
||||
if (decl.isFunctionDeclaration()) {
|
||||
exp.replaceWith(decl);
|
||||
} else {
|
||||
exp.replaceWith(
|
||||
t.variableDeclaration("var", [t.variableDeclarator(id, decl.node)]),
|
||||
);
|
||||
}
|
||||
} else if (id.type === "MemberExpression") {
|
||||
if (decl.isFunctionDeclaration()) {
|
||||
exportBindingAssignments.forEach(assignPath => {
|
||||
const assign = path.get(assignPath);
|
||||
assign.replaceWith(t.assignmentExpression("=", id, assign.node));
|
||||
});
|
||||
exp.replaceWith(decl);
|
||||
path.pushContainer(
|
||||
"body",
|
||||
t.assignmentExpression("=", id, t.identifier(exportName)),
|
||||
);
|
||||
} else {
|
||||
exp.replaceWith(t.assignmentExpression("=", id, decl.node));
|
||||
}
|
||||
} else {
|
||||
throw new Error("Unexpected helper format.");
|
||||
}
|
||||
|
||||
Object.keys(toRename).forEach(name => {
|
||||
path.scope.rename(name, toRename[name]);
|
||||
});
|
||||
|
||||
// We only use "traverse" for all the handy scoping helpers, so we can stop immediately without
|
||||
// actually doing the traversal.
|
||||
path.stop();
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const helperData = {};
|
||||
function loadHelper(name) {
|
||||
if (!helperData[name]) {
|
||||
if (!helpers[name]) throw new ReferenceError(`Unknown helper ${name}`);
|
||||
|
||||
const fn = () => {
|
||||
const ast = helpers[name]();
|
||||
return t.file(t.program(Array.isArray(ast) ? ast : [ast]));
|
||||
};
|
||||
|
||||
const metadata = getHelperMetadata(fn());
|
||||
|
||||
helperData[name] = function(id, localBindings) {
|
||||
const file = fn();
|
||||
permuteHelperAST(file, metadata, id, localBindings);
|
||||
|
||||
return {
|
||||
nodes: file.program.body,
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
return helperData[name];
|
||||
}
|
||||
|
||||
export function get(name, id?, localBindings?: Array) {
|
||||
const helper = loadHelper(name);
|
||||
return helper(id, localBindings);
|
||||
}
|
||||
|
||||
export const list = Object.keys(helpers)
|
||||
|
||||
Reference in New Issue
Block a user