conversion.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.toComputedKey = toComputedKey;
  6. exports.ensureBlock = ensureBlock;
  7. exports.arrowFunctionToShadowed = arrowFunctionToShadowed;
  8. exports.unwrapFunctionEnvironment = unwrapFunctionEnvironment;
  9. exports.arrowFunctionToExpression = arrowFunctionToExpression;
  10. function t() {
  11. var data = _interopRequireWildcard(require("@babel/types"));
  12. t = function t() {
  13. return data;
  14. };
  15. return data;
  16. }
  17. function _helperFunctionName() {
  18. var data = _interopRequireDefault(require("@babel/helper-function-name"));
  19. _helperFunctionName = function _helperFunctionName() {
  20. return data;
  21. };
  22. return data;
  23. }
  24. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  25. 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; } }
  26. function toComputedKey() {
  27. var node = this.node;
  28. var key;
  29. if (this.isMemberExpression()) {
  30. key = node.property;
  31. } else if (this.isProperty() || this.isMethod()) {
  32. key = node.key;
  33. } else {
  34. throw new ReferenceError("todo");
  35. }
  36. if (!node.computed) {
  37. if (t().isIdentifier(key)) key = t().stringLiteral(key.name);
  38. }
  39. return key;
  40. }
  41. function ensureBlock() {
  42. var body = this.get("body");
  43. var bodyNode = body.node;
  44. if (Array.isArray(body)) {
  45. throw new Error("Can't convert array path to a block statement");
  46. }
  47. if (!bodyNode) {
  48. throw new Error("Can't convert node without a body");
  49. }
  50. if (body.isBlockStatement()) {
  51. return bodyNode;
  52. }
  53. var statements = [];
  54. var stringPath = "body";
  55. var key;
  56. var listKey;
  57. if (body.isStatement()) {
  58. listKey = "body";
  59. key = 0;
  60. statements.push(body.node);
  61. } else {
  62. stringPath += ".body.0";
  63. if (this.isFunction()) {
  64. key = "argument";
  65. statements.push(t().returnStatement(body.node));
  66. } else {
  67. key = "expression";
  68. statements.push(t().expressionStatement(body.node));
  69. }
  70. }
  71. this.node.body = t().blockStatement(statements);
  72. var parentPath = this.get(stringPath);
  73. body.setup(parentPath, listKey ? parentPath.node[listKey] : parentPath.node, listKey, key);
  74. return this.node;
  75. }
  76. function arrowFunctionToShadowed() {
  77. if (!this.isArrowFunctionExpression()) return;
  78. this.arrowFunctionToExpression();
  79. }
  80. function unwrapFunctionEnvironment() {
  81. if (!this.isArrowFunctionExpression() && !this.isFunctionExpression() && !this.isFunctionDeclaration()) {
  82. throw this.buildCodeFrameError("Can only unwrap the environment of a function.");
  83. }
  84. hoistFunctionEnvironment(this);
  85. }
  86. function arrowFunctionToExpression(_temp) {
  87. var _ref = _temp === void 0 ? {} : _temp,
  88. _ref$allowInsertArrow = _ref.allowInsertArrow,
  89. allowInsertArrow = _ref$allowInsertArrow === void 0 ? true : _ref$allowInsertArrow,
  90. _ref$specCompliant = _ref.specCompliant,
  91. specCompliant = _ref$specCompliant === void 0 ? false : _ref$specCompliant;
  92. if (!this.isArrowFunctionExpression()) {
  93. throw this.buildCodeFrameError("Cannot convert non-arrow function to a function expression.");
  94. }
  95. var thisBinding = hoistFunctionEnvironment(this, specCompliant, allowInsertArrow);
  96. this.ensureBlock();
  97. this.node.type = "FunctionExpression";
  98. if (specCompliant) {
  99. var checkBinding = thisBinding ? null : this.parentPath.scope.generateUidIdentifier("arrowCheckId");
  100. if (checkBinding) {
  101. this.parentPath.scope.push({
  102. id: checkBinding,
  103. init: t().objectExpression([])
  104. });
  105. }
  106. this.get("body").unshiftContainer("body", t().expressionStatement(t().callExpression(this.hub.file.addHelper("newArrowCheck"), [t().thisExpression(), checkBinding ? t().identifier(checkBinding.name) : t().identifier(thisBinding)])));
  107. this.replaceWith(t().callExpression(t().memberExpression((0, _helperFunctionName().default)(this, true) || this.node, t().identifier("bind")), [checkBinding ? t().identifier(checkBinding.name) : t().thisExpression()]));
  108. }
  109. }
  110. function hoistFunctionEnvironment(fnPath, specCompliant, allowInsertArrow) {
  111. if (specCompliant === void 0) {
  112. specCompliant = false;
  113. }
  114. if (allowInsertArrow === void 0) {
  115. allowInsertArrow = true;
  116. }
  117. var thisEnvFn = fnPath.findParent(function (p) {
  118. return p.isFunction() && !p.isArrowFunctionExpression() || p.isProgram() || p.isClassProperty({
  119. static: false
  120. });
  121. });
  122. var inConstructor = thisEnvFn && thisEnvFn.node.kind === "constructor";
  123. if (thisEnvFn.isClassProperty()) {
  124. throw fnPath.buildCodeFrameError("Unable to transform arrow inside class property");
  125. }
  126. var _getScopeInformation = getScopeInformation(fnPath),
  127. thisPaths = _getScopeInformation.thisPaths,
  128. argumentsPaths = _getScopeInformation.argumentsPaths,
  129. newTargetPaths = _getScopeInformation.newTargetPaths,
  130. superProps = _getScopeInformation.superProps,
  131. superCalls = _getScopeInformation.superCalls;
  132. if (inConstructor && superCalls.length > 0) {
  133. if (!allowInsertArrow) {
  134. throw superCalls[0].buildCodeFrameError("Unable to handle nested super() usage in arrow");
  135. }
  136. var allSuperCalls = [];
  137. thisEnvFn.traverse({
  138. Function: function Function(child) {
  139. if (child.isArrowFunctionExpression()) return;
  140. child.skip();
  141. },
  142. ClassProperty: function ClassProperty(child) {
  143. if (child.node.static) return;
  144. child.skip();
  145. },
  146. CallExpression: function CallExpression(child) {
  147. if (!child.get("callee").isSuper()) return;
  148. allSuperCalls.push(child);
  149. }
  150. });
  151. var superBinding = getSuperBinding(thisEnvFn);
  152. allSuperCalls.forEach(function (superCall) {
  153. var callee = t().identifier(superBinding);
  154. callee.loc = superCall.node.callee.loc;
  155. superCall.get("callee").replaceWith(callee);
  156. });
  157. }
  158. var thisBinding;
  159. if (thisPaths.length > 0 || specCompliant) {
  160. thisBinding = getThisBinding(thisEnvFn, inConstructor);
  161. if (!specCompliant || inConstructor && hasSuperClass(thisEnvFn)) {
  162. thisPaths.forEach(function (thisChild) {
  163. var thisRef = thisChild.isJSX() ? t().jsxIdentifier(thisBinding) : t().identifier(thisBinding);
  164. thisRef.loc = thisChild.node.loc;
  165. thisChild.replaceWith(thisRef);
  166. });
  167. if (specCompliant) thisBinding = null;
  168. }
  169. }
  170. if (argumentsPaths.length > 0) {
  171. var argumentsBinding = getBinding(thisEnvFn, "arguments", function () {
  172. return t().identifier("arguments");
  173. });
  174. argumentsPaths.forEach(function (argumentsChild) {
  175. var argsRef = t().identifier(argumentsBinding);
  176. argsRef.loc = argumentsChild.node.loc;
  177. argumentsChild.replaceWith(argsRef);
  178. });
  179. }
  180. if (newTargetPaths.length > 0) {
  181. var newTargetBinding = getBinding(thisEnvFn, "newtarget", function () {
  182. return t().metaProperty(t().identifier("new"), t().identifier("target"));
  183. });
  184. newTargetPaths.forEach(function (targetChild) {
  185. var targetRef = t().identifier(newTargetBinding);
  186. targetRef.loc = targetChild.node.loc;
  187. targetChild.replaceWith(targetRef);
  188. });
  189. }
  190. if (superProps.length > 0) {
  191. if (!allowInsertArrow) {
  192. throw superProps[0].buildCodeFrameError("Unable to handle nested super.prop usage");
  193. }
  194. var flatSuperProps = superProps.reduce(function (acc, superProp) {
  195. return acc.concat(standardizeSuperProperty(superProp));
  196. }, []);
  197. flatSuperProps.forEach(function (superProp) {
  198. var key = superProp.node.computed ? "" : superProp.get("property").node.name;
  199. if (superProp.parentPath.isCallExpression({
  200. callee: superProp.node
  201. })) {
  202. var _superBinding = getSuperPropCallBinding(thisEnvFn, key);
  203. if (superProp.node.computed) {
  204. var prop = superProp.get("property").node;
  205. superProp.replaceWith(t().identifier(_superBinding));
  206. superProp.parentPath.node.arguments.unshift(prop);
  207. } else {
  208. superProp.replaceWith(t().identifier(_superBinding));
  209. }
  210. } else {
  211. var isAssignment = superProp.parentPath.isAssignmentExpression({
  212. left: superProp.node
  213. });
  214. var _superBinding2 = getSuperPropBinding(thisEnvFn, isAssignment, key);
  215. var args = [];
  216. if (superProp.node.computed) {
  217. args.push(superProp.get("property").node);
  218. }
  219. if (isAssignment) {
  220. var value = superProp.parentPath.node.right;
  221. args.push(value);
  222. superProp.parentPath.replaceWith(t().callExpression(t().identifier(_superBinding2), args));
  223. } else {
  224. superProp.replaceWith(t().callExpression(t().identifier(_superBinding2), args));
  225. }
  226. }
  227. });
  228. }
  229. return thisBinding;
  230. }
  231. function standardizeSuperProperty(superProp) {
  232. if (superProp.parentPath.isAssignmentExpression() && superProp.parentPath.node.operator !== "=") {
  233. var assignmentPath = superProp.parentPath;
  234. var op = assignmentPath.node.operator.slice(0, -1);
  235. var value = assignmentPath.node.right;
  236. assignmentPath.node.operator = "=";
  237. if (superProp.node.computed) {
  238. var tmp = superProp.scope.generateDeclaredUidIdentifier("tmp");
  239. assignmentPath.get("left").replaceWith(t().memberExpression(superProp.node.object, t().assignmentExpression("=", tmp, superProp.node.property), true));
  240. assignmentPath.get("right").replaceWith(t().binaryExpression(op, t().memberExpression(superProp.node.object, t().identifier(tmp.name), true), value));
  241. } else {
  242. assignmentPath.get("left").replaceWith(t().memberExpression(superProp.node.object, superProp.node.property));
  243. assignmentPath.get("right").replaceWith(t().binaryExpression(op, t().memberExpression(superProp.node.object, t().identifier(superProp.node.property.name)), value));
  244. }
  245. return [assignmentPath.get("left"), assignmentPath.get("right").get("left")];
  246. } else if (superProp.parentPath.isUpdateExpression()) {
  247. var updateExpr = superProp.parentPath;
  248. var _tmp = superProp.scope.generateDeclaredUidIdentifier("tmp");
  249. var computedKey = superProp.node.computed ? superProp.scope.generateDeclaredUidIdentifier("prop") : null;
  250. var parts = [t().assignmentExpression("=", _tmp, t().memberExpression(superProp.node.object, computedKey ? t().assignmentExpression("=", computedKey, superProp.node.property) : superProp.node.property, superProp.node.computed)), t().assignmentExpression("=", t().memberExpression(superProp.node.object, computedKey ? t().identifier(computedKey.name) : superProp.node.property, superProp.node.computed), t().binaryExpression("+", t().identifier(_tmp.name), t().numericLiteral(1)))];
  251. if (!superProp.parentPath.node.prefix) {
  252. parts.push(t().identifier(_tmp.name));
  253. }
  254. updateExpr.replaceWith(t().sequenceExpression(parts));
  255. var left = updateExpr.get("expressions.0.right");
  256. var right = updateExpr.get("expressions.1.left");
  257. return [left, right];
  258. }
  259. return [superProp];
  260. }
  261. function hasSuperClass(thisEnvFn) {
  262. return thisEnvFn.isClassMethod() && !!thisEnvFn.parentPath.parentPath.node.superClass;
  263. }
  264. function getThisBinding(thisEnvFn, inConstructor) {
  265. return getBinding(thisEnvFn, "this", function (thisBinding) {
  266. if (!inConstructor || !hasSuperClass(thisEnvFn)) return t().thisExpression();
  267. var supers = new WeakSet();
  268. thisEnvFn.traverse({
  269. Function: function Function(child) {
  270. if (child.isArrowFunctionExpression()) return;
  271. child.skip();
  272. },
  273. ClassProperty: function ClassProperty(child) {
  274. if (child.node.static) return;
  275. child.skip();
  276. },
  277. CallExpression: function CallExpression(child) {
  278. if (!child.get("callee").isSuper()) return;
  279. if (supers.has(child.node)) return;
  280. supers.add(child.node);
  281. child.replaceWith(t().assignmentExpression("=", t().identifier(thisBinding), child.node));
  282. }
  283. });
  284. });
  285. }
  286. function getSuperBinding(thisEnvFn) {
  287. return getBinding(thisEnvFn, "supercall", function () {
  288. var argsBinding = thisEnvFn.scope.generateUidIdentifier("args");
  289. return t().arrowFunctionExpression([t().restElement(argsBinding)], t().callExpression(t().super(), [t().spreadElement(t().identifier(argsBinding.name))]));
  290. });
  291. }
  292. function getSuperPropCallBinding(thisEnvFn, propName) {
  293. return getBinding(thisEnvFn, "superprop_call:" + (propName || ""), function () {
  294. var argsBinding = thisEnvFn.scope.generateUidIdentifier("args");
  295. var argsList = [t().restElement(argsBinding)];
  296. var fnBody;
  297. if (propName) {
  298. fnBody = t().callExpression(t().memberExpression(t().super(), t().identifier(propName)), [t().spreadElement(t().identifier(argsBinding.name))]);
  299. } else {
  300. var method = thisEnvFn.scope.generateUidIdentifier("prop");
  301. argsList.unshift(method);
  302. fnBody = t().callExpression(t().memberExpression(t().super(), t().identifier(method.name), true), [t().spreadElement(t().identifier(argsBinding.name))]);
  303. }
  304. return t().arrowFunctionExpression(argsList, fnBody);
  305. });
  306. }
  307. function getSuperPropBinding(thisEnvFn, isAssignment, propName) {
  308. var op = isAssignment ? "set" : "get";
  309. return getBinding(thisEnvFn, "superprop_" + op + ":" + (propName || ""), function () {
  310. var argsList = [];
  311. var fnBody;
  312. if (propName) {
  313. fnBody = t().memberExpression(t().super(), t().identifier(propName));
  314. } else {
  315. var method = thisEnvFn.scope.generateUidIdentifier("prop");
  316. argsList.unshift(method);
  317. fnBody = t().memberExpression(t().super(), t().identifier(method.name), true);
  318. }
  319. if (isAssignment) {
  320. var valueIdent = thisEnvFn.scope.generateUidIdentifier("value");
  321. argsList.push(valueIdent);
  322. fnBody = t().assignmentExpression("=", fnBody, t().identifier(valueIdent.name));
  323. }
  324. return t().arrowFunctionExpression(argsList, fnBody);
  325. });
  326. }
  327. function getBinding(thisEnvFn, key, init) {
  328. var cacheKey = "binding:" + key;
  329. var data = thisEnvFn.getData(cacheKey);
  330. if (!data) {
  331. var id = thisEnvFn.scope.generateUidIdentifier(key);
  332. data = id.name;
  333. thisEnvFn.setData(cacheKey, data);
  334. thisEnvFn.scope.push({
  335. id: id,
  336. init: init(data)
  337. });
  338. }
  339. return data;
  340. }
  341. function getScopeInformation(fnPath) {
  342. var thisPaths = [];
  343. var argumentsPaths = [];
  344. var newTargetPaths = [];
  345. var superProps = [];
  346. var superCalls = [];
  347. fnPath.traverse({
  348. ClassProperty: function ClassProperty(child) {
  349. if (child.node.static) return;
  350. child.skip();
  351. },
  352. Function: function Function(child) {
  353. if (child.isArrowFunctionExpression()) return;
  354. child.skip();
  355. },
  356. ThisExpression: function ThisExpression(child) {
  357. thisPaths.push(child);
  358. },
  359. JSXIdentifier: function JSXIdentifier(child) {
  360. if (child.node.name !== "this") return;
  361. if (!child.parentPath.isJSXMemberExpression({
  362. object: child.node
  363. }) && !child.parentPath.isJSXOpeningElement({
  364. name: child.node
  365. })) {
  366. return;
  367. }
  368. thisPaths.push(child);
  369. },
  370. CallExpression: function CallExpression(child) {
  371. if (child.get("callee").isSuper()) superCalls.push(child);
  372. },
  373. MemberExpression: function MemberExpression(child) {
  374. if (child.get("object").isSuper()) superProps.push(child);
  375. },
  376. ReferencedIdentifier: function ReferencedIdentifier(child) {
  377. if (child.node.name !== "arguments") return;
  378. argumentsPaths.push(child);
  379. },
  380. MetaProperty: function MetaProperty(child) {
  381. if (!child.get("meta").isIdentifier({
  382. name: "new"
  383. })) return;
  384. if (!child.get("property").isIdentifier({
  385. name: "target"
  386. })) return;
  387. newTargetPaths.push(child);
  388. }
  389. });
  390. return {
  391. thisPaths: thisPaths,
  392. argumentsPaths: argumentsPaths,
  393. newTargetPaths: newTargetPaths,
  394. superProps: superProps,
  395. superCalls: superCalls
  396. };
  397. }