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.

validators.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import definitions from "../../lib/definitions/index.js";
  2. const has = Function.call.bind(Object.prototype.hasOwnProperty);
  3. function joinComparisons(leftArr, right) {
  4. return (
  5. leftArr.map(JSON.stringify).join(` === ${right} || `) + ` === ${right}`
  6. );
  7. }
  8. function addIsHelper(type, aliasKeys, deprecated) {
  9. const targetType = JSON.stringify(type);
  10. let aliasSource = "";
  11. if (aliasKeys) {
  12. aliasSource = joinComparisons(aliasKeys, "nodeType");
  13. }
  14. let placeholderSource = "";
  15. const placeholderTypes = [];
  16. if (
  17. definitions.PLACEHOLDERS.includes(type) &&
  18. has(definitions.FLIPPED_ALIAS_KEYS, type)
  19. ) {
  20. placeholderTypes.push(type);
  21. }
  22. if (has(definitions.PLACEHOLDERS_FLIPPED_ALIAS, type)) {
  23. placeholderTypes.push(...definitions.PLACEHOLDERS_FLIPPED_ALIAS[type]);
  24. }
  25. if (placeholderTypes.length > 0) {
  26. placeholderSource =
  27. ' || nodeType === "Placeholder" && (' +
  28. joinComparisons(
  29. placeholderTypes,
  30. "(node as t.Placeholder).expectedNode"
  31. ) +
  32. ")";
  33. }
  34. const result =
  35. definitions.NODE_FIELDS[type] || definitions.FLIPPED_ALIAS_KEYS[type]
  36. ? `node is t.${type}`
  37. : "boolean";
  38. return `export function is${type}(node: object | null | undefined, opts?: object | null): ${result} {
  39. ${deprecated || ""}
  40. if (!node) return false;
  41. const nodeType = (node as t.Node).type;
  42. if (${
  43. aliasSource ? aliasSource : `nodeType === ${targetType}`
  44. }${placeholderSource}) {
  45. if (typeof opts === "undefined") {
  46. return true;
  47. } else {
  48. return shallowEqual(node, opts);
  49. }
  50. }
  51. return false;
  52. }
  53. `;
  54. }
  55. export default function generateValidators() {
  56. let output = `/*
  57. * This file is auto-generated! Do not modify it directly.
  58. * To re-generate run 'make build'
  59. */
  60. import shallowEqual from "../../utils/shallowEqual";
  61. import type * as t from "../..";\n\n`;
  62. Object.keys(definitions.VISITOR_KEYS).forEach(type => {
  63. output += addIsHelper(type);
  64. });
  65. Object.keys(definitions.FLIPPED_ALIAS_KEYS).forEach(type => {
  66. output += addIsHelper(type, definitions.FLIPPED_ALIAS_KEYS[type]);
  67. });
  68. Object.keys(definitions.DEPRECATED_KEYS).forEach(type => {
  69. const newType = definitions.DEPRECATED_KEYS[type];
  70. const deprecated = `console.trace("The node type ${type} has been renamed to ${newType}");`;
  71. output += addIsHelper(type, null, deprecated);
  72. });
  73. return output;
  74. }