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.

replacement.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.replaceWithMultiple = replaceWithMultiple;
  6. exports.replaceWithSourceString = replaceWithSourceString;
  7. exports.replaceWith = replaceWith;
  8. exports._replaceWith = _replaceWith;
  9. exports.replaceExpressionWithStatements = replaceExpressionWithStatements;
  10. exports.replaceInline = replaceInline;
  11. var _codeFrame = require("@babel/code-frame");
  12. var _index = require("../index");
  13. var _index2 = require("./index");
  14. var _cache = require("../cache");
  15. var _parser = require("@babel/parser");
  16. var t = require("@babel/types");
  17. var _helperHoistVariables = require("@babel/helper-hoist-variables");
  18. function replaceWithMultiple(nodes) {
  19. var _pathCache$get;
  20. this.resync();
  21. nodes = this._verifyNodeList(nodes);
  22. t.inheritLeadingComments(nodes[0], this.node);
  23. t.inheritTrailingComments(nodes[nodes.length - 1], this.node);
  24. (_pathCache$get = _cache.path.get(this.parent)) == null ? void 0 : _pathCache$get.delete(this.node);
  25. this.node = this.container[this.key] = null;
  26. const paths = this.insertAfter(nodes);
  27. if (this.node) {
  28. this.requeue();
  29. } else {
  30. this.remove();
  31. }
  32. return paths;
  33. }
  34. function replaceWithSourceString(replacement) {
  35. this.resync();
  36. try {
  37. replacement = `(${replacement})`;
  38. replacement = (0, _parser.parse)(replacement);
  39. } catch (err) {
  40. const loc = err.loc;
  41. if (loc) {
  42. err.message += " - make sure this is an expression.\n" + (0, _codeFrame.codeFrameColumns)(replacement, {
  43. start: {
  44. line: loc.line,
  45. column: loc.column + 1
  46. }
  47. });
  48. err.code = "BABEL_REPLACE_SOURCE_ERROR";
  49. }
  50. throw err;
  51. }
  52. replacement = replacement.program.body[0].expression;
  53. _index.default.removeProperties(replacement);
  54. return this.replaceWith(replacement);
  55. }
  56. function replaceWith(replacement) {
  57. this.resync();
  58. if (this.removed) {
  59. throw new Error("You can't replace this node, we've already removed it");
  60. }
  61. if (replacement instanceof _index2.default) {
  62. replacement = replacement.node;
  63. }
  64. if (!replacement) {
  65. throw new Error("You passed `path.replaceWith()` a falsy node, use `path.remove()` instead");
  66. }
  67. if (this.node === replacement) {
  68. return [this];
  69. }
  70. if (this.isProgram() && !t.isProgram(replacement)) {
  71. throw new Error("You can only replace a Program root node with another Program node");
  72. }
  73. if (Array.isArray(replacement)) {
  74. throw new Error("Don't use `path.replaceWith()` with an array of nodes, use `path.replaceWithMultiple()`");
  75. }
  76. if (typeof replacement === "string") {
  77. throw new Error("Don't use `path.replaceWith()` with a source string, use `path.replaceWithSourceString()`");
  78. }
  79. let nodePath = "";
  80. if (this.isNodeType("Statement") && t.isExpression(replacement)) {
  81. if (!this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement) && !this.parentPath.isExportDefaultDeclaration()) {
  82. replacement = t.expressionStatement(replacement);
  83. nodePath = "expression";
  84. }
  85. }
  86. if (this.isNodeType("Expression") && t.isStatement(replacement)) {
  87. if (!this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement)) {
  88. return this.replaceExpressionWithStatements([replacement]);
  89. }
  90. }
  91. const oldNode = this.node;
  92. if (oldNode) {
  93. t.inheritsComments(replacement, oldNode);
  94. t.removeComments(oldNode);
  95. }
  96. this._replaceWith(replacement);
  97. this.type = replacement.type;
  98. this.setScope();
  99. this.requeue();
  100. return [nodePath ? this.get(nodePath) : this];
  101. }
  102. function _replaceWith(node) {
  103. var _pathCache$get2;
  104. if (!this.container) {
  105. throw new ReferenceError("Container is falsy");
  106. }
  107. if (this.inList) {
  108. t.validate(this.parent, this.key, [node]);
  109. } else {
  110. t.validate(this.parent, this.key, node);
  111. }
  112. this.debug(`Replace with ${node == null ? void 0 : node.type}`);
  113. (_pathCache$get2 = _cache.path.get(this.parent)) == null ? void 0 : _pathCache$get2.set(node, this).delete(this.node);
  114. this.node = this.container[this.key] = node;
  115. }
  116. function replaceExpressionWithStatements(nodes) {
  117. this.resync();
  118. const toSequenceExpression = t.toSequenceExpression(nodes, this.scope);
  119. if (toSequenceExpression) {
  120. return this.replaceWith(toSequenceExpression)[0].get("expressions");
  121. }
  122. const functionParent = this.getFunctionParent();
  123. const isParentAsync = functionParent == null ? void 0 : functionParent.is("async");
  124. const isParentGenerator = functionParent == null ? void 0 : functionParent.is("generator");
  125. const container = t.arrowFunctionExpression([], t.blockStatement(nodes));
  126. this.replaceWith(t.callExpression(container, []));
  127. const callee = this.get("callee");
  128. (0, _helperHoistVariables.default)(callee.get("body"), id => {
  129. this.scope.push({
  130. id
  131. });
  132. }, "var");
  133. const completionRecords = this.get("callee").getCompletionRecords();
  134. for (const path of completionRecords) {
  135. if (!path.isExpressionStatement()) continue;
  136. const loop = path.findParent(path => path.isLoop());
  137. if (loop) {
  138. let uid = loop.getData("expressionReplacementReturnUid");
  139. if (!uid) {
  140. uid = callee.scope.generateDeclaredUidIdentifier("ret");
  141. callee.get("body").pushContainer("body", t.returnStatement(t.cloneNode(uid)));
  142. loop.setData("expressionReplacementReturnUid", uid);
  143. } else {
  144. uid = t.identifier(uid.name);
  145. }
  146. path.get("expression").replaceWith(t.assignmentExpression("=", t.cloneNode(uid), path.node.expression));
  147. } else {
  148. path.replaceWith(t.returnStatement(path.node.expression));
  149. }
  150. }
  151. callee.arrowFunctionToExpression();
  152. const newCallee = callee;
  153. const needToAwaitFunction = isParentAsync && _index.default.hasType(this.get("callee.body").node, "AwaitExpression", t.FUNCTION_TYPES);
  154. const needToYieldFunction = isParentGenerator && _index.default.hasType(this.get("callee.body").node, "YieldExpression", t.FUNCTION_TYPES);
  155. if (needToAwaitFunction) {
  156. newCallee.set("async", true);
  157. if (!needToYieldFunction) {
  158. this.replaceWith(t.awaitExpression(this.node));
  159. }
  160. }
  161. if (needToYieldFunction) {
  162. newCallee.set("generator", true);
  163. this.replaceWith(t.yieldExpression(this.node, true));
  164. }
  165. return newCallee.get("body.body");
  166. }
  167. function replaceInline(nodes) {
  168. this.resync();
  169. if (Array.isArray(nodes)) {
  170. if (Array.isArray(this.container)) {
  171. nodes = this._verifyNodeList(nodes);
  172. const paths = this._containerInsertAfter(nodes);
  173. this.remove();
  174. return paths;
  175. } else {
  176. return this.replaceWithMultiple(nodes);
  177. }
  178. } else {
  179. return this.replaceWith(nodes);
  180. }
  181. }