123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- "use strict";
- var _assert = _interopRequireDefault(require("assert"));
- var _hoist = require("./hoist");
- var _emit = require("./emit");
- var _replaceShorthandObjectMethod = _interopRequireDefault(require("./replaceShorthandObjectMethod"));
- var util = _interopRequireWildcard(require("./util"));
- var _private = require("private");
- function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
- exports.getVisitor = function (_ref) {
- var t = _ref.types;
- return {
- Function: {
- exit: util.wrapWithTypes(t, function (path, state) {
- var node = path.node;
- if (node.generator) {
- if (node.async) {
-
- if (state.opts.asyncGenerators === false) return;
- } else {
-
- if (state.opts.generators === false) return;
- }
- } else if (node.async) {
-
- if (state.opts.async === false) return;
- } else {
-
- return;
- }
- path = (0, _replaceShorthandObjectMethod.default)(path);
- node = path.node;
- var contextId = path.scope.generateUidIdentifier("context");
- var argsId = path.scope.generateUidIdentifier("args");
- path.ensureBlock();
- var bodyBlockPath = path.get("body");
- if (node.async) {
- bodyBlockPath.traverse(awaitVisitor);
- }
- bodyBlockPath.traverse(functionSentVisitor, {
- context: contextId
- });
- var outerBody = [];
- var innerBody = [];
- bodyBlockPath.get("body").forEach(function (childPath) {
- var node = childPath.node;
- if (t.isExpressionStatement(node) && t.isStringLiteral(node.expression)) {
-
-
-
-
-
- outerBody.push(node);
- } else if (node && node._blockHoist != null) {
- outerBody.push(node);
- } else {
- innerBody.push(node);
- }
- });
- if (outerBody.length > 0) {
-
-
- bodyBlockPath.node.body = innerBody;
- }
- var outerFnExpr = getOuterFnExpr(path);
-
-
- t.assertIdentifier(node.id);
- var innerFnId = t.identifier(node.id.name + "$");
-
- var vars = (0, _hoist.hoist)(path);
- var context = {
- usesThis: false,
- usesArguments: false,
- getArgsId: function getArgsId() {
- return t.clone(argsId);
- }
- };
- path.traverse(argumentsThisVisitor, context);
- if (context.usesArguments) {
- vars = vars || t.variableDeclaration("var", []);
- var argumentIdentifier = t.identifier("arguments");
- argumentIdentifier._shadowedFunctionLiteral = path;
- vars.declarations.push(t.variableDeclarator(t.clone(argsId), argumentIdentifier));
- }
- var emitter = new _emit.Emitter(contextId);
- emitter.explode(path.get("body"));
- if (vars && vars.declarations.length > 0) {
- outerBody.push(vars);
- }
- var wrapArgs = [emitter.getContextFunction(innerFnId)];
- var tryLocsList = emitter.getTryLocsList();
- if (node.generator) {
- wrapArgs.push(outerFnExpr);
- } else if (context.usesThis || tryLocsList) {
-
-
-
- wrapArgs.push(t.nullLiteral());
- }
- if (context.usesThis) {
- wrapArgs.push(t.thisExpression());
- } else if (tryLocsList) {
- wrapArgs.push(t.nullLiteral());
- }
- if (tryLocsList) {
- wrapArgs.push(tryLocsList);
- }
- var wrapCall = t.callExpression(util.runtimeProperty(node.async ? "async" : "wrap"), wrapArgs);
- outerBody.push(t.returnStatement(wrapCall));
- node.body = t.blockStatement(outerBody);
- var oldDirectives = bodyBlockPath.node.directives;
- if (oldDirectives) {
-
-
- node.body.directives = oldDirectives;
- }
- var wasGeneratorFunction = node.generator;
- if (wasGeneratorFunction) {
- node.generator = false;
- }
- if (node.async) {
- node.async = false;
- }
- if (wasGeneratorFunction && t.isExpression(node)) {
- util.replaceWithOrRemove(path, t.callExpression(util.runtimeProperty("mark"), [node]));
- path.addComment("leading", "#__PURE__");
- }
- var insertedLocs = emitter.getInsertedLocs();
- path.traverse({
- NumericLiteral: function NumericLiteral(path) {
- if (!insertedLocs.has(path.node)) {
- return;
- }
- path.replaceWith(t.numericLiteral(path.node.value));
- }
- });
-
-
- path.requeue();
- })
- }
- };
- };
- function getOuterFnExpr(funPath) {
- var t = util.getTypes();
- var node = funPath.node;
- t.assertFunction(node);
- if (!node.id) {
-
-
- node.id = funPath.scope.parent.generateUidIdentifier("callee");
- }
- if (node.generator &&
- t.isFunctionDeclaration(node)) {
-
- return getMarkedFunctionId(funPath);
- }
- return t.clone(node.id);
- }
- var getMarkInfo = (0, _private.makeAccessor)();
- function getMarkedFunctionId(funPath) {
- var t = util.getTypes();
- var node = funPath.node;
- t.assertIdentifier(node.id);
- var blockPath = funPath.findParent(function (path) {
- return path.isProgram() || path.isBlockStatement();
- });
- if (!blockPath) {
- return node.id;
- }
- var block = blockPath.node;
- _assert.default.ok(Array.isArray(block.body));
- var info = getMarkInfo(block);
- if (!info.decl) {
- info.decl = t.variableDeclaration("var", []);
- blockPath.unshiftContainer("body", info.decl);
- info.declPath = blockPath.get("body.0");
- }
- _assert.default.strictEqual(info.declPath.node, info.decl);
- var markedId = blockPath.scope.generateUidIdentifier("marked");
- var markCallExp = t.callExpression(util.runtimeProperty("mark"), [t.clone(node.id)]);
- var index = info.decl.declarations.push(t.variableDeclarator(markedId, markCallExp)) - 1;
- var markCallExpPath = info.declPath.get("declarations." + index + ".init");
- _assert.default.strictEqual(markCallExpPath.node, markCallExp);
- markCallExpPath.addComment("leading", "#__PURE__");
- return t.clone(markedId);
- }
- var argumentsThisVisitor = {
- "FunctionExpression|FunctionDeclaration": function FunctionExpressionFunctionDeclaration(path) {
- path.skip();
- },
- Identifier: function Identifier(path, state) {
- if (path.node.name === "arguments" && util.isReference(path)) {
- util.replaceWithOrRemove(path, state.getArgsId());
- state.usesArguments = true;
- }
- },
- ThisExpression: function ThisExpression(path, state) {
- state.usesThis = true;
- }
- };
- var functionSentVisitor = {
- MetaProperty: function MetaProperty(path) {
- var node = path.node;
- if (node.meta.name === "function" && node.property.name === "sent") {
- var t = util.getTypes();
- util.replaceWithOrRemove(path, t.memberExpression(t.clone(this.context), t.identifier("_sent")));
- }
- }
- };
- var awaitVisitor = {
- Function: function Function(path) {
- path.skip();
- },
- AwaitExpression: function AwaitExpression(path) {
- var t = util.getTypes();
- var argument = path.node.argument;
-
-
- util.replaceWithOrRemove(path, t.yieldExpression(t.callExpression(util.runtimeProperty("awrap"), [argument]), false));
- }
- };
|