123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- "use strict";
- var util = _interopRequireWildcard(require("./util"));
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
- var hasOwn = Object.prototype.hasOwnProperty;
- exports.hoist = function (funPath) {
- var t = util.getTypes();
- t.assertFunction(funPath.node);
- var vars = {};
- function varDeclToExpr(_ref, includeIdentifiers) {
- var vdec = _ref.node,
- scope = _ref.scope;
- t.assertVariableDeclaration(vdec);
-
- var exprs = [];
- vdec.declarations.forEach(function (dec) {
-
-
- vars[dec.id.name] = t.identifier(dec.id.name);
-
-
- scope.removeBinding(dec.id.name);
- if (dec.init) {
- exprs.push(t.assignmentExpression("=", dec.id, dec.init));
- } else if (includeIdentifiers) {
- exprs.push(dec.id);
- }
- });
- if (exprs.length === 0) return null;
- if (exprs.length === 1) return exprs[0];
- return t.sequenceExpression(exprs);
- }
- funPath.get("body").traverse({
- VariableDeclaration: {
- exit: function exit(path) {
- var expr = varDeclToExpr(path, false);
- if (expr === null) {
- path.remove();
- } else {
-
-
- util.replaceWithOrRemove(path, t.expressionStatement(expr));
- }
-
-
- path.skip();
- }
- },
- ForStatement: function ForStatement(path) {
- var init = path.get("init");
- if (init.isVariableDeclaration()) {
- util.replaceWithOrRemove(init, varDeclToExpr(init, false));
- }
- },
- ForXStatement: function ForXStatement(path) {
- var left = path.get("left");
- if (left.isVariableDeclaration()) {
- util.replaceWithOrRemove(left, varDeclToExpr(left, true));
- }
- },
- FunctionDeclaration: function FunctionDeclaration(path) {
- var node = path.node;
- vars[node.id.name] = node.id;
- var assignment = t.expressionStatement(t.assignmentExpression("=", t.clone(node.id), t.functionExpression(path.scope.generateUidIdentifierBasedOnNode(node), node.params, node.body, node.generator, node.expression)));
- if (path.parentPath.isBlockStatement()) {
-
-
- path.parentPath.unshiftContainer("body", assignment);
-
-
- path.remove();
- } else {
-
-
-
- util.replaceWithOrRemove(path, assignment);
- }
-
-
- path.scope.removeBinding(node.id.name);
-
- path.skip();
- },
- FunctionExpression: function FunctionExpression(path) {
-
- path.skip();
- },
- ArrowFunctionExpression: function ArrowFunctionExpression(path) {
-
- path.skip();
- }
- });
- var paramNames = {};
- funPath.get("params").forEach(function (paramPath) {
- var param = paramPath.node;
- if (t.isIdentifier(param)) {
- paramNames[param.name] = param;
- } else {
-
-
- }
- });
- var declarations = [];
- Object.keys(vars).forEach(function (name) {
- if (!hasOwn.call(paramNames, name)) {
- declarations.push(t.variableDeclarator(vars[name], null));
- }
- });
- if (declarations.length === 0) {
- return null;
- }
- return t.variableDeclaration("var", declarations);
- };
|