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.

ast-types.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import t from "../../lib/index.js";
  2. import stringifyValidator from "../utils/stringifyValidator.js";
  3. export default function generateAstTypes() {
  4. let code = `// NOTE: This file is autogenerated. Do not modify.
  5. // See packages/babel-types/scripts/generators/ast-types.js for script used.
  6. interface BaseComment {
  7. value: string;
  8. start: number;
  9. end: number;
  10. loc: SourceLocation;
  11. type: "CommentBlock" | "CommentLine";
  12. }
  13. export interface CommentBlock extends BaseComment {
  14. type: "CommentBlock";
  15. }
  16. export interface CommentLine extends BaseComment {
  17. type: "CommentLine";
  18. }
  19. export type Comment = CommentBlock | CommentLine;
  20. export interface SourceLocation {
  21. start: {
  22. line: number;
  23. column: number;
  24. };
  25. end: {
  26. line: number;
  27. column: number;
  28. };
  29. }
  30. interface BaseNode {
  31. leadingComments: ReadonlyArray<Comment> | null;
  32. innerComments: ReadonlyArray<Comment> | null;
  33. trailingComments: ReadonlyArray<Comment> | null;
  34. start: number | null;
  35. end: number | null;
  36. loc: SourceLocation | null;
  37. type: Node["type"];
  38. range?: [number, number];
  39. extra?: Record<string, unknown>;
  40. }
  41. export type CommentTypeShorthand = "leading" | "inner" | "trailing";
  42. export type Node = ${t.TYPES.sort().join(" | ")};\n\n`;
  43. const deprecatedAlias = {};
  44. for (const type in t.DEPRECATED_KEYS) {
  45. deprecatedAlias[t.DEPRECATED_KEYS[type]] = type;
  46. }
  47. for (const type in t.NODE_FIELDS) {
  48. const fields = t.NODE_FIELDS[type];
  49. const fieldNames = sortFieldNames(Object.keys(t.NODE_FIELDS[type]), type);
  50. const struct = [];
  51. fieldNames.forEach(fieldName => {
  52. const field = fields[fieldName];
  53. // Future / annoying TODO:
  54. // MemberExpression.property, ObjectProperty.key and ObjectMethod.key need special cases; either:
  55. // - convert the declaration to chain() like ClassProperty.key and ClassMethod.key,
  56. // - declare an alias type for valid keys, detect the case and reuse it here,
  57. // - declare a disjoint union with, for example, ObjectPropertyBase,
  58. // ObjectPropertyLiteralKey and ObjectPropertyComputedKey, and declare ObjectProperty
  59. // as "ObjectPropertyBase & (ObjectPropertyLiteralKey | ObjectPropertyComputedKey)"
  60. let typeAnnotation = stringifyValidator(field.validate, "");
  61. if (isNullable(field) && !hasDefault(field)) {
  62. typeAnnotation += " | null";
  63. }
  64. const alphaNumeric = /^\w+$/;
  65. const optional = field.optional ? "?" : "";
  66. if (t.isValidIdentifier(fieldName) || alphaNumeric.test(fieldName)) {
  67. struct.push(`${fieldName}${optional}: ${typeAnnotation};`);
  68. } else {
  69. struct.push(`"${fieldName}"${optional}: ${typeAnnotation};`);
  70. }
  71. });
  72. code += `export interface ${type} extends BaseNode {
  73. type: "${type}";
  74. ${struct.join("\n ").trim()}
  75. }\n\n`;
  76. if (deprecatedAlias[type]) {
  77. code += `/**
  78. * @deprecated Use \`${type}\`
  79. */
  80. export interface ${deprecatedAlias[type]} extends BaseNode {
  81. type: "${deprecatedAlias[type]}";
  82. ${struct.join("\n ").trim()}
  83. }\n\n
  84. `;
  85. }
  86. }
  87. for (const type in t.FLIPPED_ALIAS_KEYS) {
  88. const types = t.FLIPPED_ALIAS_KEYS[type];
  89. code += `export type ${type} = ${types
  90. .map(type => `${type}`)
  91. .join(" | ")};\n`;
  92. }
  93. code += "\n";
  94. code += "export interface Aliases {\n";
  95. for (const type in t.FLIPPED_ALIAS_KEYS) {
  96. code += ` ${type}: ${type};\n`;
  97. }
  98. code += "}\n\n";
  99. return code;
  100. }
  101. function hasDefault(field) {
  102. return field.default != null;
  103. }
  104. function isNullable(field) {
  105. return field.optional || hasDefault(field);
  106. }
  107. function sortFieldNames(fields, type) {
  108. return fields.sort((fieldA, fieldB) => {
  109. const indexA = t.BUILDER_KEYS[type].indexOf(fieldA);
  110. const indexB = t.BUILDER_KEYS[type].indexOf(fieldB);
  111. if (indexA === indexB) return fieldA < fieldB ? -1 : 1;
  112. if (indexA === -1) return 1;
  113. if (indexB === -1) return -1;
  114. return indexA - indexB;
  115. });
  116. }