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.

introspection.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.matchesPattern = matchesPattern;
  6. exports.has = has;
  7. exports.isStatic = isStatic;
  8. exports.isnt = isnt;
  9. exports.equals = equals;
  10. exports.isNodeType = isNodeType;
  11. exports.canHaveVariableDeclarationOrExpression = canHaveVariableDeclarationOrExpression;
  12. exports.canSwapBetweenExpressionAndStatement = canSwapBetweenExpressionAndStatement;
  13. exports.isCompletionRecord = isCompletionRecord;
  14. exports.isStatementOrBlock = isStatementOrBlock;
  15. exports.referencesImport = referencesImport;
  16. exports.getSource = getSource;
  17. exports.willIMaybeExecuteBefore = willIMaybeExecuteBefore;
  18. exports._guessExecutionStatusRelativeTo = _guessExecutionStatusRelativeTo;
  19. exports._guessExecutionStatusRelativeToDifferentFunctions = _guessExecutionStatusRelativeToDifferentFunctions;
  20. exports.resolve = resolve;
  21. exports._resolve = _resolve;
  22. exports.isConstantExpression = isConstantExpression;
  23. exports.isInStrictMode = isInStrictMode;
  24. exports.is = void 0;
  25. var t = require("@babel/types");
  26. function matchesPattern(pattern, allowPartial) {
  27. return t.matchesPattern(this.node, pattern, allowPartial);
  28. }
  29. function has(key) {
  30. const val = this.node && this.node[key];
  31. if (val && Array.isArray(val)) {
  32. return !!val.length;
  33. } else {
  34. return !!val;
  35. }
  36. }
  37. function isStatic() {
  38. return this.scope.isStatic(this.node);
  39. }
  40. const is = has;
  41. exports.is = is;
  42. function isnt(key) {
  43. return !this.has(key);
  44. }
  45. function equals(key, value) {
  46. return this.node[key] === value;
  47. }
  48. function isNodeType(type) {
  49. return t.isType(this.type, type);
  50. }
  51. function canHaveVariableDeclarationOrExpression() {
  52. return (this.key === "init" || this.key === "left") && this.parentPath.isFor();
  53. }
  54. function canSwapBetweenExpressionAndStatement(replacement) {
  55. if (this.key !== "body" || !this.parentPath.isArrowFunctionExpression()) {
  56. return false;
  57. }
  58. if (this.isExpression()) {
  59. return t.isBlockStatement(replacement);
  60. } else if (this.isBlockStatement()) {
  61. return t.isExpression(replacement);
  62. }
  63. return false;
  64. }
  65. function isCompletionRecord(allowInsideFunction) {
  66. let path = this;
  67. let first = true;
  68. do {
  69. const container = path.container;
  70. if (path.isFunction() && !first) {
  71. return !!allowInsideFunction;
  72. }
  73. first = false;
  74. if (Array.isArray(container) && path.key !== container.length - 1) {
  75. return false;
  76. }
  77. } while ((path = path.parentPath) && !path.isProgram());
  78. return true;
  79. }
  80. function isStatementOrBlock() {
  81. if (this.parentPath.isLabeledStatement() || t.isBlockStatement(this.container)) {
  82. return false;
  83. } else {
  84. return t.STATEMENT_OR_BLOCK_KEYS.includes(this.key);
  85. }
  86. }
  87. function referencesImport(moduleSource, importName) {
  88. if (!this.isReferencedIdentifier()) {
  89. if ((this.isMemberExpression() || this.isOptionalMemberExpression()) && (this.node.computed ? t.isStringLiteral(this.node.property, {
  90. value: importName
  91. }) : this.node.property.name === importName)) {
  92. const object = this.get("object");
  93. return object.isReferencedIdentifier() && object.referencesImport(moduleSource, "*");
  94. }
  95. return false;
  96. }
  97. const binding = this.scope.getBinding(this.node.name);
  98. if (!binding || binding.kind !== "module") return false;
  99. const path = binding.path;
  100. const parent = path.parentPath;
  101. if (!parent.isImportDeclaration()) return false;
  102. if (parent.node.source.value === moduleSource) {
  103. if (!importName) return true;
  104. } else {
  105. return false;
  106. }
  107. if (path.isImportDefaultSpecifier() && importName === "default") {
  108. return true;
  109. }
  110. if (path.isImportNamespaceSpecifier() && importName === "*") {
  111. return true;
  112. }
  113. if (path.isImportSpecifier() && t.isIdentifier(path.node.imported, {
  114. name: importName
  115. })) {
  116. return true;
  117. }
  118. return false;
  119. }
  120. function getSource() {
  121. const node = this.node;
  122. if (node.end) {
  123. const code = this.hub.getCode();
  124. if (code) return code.slice(node.start, node.end);
  125. }
  126. return "";
  127. }
  128. function willIMaybeExecuteBefore(target) {
  129. return this._guessExecutionStatusRelativeTo(target) !== "after";
  130. }
  131. function getOuterFunction(path) {
  132. return (path.scope.getFunctionParent() || path.scope.getProgramParent()).path;
  133. }
  134. function isExecutionUncertain(type, key) {
  135. switch (type) {
  136. case "LogicalExpression":
  137. return key === "right";
  138. case "ConditionalExpression":
  139. case "IfStatement":
  140. return key === "consequent" || key === "alternate";
  141. case "WhileStatement":
  142. case "DoWhileStatement":
  143. case "ForInStatement":
  144. case "ForOfStatement":
  145. return key === "body";
  146. case "ForStatement":
  147. return key === "body" || key === "update";
  148. case "SwitchStatement":
  149. return key === "cases";
  150. case "TryStatement":
  151. return key === "handler";
  152. case "AssignmentPattern":
  153. return key === "right";
  154. case "OptionalMemberExpression":
  155. return key === "property";
  156. case "OptionalCallExpression":
  157. return key === "arguments";
  158. default:
  159. return false;
  160. }
  161. }
  162. function isExecutionUncertainInList(paths, maxIndex) {
  163. for (let i = 0; i < maxIndex; i++) {
  164. const path = paths[i];
  165. if (isExecutionUncertain(path.parent.type, path.parentKey)) {
  166. return true;
  167. }
  168. }
  169. return false;
  170. }
  171. function _guessExecutionStatusRelativeTo(target) {
  172. const funcParent = {
  173. this: getOuterFunction(this),
  174. target: getOuterFunction(target)
  175. };
  176. if (funcParent.target.node !== funcParent.this.node) {
  177. return this._guessExecutionStatusRelativeToDifferentFunctions(funcParent.target);
  178. }
  179. const paths = {
  180. target: target.getAncestry(),
  181. this: this.getAncestry()
  182. };
  183. if (paths.target.indexOf(this) >= 0) return "after";
  184. if (paths.this.indexOf(target) >= 0) return "before";
  185. let commonPath;
  186. const commonIndex = {
  187. target: 0,
  188. this: 0
  189. };
  190. while (!commonPath && commonIndex.this < paths.this.length) {
  191. const path = paths.this[commonIndex.this];
  192. commonIndex.target = paths.target.indexOf(path);
  193. if (commonIndex.target >= 0) {
  194. commonPath = path;
  195. } else {
  196. commonIndex.this++;
  197. }
  198. }
  199. if (!commonPath) {
  200. throw new Error("Internal Babel error - The two compared nodes" + " don't appear to belong to the same program.");
  201. }
  202. if (isExecutionUncertainInList(paths.this, commonIndex.this - 1) || isExecutionUncertainInList(paths.target, commonIndex.target - 1)) {
  203. return "unknown";
  204. }
  205. const divergence = {
  206. this: paths.this[commonIndex.this - 1],
  207. target: paths.target[commonIndex.target - 1]
  208. };
  209. if (divergence.target.listKey && divergence.this.listKey && divergence.target.container === divergence.this.container) {
  210. return divergence.target.key > divergence.this.key ? "before" : "after";
  211. }
  212. const keys = t.VISITOR_KEYS[commonPath.type];
  213. const keyPosition = {
  214. this: keys.indexOf(divergence.this.parentKey),
  215. target: keys.indexOf(divergence.target.parentKey)
  216. };
  217. return keyPosition.target > keyPosition.this ? "before" : "after";
  218. }
  219. const executionOrderCheckedNodes = new WeakSet();
  220. function _guessExecutionStatusRelativeToDifferentFunctions(target) {
  221. if (!target.isFunctionDeclaration() || target.parentPath.isExportDeclaration()) {
  222. return "unknown";
  223. }
  224. const binding = target.scope.getBinding(target.node.id.name);
  225. if (!binding.references) return "before";
  226. const referencePaths = binding.referencePaths;
  227. let allStatus;
  228. for (const path of referencePaths) {
  229. const childOfFunction = !!path.find(path => path.node === target.node);
  230. if (childOfFunction) continue;
  231. if (path.key !== "callee" || !path.parentPath.isCallExpression()) {
  232. return "unknown";
  233. }
  234. if (executionOrderCheckedNodes.has(path.node)) continue;
  235. executionOrderCheckedNodes.add(path.node);
  236. const status = this._guessExecutionStatusRelativeTo(path);
  237. executionOrderCheckedNodes.delete(path.node);
  238. if (allStatus && allStatus !== status) {
  239. return "unknown";
  240. } else {
  241. allStatus = status;
  242. }
  243. }
  244. return allStatus;
  245. }
  246. function resolve(dangerous, resolved) {
  247. return this._resolve(dangerous, resolved) || this;
  248. }
  249. function _resolve(dangerous, resolved) {
  250. if (resolved && resolved.indexOf(this) >= 0) return;
  251. resolved = resolved || [];
  252. resolved.push(this);
  253. if (this.isVariableDeclarator()) {
  254. if (this.get("id").isIdentifier()) {
  255. return this.get("init").resolve(dangerous, resolved);
  256. } else {}
  257. } else if (this.isReferencedIdentifier()) {
  258. const binding = this.scope.getBinding(this.node.name);
  259. if (!binding) return;
  260. if (!binding.constant) return;
  261. if (binding.kind === "module") return;
  262. if (binding.path !== this) {
  263. const ret = binding.path.resolve(dangerous, resolved);
  264. if (this.find(parent => parent.node === ret.node)) return;
  265. return ret;
  266. }
  267. } else if (this.isTypeCastExpression()) {
  268. return this.get("expression").resolve(dangerous, resolved);
  269. } else if (dangerous && this.isMemberExpression()) {
  270. const targetKey = this.toComputedKey();
  271. if (!t.isLiteral(targetKey)) return;
  272. const targetName = targetKey.value;
  273. const target = this.get("object").resolve(dangerous, resolved);
  274. if (target.isObjectExpression()) {
  275. const props = target.get("properties");
  276. for (const prop of props) {
  277. if (!prop.isProperty()) continue;
  278. const key = prop.get("key");
  279. let match = prop.isnt("computed") && key.isIdentifier({
  280. name: targetName
  281. });
  282. match = match || key.isLiteral({
  283. value: targetName
  284. });
  285. if (match) return prop.get("value").resolve(dangerous, resolved);
  286. }
  287. } else if (target.isArrayExpression() && !isNaN(+targetName)) {
  288. const elems = target.get("elements");
  289. const elem = elems[targetName];
  290. if (elem) return elem.resolve(dangerous, resolved);
  291. }
  292. }
  293. }
  294. function isConstantExpression() {
  295. if (this.isIdentifier()) {
  296. const binding = this.scope.getBinding(this.node.name);
  297. if (!binding) return false;
  298. return binding.constant;
  299. }
  300. if (this.isLiteral()) {
  301. if (this.isRegExpLiteral()) {
  302. return false;
  303. }
  304. if (this.isTemplateLiteral()) {
  305. return this.get("expressions").every(expression => expression.isConstantExpression());
  306. }
  307. return true;
  308. }
  309. if (this.isUnaryExpression()) {
  310. if (this.node.operator !== "void") {
  311. return false;
  312. }
  313. return this.get("argument").isConstantExpression();
  314. }
  315. if (this.isBinaryExpression()) {
  316. return this.get("left").isConstantExpression() && this.get("right").isConstantExpression();
  317. }
  318. return false;
  319. }
  320. function isInStrictMode() {
  321. const start = this.isProgram() ? this : this.parentPath;
  322. const strictParent = start.find(path => {
  323. if (path.isProgram({
  324. sourceType: "module"
  325. })) return true;
  326. if (path.isClass()) return true;
  327. if (!path.isProgram() && !path.isFunction()) return false;
  328. if (path.isArrowFunctionExpression() && !path.get("body").isBlockStatement()) {
  329. return false;
  330. }
  331. const body = path.isFunction() ? path.node.body : path.node;
  332. for (const directive of body.directives) {
  333. if (directive.value.value === "use strict") {
  334. return true;
  335. }
  336. }
  337. });
  338. return !!strictParent;
  339. }