Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

conversion.js 13KB

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