|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +Object.defineProperty(exports, "__esModule", { |
| 4 | + value: true |
| 5 | +}); |
| 6 | +exports.default = void 0; |
| 7 | + |
| 8 | +var _default = function _default(_ref) { |
| 9 | + var t = _ref.types; |
| 10 | + var unreturnableStatements = new Set(["DebuggerStatement", "WithStatement", "ReturnStatement", "LabeledStatement", "BreakStatement", "ContinueStatement", "ThrowStatement"]); |
| 11 | + var returnableStatements = new Set(["IfStatement", "SwitchStatement", "TryStatement", "WhileStatement", "DoWhileStatement", "ForStatement", "ForInStatement", "ForOfStatement"]); |
| 12 | + |
| 13 | + var last = function last(array) { |
| 14 | + return array[array.length - 1]; |
| 15 | + }; |
| 16 | + |
| 17 | + var lastNotEmptyIndex = function lastNotEmptyIndex(nodes) { |
| 18 | + for (var index = nodes.length - 1; index >= 0; index -= 1) { |
| 19 | + if (!t.isEmptyStatement(nodes[index])) return index; |
| 20 | + } |
| 21 | + }; // like babel-types#toExpression, but preserves function expressions |
| 22 | + |
| 23 | + |
| 24 | + var toExpression = function toExpression(node) { |
| 25 | + if (t.isExpressionStatement(node)) node = node.expression; |
| 26 | + |
| 27 | + if (t.isClass(node)) { |
| 28 | + node.type = "ClassExpression"; |
| 29 | + } else if (t.isFunctionDeclaration(node)) { |
| 30 | + node.type = "FunctionExpression"; |
| 31 | + } |
| 32 | + |
| 33 | + if (t.isExpression(node)) return node; |
| 34 | + throw new Error("cannot turn ".concat(node.type, " to an expression")); |
| 35 | + }; |
| 36 | + |
| 37 | + return { |
| 38 | + visitor: { |
| 39 | + Function: function Function(path) { |
| 40 | + var node = path.node; // arrow function expression |
| 41 | + |
| 42 | + if (node.expression) return; |
| 43 | + var _node$body = node.body, |
| 44 | + body = _node$body.body, |
| 45 | + directives = _node$body.directives; |
| 46 | + |
| 47 | + try { |
| 48 | + if (body.length === 0) { |
| 49 | + // empty function |
| 50 | + if (directives.length === 0) return; // function with directives only |
| 51 | + |
| 52 | + var directive = directives.pop(); |
| 53 | + body.push(t.returnStatement(t.stringLiteral(directive.value.value))); |
| 54 | + return; |
| 55 | + } |
| 56 | + } catch (error) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + var lastIndex = lastNotEmptyIndex(body); |
| 61 | + var lastPath = path.get("body.body.".concat(lastIndex)); |
| 62 | + var lastNode = body[lastIndex]; // skip unreturnable statements |
| 63 | + |
| 64 | + try { |
| 65 | + if (unreturnableStatements.has(lastNode.type)) return; |
| 66 | + } catch (error) { |
| 67 | + return; |
| 68 | + } // convert returnable statements |
| 69 | + |
| 70 | + |
| 71 | + if (returnableStatements.has(lastNode.type)) { |
| 72 | + var completionRecords = lastPath.getCompletionRecords(); |
| 73 | + var returnUid = null; |
| 74 | + completionRecords.forEach(function (subPath) { |
| 75 | + if (!subPath.isExpressionStatement()) return; |
| 76 | + var isLoop = subPath.findParent(function (subPath) { |
| 77 | + return subPath.isLoop(); |
| 78 | + }); |
| 79 | + |
| 80 | + if (isLoop) { |
| 81 | + if (!returnUid) returnUid = path.scope.generateDeclaredUidIdentifier("ret"); |
| 82 | + subPath.get("expression").replaceWith(t.assignmentExpression("=", returnUid, subPath.node.expression)); |
| 83 | + } else { |
| 84 | + subPath.replaceWith(t.returnStatement(subPath.node.expression)); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + if (returnUid) { |
| 89 | + path.get("body").pushContainer("body", t.returnStatement(returnUid)); |
| 90 | + } |
| 91 | + |
| 92 | + return; |
| 93 | + } // variables declaration |
| 94 | + |
| 95 | + |
| 96 | + if (t.isVariableDeclaration(lastNode)) { |
| 97 | + var _last = last(lastNode.declarations), |
| 98 | + id = _last.id; |
| 99 | + |
| 100 | + if (t.isArrayPattern(id)) { |
| 101 | + id = last(id.elements).argument; |
| 102 | + } |
| 103 | + |
| 104 | + if (t.isObjectPattern(id)) { |
| 105 | + id = last(id.properties).argument; |
| 106 | + } |
| 107 | + |
| 108 | + body.push(t.returnStatement(id)); |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + body[lastIndex] = t.returnStatement(toExpression(lastNode)); |
| 113 | + } |
| 114 | + } |
| 115 | + }; |
| 116 | +}; |
| 117 | + |
| 118 | +exports.default = _default; |
0 commit comments