rest.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = convertFunctionRest;
  6. function _core() {
  7. const data = require("@babel/core");
  8. _core = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. const buildRest = (0, _core().template)(`
  14. for (var LEN = ARGUMENTS.length,
  15. ARRAY = new Array(ARRAY_LEN),
  16. KEY = START;
  17. KEY < LEN;
  18. KEY++) {
  19. ARRAY[ARRAY_KEY] = ARGUMENTS[KEY];
  20. }
  21. `);
  22. const restIndex = (0, _core().template)(`
  23. (INDEX < OFFSET || ARGUMENTS.length <= INDEX) ? undefined : ARGUMENTS[INDEX]
  24. `);
  25. const restIndexImpure = (0, _core().template)(`
  26. REF = INDEX, (REF < OFFSET || ARGUMENTS.length <= REF) ? undefined : ARGUMENTS[REF]
  27. `);
  28. const restLength = (0, _core().template)(`
  29. ARGUMENTS.length <= OFFSET ? 0 : ARGUMENTS.length - OFFSET
  30. `);
  31. function referencesRest(path, state) {
  32. if (path.node.name === state.name) {
  33. return path.scope.bindingIdentifierEquals(state.name, state.outerBinding);
  34. }
  35. return false;
  36. }
  37. const memberExpressionOptimisationVisitor = {
  38. Scope(path, state) {
  39. if (!path.scope.bindingIdentifierEquals(state.name, state.outerBinding)) {
  40. path.skip();
  41. }
  42. },
  43. Flow(path) {
  44. if (path.isTypeCastExpression()) return;
  45. path.skip();
  46. },
  47. Function(path, state) {
  48. const oldNoOptimise = state.noOptimise;
  49. state.noOptimise = true;
  50. path.traverse(memberExpressionOptimisationVisitor, state);
  51. state.noOptimise = oldNoOptimise;
  52. path.skip();
  53. },
  54. ReferencedIdentifier(path, state) {
  55. const {
  56. node
  57. } = path;
  58. if (node.name === "arguments") {
  59. state.deopted = true;
  60. }
  61. if (!referencesRest(path, state)) return;
  62. if (state.noOptimise) {
  63. state.deopted = true;
  64. } else {
  65. const {
  66. parentPath
  67. } = path;
  68. if (parentPath.listKey === "params" && parentPath.key < state.offset) {
  69. return;
  70. }
  71. if (parentPath.isMemberExpression({
  72. object: node
  73. })) {
  74. const grandparentPath = parentPath.parentPath;
  75. const argsOptEligible = !state.deopted && !(grandparentPath.isAssignmentExpression() && parentPath.node === grandparentPath.node.left || grandparentPath.isLVal() || grandparentPath.isForXStatement() || grandparentPath.isUpdateExpression() || grandparentPath.isUnaryExpression({
  76. operator: "delete"
  77. }) || (grandparentPath.isCallExpression() || grandparentPath.isNewExpression()) && parentPath.node === grandparentPath.node.callee);
  78. if (argsOptEligible) {
  79. if (parentPath.node.computed) {
  80. if (parentPath.get("property").isBaseType("number")) {
  81. state.candidates.push({
  82. cause: "indexGetter",
  83. path
  84. });
  85. return;
  86. }
  87. } else if (parentPath.node.property.name === "length") {
  88. state.candidates.push({
  89. cause: "lengthGetter",
  90. path
  91. });
  92. return;
  93. }
  94. }
  95. }
  96. if (state.offset === 0 && parentPath.isSpreadElement()) {
  97. const call = parentPath.parentPath;
  98. if (call.isCallExpression() && call.node.arguments.length === 1) {
  99. state.candidates.push({
  100. cause: "argSpread",
  101. path
  102. });
  103. return;
  104. }
  105. }
  106. state.references.push(path);
  107. }
  108. },
  109. BindingIdentifier(path, state) {
  110. if (referencesRest(path, state)) {
  111. state.deopted = true;
  112. }
  113. }
  114. };
  115. function hasRest(node) {
  116. const length = node.params.length;
  117. return length > 0 && _core().types.isRestElement(node.params[length - 1]);
  118. }
  119. function optimiseIndexGetter(path, argsId, offset) {
  120. const offsetLiteral = _core().types.numericLiteral(offset);
  121. let index;
  122. if (_core().types.isNumericLiteral(path.parent.property)) {
  123. index = _core().types.numericLiteral(path.parent.property.value + offset);
  124. } else if (offset === 0) {
  125. index = path.parent.property;
  126. } else {
  127. index = _core().types.binaryExpression("+", path.parent.property, _core().types.cloneNode(offsetLiteral));
  128. }
  129. const {
  130. scope
  131. } = path;
  132. if (!scope.isPure(index)) {
  133. const temp = scope.generateUidIdentifierBasedOnNode(index);
  134. scope.push({
  135. id: temp,
  136. kind: "var"
  137. });
  138. path.parentPath.replaceWith(restIndexImpure({
  139. ARGUMENTS: argsId,
  140. OFFSET: offsetLiteral,
  141. INDEX: index,
  142. REF: _core().types.cloneNode(temp)
  143. }));
  144. } else {
  145. const parentPath = path.parentPath;
  146. parentPath.replaceWith(restIndex({
  147. ARGUMENTS: argsId,
  148. OFFSET: offsetLiteral,
  149. INDEX: index
  150. }));
  151. const offsetTestPath = parentPath.get("test").get("left");
  152. const valRes = offsetTestPath.evaluate();
  153. if (valRes.confident) {
  154. if (valRes.value === true) {
  155. parentPath.replaceWith(parentPath.scope.buildUndefinedNode());
  156. } else {
  157. parentPath.get("test").replaceWith(parentPath.get("test").get("right"));
  158. }
  159. }
  160. }
  161. }
  162. function optimiseLengthGetter(path, argsId, offset) {
  163. if (offset) {
  164. path.parentPath.replaceWith(restLength({
  165. ARGUMENTS: argsId,
  166. OFFSET: _core().types.numericLiteral(offset)
  167. }));
  168. } else {
  169. path.replaceWith(argsId);
  170. }
  171. }
  172. function convertFunctionRest(path) {
  173. const {
  174. node,
  175. scope
  176. } = path;
  177. if (!hasRest(node)) return false;
  178. let rest = node.params.pop().argument;
  179. const argsId = _core().types.identifier("arguments");
  180. if (_core().types.isPattern(rest)) {
  181. const pattern = rest;
  182. rest = scope.generateUidIdentifier("ref");
  183. const declar = _core().types.variableDeclaration("let", [_core().types.variableDeclarator(pattern, rest)]);
  184. node.body.body.unshift(declar);
  185. }
  186. const state = {
  187. references: [],
  188. offset: node.params.length,
  189. argumentsNode: argsId,
  190. outerBinding: scope.getBindingIdentifier(rest.name),
  191. candidates: [],
  192. name: rest.name,
  193. deopted: false
  194. };
  195. path.traverse(memberExpressionOptimisationVisitor, state);
  196. if (!state.deopted && !state.references.length) {
  197. for (const _ref of state.candidates) {
  198. const {
  199. path,
  200. cause
  201. } = _ref;
  202. const clonedArgsId = _core().types.cloneNode(argsId);
  203. switch (cause) {
  204. case "indexGetter":
  205. optimiseIndexGetter(path, clonedArgsId, state.offset);
  206. break;
  207. case "lengthGetter":
  208. optimiseLengthGetter(path, clonedArgsId, state.offset);
  209. break;
  210. default:
  211. path.replaceWith(clonedArgsId);
  212. }
  213. }
  214. return true;
  215. }
  216. state.references = state.references.concat(state.candidates.map(({
  217. path
  218. }) => path));
  219. const start = _core().types.numericLiteral(node.params.length);
  220. const key = scope.generateUidIdentifier("key");
  221. const len = scope.generateUidIdentifier("len");
  222. let arrKey, arrLen;
  223. if (node.params.length) {
  224. arrKey = _core().types.binaryExpression("-", _core().types.cloneNode(key), _core().types.cloneNode(start));
  225. arrLen = _core().types.conditionalExpression(_core().types.binaryExpression(">", _core().types.cloneNode(len), _core().types.cloneNode(start)), _core().types.binaryExpression("-", _core().types.cloneNode(len), _core().types.cloneNode(start)), _core().types.numericLiteral(0));
  226. } else {
  227. arrKey = _core().types.identifier(key.name);
  228. arrLen = _core().types.identifier(len.name);
  229. }
  230. const loop = buildRest({
  231. ARGUMENTS: argsId,
  232. ARRAY_KEY: arrKey,
  233. ARRAY_LEN: arrLen,
  234. START: start,
  235. ARRAY: rest,
  236. KEY: key,
  237. LEN: len
  238. });
  239. if (state.deopted) {
  240. node.body.body.unshift(loop);
  241. } else {
  242. let target = path.getEarliestCommonAncestorFrom(state.references).getStatementParent();
  243. target.findParent(path => {
  244. if (path.isLoop()) {
  245. target = path;
  246. } else {
  247. return path.isFunction();
  248. }
  249. });
  250. target.insertBefore(loop);
  251. }
  252. return true;
  253. }