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.

parse.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = parseAndBuildMetadata;
  6. var t = require("@babel/types");
  7. var _parser = require("@babel/parser");
  8. var _codeFrame = require("@babel/code-frame");
  9. const PATTERN = /^[_$A-Z0-9]+$/;
  10. function parseAndBuildMetadata(formatter, code, opts) {
  11. const {
  12. placeholderWhitelist,
  13. placeholderPattern,
  14. preserveComments,
  15. syntacticPlaceholders
  16. } = opts;
  17. const ast = parseWithCodeFrame(code, opts.parser, syntacticPlaceholders);
  18. t.removePropertiesDeep(ast, {
  19. preserveComments
  20. });
  21. formatter.validate(ast);
  22. const syntactic = {
  23. placeholders: [],
  24. placeholderNames: new Set()
  25. };
  26. const legacy = {
  27. placeholders: [],
  28. placeholderNames: new Set()
  29. };
  30. const isLegacyRef = {
  31. value: undefined
  32. };
  33. t.traverse(ast, placeholderVisitorHandler, {
  34. syntactic,
  35. legacy,
  36. isLegacyRef,
  37. placeholderWhitelist,
  38. placeholderPattern,
  39. syntacticPlaceholders
  40. });
  41. return Object.assign({
  42. ast
  43. }, isLegacyRef.value ? legacy : syntactic);
  44. }
  45. function placeholderVisitorHandler(node, ancestors, state) {
  46. var _state$placeholderWhi;
  47. let name;
  48. if (t.isPlaceholder(node)) {
  49. if (state.syntacticPlaceholders === false) {
  50. throw new Error("%%foo%%-style placeholders can't be used when " + "'.syntacticPlaceholders' is false.");
  51. } else {
  52. name = node.name.name;
  53. state.isLegacyRef.value = false;
  54. }
  55. } else if (state.isLegacyRef.value === false || state.syntacticPlaceholders) {
  56. return;
  57. } else if (t.isIdentifier(node) || t.isJSXIdentifier(node)) {
  58. name = node.name;
  59. state.isLegacyRef.value = true;
  60. } else if (t.isStringLiteral(node)) {
  61. name = node.value;
  62. state.isLegacyRef.value = true;
  63. } else {
  64. return;
  65. }
  66. if (!state.isLegacyRef.value && (state.placeholderPattern != null || state.placeholderWhitelist != null)) {
  67. throw new Error("'.placeholderWhitelist' and '.placeholderPattern' aren't compatible" + " with '.syntacticPlaceholders: true'");
  68. }
  69. if (state.isLegacyRef.value && (state.placeholderPattern === false || !(state.placeholderPattern || PATTERN).test(name)) && !((_state$placeholderWhi = state.placeholderWhitelist) != null && _state$placeholderWhi.has(name))) {
  70. return;
  71. }
  72. ancestors = ancestors.slice();
  73. const {
  74. node: parent,
  75. key
  76. } = ancestors[ancestors.length - 1];
  77. let type;
  78. if (t.isStringLiteral(node) || t.isPlaceholder(node, {
  79. expectedNode: "StringLiteral"
  80. })) {
  81. type = "string";
  82. } else if (t.isNewExpression(parent) && key === "arguments" || t.isCallExpression(parent) && key === "arguments" || t.isFunction(parent) && key === "params") {
  83. type = "param";
  84. } else if (t.isExpressionStatement(parent) && !t.isPlaceholder(node)) {
  85. type = "statement";
  86. ancestors = ancestors.slice(0, -1);
  87. } else if (t.isStatement(node) && t.isPlaceholder(node)) {
  88. type = "statement";
  89. } else {
  90. type = "other";
  91. }
  92. const {
  93. placeholders,
  94. placeholderNames
  95. } = state.isLegacyRef.value ? state.legacy : state.syntactic;
  96. placeholders.push({
  97. name,
  98. type,
  99. resolve: ast => resolveAncestors(ast, ancestors),
  100. isDuplicate: placeholderNames.has(name)
  101. });
  102. placeholderNames.add(name);
  103. }
  104. function resolveAncestors(ast, ancestors) {
  105. let parent = ast;
  106. for (let i = 0; i < ancestors.length - 1; i++) {
  107. const {
  108. key,
  109. index
  110. } = ancestors[i];
  111. if (index === undefined) {
  112. parent = parent[key];
  113. } else {
  114. parent = parent[key][index];
  115. }
  116. }
  117. const {
  118. key,
  119. index
  120. } = ancestors[ancestors.length - 1];
  121. return {
  122. parent,
  123. key,
  124. index
  125. };
  126. }
  127. function parseWithCodeFrame(code, parserOpts, syntacticPlaceholders) {
  128. const plugins = (parserOpts.plugins || []).slice();
  129. if (syntacticPlaceholders !== false) {
  130. plugins.push("placeholders");
  131. }
  132. parserOpts = Object.assign({
  133. allowReturnOutsideFunction: true,
  134. allowSuperOutsideMethod: true,
  135. sourceType: "module"
  136. }, parserOpts, {
  137. plugins
  138. });
  139. try {
  140. return (0, _parser.parse)(code, parserOpts);
  141. } catch (err) {
  142. const loc = err.loc;
  143. if (loc) {
  144. err.message += "\n" + (0, _codeFrame.codeFrameColumns)(code, {
  145. start: loc
  146. });
  147. err.code = "BABEL_TEMPLATE_PARSE_ERROR";
  148. }
  149. throw err;
  150. }
  151. }