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.

typescript-legacy.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. import t from "../../lib/index.js";
  2. import stringifyValidator from "../utils/stringifyValidator.js";
  3. import toFunctionName from "../utils/toFunctionName.js";
  4. let code = `// NOTE: This file is autogenerated. Do not modify.
  5. // See packages/babel-types/scripts/generators/typescript-legacy.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. extra?: Record<string, unknown>;
  39. }
  40. export type Node = ${t.TYPES.sort().join(" | ")};\n\n`;
  41. //
  42. const lines = [];
  43. for (const type in t.NODE_FIELDS) {
  44. const fields = t.NODE_FIELDS[type];
  45. const fieldNames = sortFieldNames(Object.keys(t.NODE_FIELDS[type]), type);
  46. const builderNames = t.BUILDER_KEYS[type];
  47. const struct = ['type: "' + type + '";'];
  48. const args = [];
  49. fieldNames.forEach(fieldName => {
  50. const field = fields[fieldName];
  51. // Future / annoying TODO:
  52. // MemberExpression.property, ObjectProperty.key and ObjectMethod.key need special cases; either:
  53. // - convert the declaration to chain() like ClassProperty.key and ClassMethod.key,
  54. // - declare an alias type for valid keys, detect the case and reuse it here,
  55. // - declare a disjoint union with, for example, ObjectPropertyBase,
  56. // ObjectPropertyLiteralKey and ObjectPropertyComputedKey, and declare ObjectProperty
  57. // as "ObjectPropertyBase & (ObjectPropertyLiteralKey | ObjectPropertyComputedKey)"
  58. let typeAnnotation = stringifyValidator(field.validate, "");
  59. if (isNullable(field) && !hasDefault(field)) {
  60. typeAnnotation += " | null";
  61. }
  62. if (builderNames.includes(fieldName)) {
  63. if (areAllRemainingFieldsNullable(fieldName, builderNames, fields)) {
  64. args.push(
  65. `${t.toBindingIdentifierName(fieldName)}${
  66. isNullable(field) ? "?:" : ":"
  67. } ${typeAnnotation}`
  68. );
  69. } else {
  70. args.push(
  71. `${t.toBindingIdentifierName(fieldName)}: ${typeAnnotation}${
  72. isNullable(field) ? " | undefined" : ""
  73. }`
  74. );
  75. }
  76. }
  77. const alphaNumeric = /^\w+$/;
  78. if (t.isValidIdentifier(fieldName) || alphaNumeric.test(fieldName)) {
  79. struct.push(`${fieldName}: ${typeAnnotation};`);
  80. } else {
  81. struct.push(`"${fieldName}": ${typeAnnotation};`);
  82. }
  83. });
  84. code += `export interface ${type} extends BaseNode {
  85. ${struct.join("\n ").trim()}
  86. }\n\n`;
  87. // super and import are reserved words in JavaScript
  88. if (type !== "Super" && type !== "Import") {
  89. lines.push(
  90. `export function ${toFunctionName(type)}(${args.join(", ")}): ${type};`
  91. );
  92. } else {
  93. const functionName = toFunctionName(type);
  94. lines.push(
  95. `declare function _${functionName}(${args.join(", ")}): ${type};`,
  96. `export { _${functionName} as ${functionName}}`
  97. );
  98. }
  99. }
  100. for (const typeName of t.TYPES) {
  101. const isDeprecated = !!t.DEPRECATED_KEYS[typeName];
  102. const realName = isDeprecated ? t.DEPRECATED_KEYS[typeName] : typeName;
  103. const result =
  104. t.NODE_FIELDS[realName] || t.FLIPPED_ALIAS_KEYS[realName]
  105. ? `node is ${realName}`
  106. : "boolean";
  107. if (isDeprecated) {
  108. lines.push(`/** @deprecated Use \`is${realName}\` */`);
  109. }
  110. lines.push(
  111. `export function is${typeName}(node: object | null | undefined, opts?: object | null): ${result};`
  112. );
  113. if (isDeprecated) {
  114. lines.push(`/** @deprecated Use \`assert${realName}\` */`);
  115. }
  116. lines.push(
  117. `export function assert${typeName}(node: object | null | undefined, opts?: object | null): void;`
  118. );
  119. }
  120. lines.push(
  121. // assert/
  122. `export function assertNode(obj: any): void`,
  123. // builders/
  124. // eslint-disable-next-line max-len
  125. `export function createTypeAnnotationBasedOnTypeof(type: 'string' | 'number' | 'undefined' | 'boolean' | 'function' | 'object' | 'symbol'): StringTypeAnnotation | VoidTypeAnnotation | NumberTypeAnnotation | BooleanTypeAnnotation | GenericTypeAnnotation`,
  126. `export function createUnionTypeAnnotation<T extends FlowType>(types: [T]): T`,
  127. `export function createFlowUnionType<T extends FlowType>(types: [T]): T`,
  128. // this probably misbehaves if there are 0 elements, and it's not a UnionTypeAnnotation if there's only 1
  129. // it is possible to require "2 or more" for this overload ([T, T, ...T[]]) but it requires typescript 3.0
  130. `export function createUnionTypeAnnotation(types: ReadonlyArray<FlowType>): UnionTypeAnnotation`,
  131. `export function createFlowUnionType(types: ReadonlyArray<FlowType>): UnionTypeAnnotation`,
  132. // this smells like "internal API"
  133. // eslint-disable-next-line max-len
  134. `export function buildChildren(node: { children: ReadonlyArray<JSXText | JSXExpressionContainer | JSXSpreadChild | JSXElement | JSXFragment | JSXEmptyExpression> }): JSXElement['children']`,
  135. // clone/
  136. `export function clone<T extends Node>(n: T): T;`,
  137. `export function cloneDeep<T extends Node>(n: T): T;`,
  138. `export function cloneDeepWithoutLoc<T extends Node>(n: T): T;`,
  139. `export function cloneNode<T extends Node>(n: T, deep?: boolean, withoutLoc?: boolean): T;`,
  140. `export function cloneWithoutLoc<T extends Node>(n: T): T;`,
  141. // comments/
  142. `export type CommentTypeShorthand = 'leading' | 'inner' | 'trailing'`,
  143. // eslint-disable-next-line max-len
  144. `export function addComment<T extends Node>(node: T, type: CommentTypeShorthand, content: string, line?: boolean): T`,
  145. // eslint-disable-next-line max-len
  146. `export function addComments<T extends Node>(node: T, type: CommentTypeShorthand, comments: ReadonlyArray<Comment>): T`,
  147. `export function inheritInnerComments(node: Node, parent: Node): void`,
  148. `export function inheritLeadingComments(node: Node, parent: Node): void`,
  149. `export function inheritsComments<T extends Node>(node: T, parent: Node): void`,
  150. `export function inheritTrailingComments(node: Node, parent: Node): void`,
  151. `export function removeComments<T extends Node>(node: T): T`,
  152. // converters/
  153. // eslint-disable-next-line max-len
  154. `export function ensureBlock(node: Extract<Node, { body: BlockStatement | Statement | Expression }>): BlockStatement`,
  155. // too complex?
  156. // eslint-disable-next-line max-len
  157. `export function ensureBlock<K extends keyof Extract<Node, { body: BlockStatement | Statement | Expression }> = 'body'>(node: Extract<Node, Record<K, BlockStatement | Statement | Expression>>, key: K): BlockStatement`,
  158. // gatherSequenceExpressions is not exported
  159. `export function toBindingIdentifierName(name: { toString(): string } | null | undefined): string`,
  160. `export function toBlock(node: Statement | Expression, parent?: Function | null): BlockStatement`,
  161. // it is possible for `node` to be an arbitrary object if `key` is always provided,
  162. // but that doesn't look like intended API
  163. // eslint-disable-next-line max-len
  164. `export function toComputedKey<T extends Extract<Node, { computed: boolean | null }>>(node: T, key?: Expression | Identifier): Expression`,
  165. `export function toExpression(node: Function): FunctionExpression`,
  166. `export function toExpression(node: Class): ClassExpression`,
  167. `export function toExpression(node: ExpressionStatement | Expression | Class | Function): Expression`,
  168. `export function toIdentifier(name: { toString(): string } | null | undefined): string`,
  169. `export function toKeyAlias(node: Method | Property, key?: Node): string`,
  170. // NOTE: this actually uses Scope from @babel/traverse, but we can't add a dependency on its types,
  171. // as they live in @types. Declare the structural subset that is required.
  172. // eslint-disable-next-line max-len
  173. `export function toSequenceExpression(nodes: ReadonlyArray<Node>, scope: { push(value: { id: LVal; kind: 'var'; init?: Expression}): void; buildUndefinedNode(): Node }): SequenceExpression | undefined`,
  174. `export function toStatement(node: AssignmentExpression, ignore?: boolean): ExpressionStatement`,
  175. `export function toStatement(node: Statement | AssignmentExpression, ignore?: boolean): Statement`,
  176. `export function toStatement(node: Class, ignore: true): ClassDeclaration | undefined`,
  177. `export function toStatement(node: Class, ignore?: boolean): ClassDeclaration`,
  178. `export function toStatement(node: Function, ignore: true): FunctionDeclaration | undefined`,
  179. `export function toStatement(node: Function, ignore?: boolean): FunctionDeclaration`,
  180. // eslint-disable-next-line max-len
  181. `export function toStatement(node: Statement | Class | Function | AssignmentExpression, ignore: true): Statement | undefined`,
  182. // eslint-disable-next-line max-len
  183. `export function toStatement(node: Statement | Class | Function | AssignmentExpression, ignore?: boolean): Statement`,
  184. // eslint-disable-next-line max-len
  185. `export function valueToNode(value: undefined): Identifier`, // (should this not be a UnaryExpression to avoid shadowing?)
  186. `export function valueToNode(value: boolean): BooleanLiteral`,
  187. `export function valueToNode(value: null): NullLiteral`,
  188. `export function valueToNode(value: string): StringLiteral`,
  189. // Infinities and NaN need to use a BinaryExpression; negative values must be wrapped in UnaryExpression
  190. `export function valueToNode(value: number): NumericLiteral | BinaryExpression | UnaryExpression`,
  191. `export function valueToNode(value: RegExp): RegExpLiteral`,
  192. // eslint-disable-next-line max-len
  193. `export function valueToNode(value: ReadonlyArray<undefined | boolean | null | string | number | RegExp | object>): ArrayExpression`,
  194. // this throws with objects that are not PlainObject according to lodash,
  195. // or if there are non-valueToNode-able values
  196. `export function valueToNode(value: object): ObjectExpression`,
  197. // eslint-disable-next-line max-len
  198. `export function valueToNode(value: undefined | boolean | null | string | number | RegExp | object): Expression`,
  199. // modifications/
  200. // eslint-disable-next-line max-len
  201. `export function removeTypeDuplicates(types: ReadonlyArray<FlowType | false | null | undefined>): FlowType[]`,
  202. // eslint-disable-next-line max-len
  203. `export function appendToMemberExpression<T extends Pick<MemberExpression, 'object' | 'property'>>(member: T, append: MemberExpression['property'], computed?: boolean): T`,
  204. // eslint-disable-next-line max-len
  205. `export function inherits<T extends Node | null | undefined>(child: T, parent: Node | null | undefined): T`,
  206. // eslint-disable-next-line max-len
  207. `export function prependToMemberExpression<T extends Pick<MemberExpression, 'object' | 'property'>>(member: T, prepend: MemberExpression['object']): T`,
  208. `export function removeProperties(
  209. n: Node,
  210. opts?: { preserveComments: boolean } | null
  211. ): void;`,
  212. `export function removePropertiesDeep<T extends Node>(
  213. n: T,
  214. opts?: { preserveComments: boolean } | null
  215. ): T;`,
  216. // retrievers/
  217. // eslint-disable-next-line max-len
  218. `export function getBindingIdentifiers(node: Node, duplicates: true, outerOnly?: boolean): Record<string, Array<Identifier>>`,
  219. // eslint-disable-next-line max-len
  220. `export function getBindingIdentifiers(node: Node, duplicates?: false, outerOnly?: boolean): Record<string, Identifier>`,
  221. // eslint-disable-next-line max-len
  222. `export function getBindingIdentifiers(node: Node, duplicates: boolean, outerOnly?: boolean): Record<string, Identifier | Array<Identifier>>`,
  223. // eslint-disable-next-line max-len
  224. `export function getOuterBindingIdentifiers(node: Node, duplicates: true): Record<string, Array<Identifier>>`,
  225. `export function getOuterBindingIdentifiers(node: Node, duplicates?: false): Record<string, Identifier>`,
  226. // eslint-disable-next-line max-len
  227. `export function getOuterBindingIdentifiers(node: Node, duplicates: boolean): Record<string, Identifier | Array<Identifier>>`,
  228. // traverse/
  229. `export type TraversalAncestors = ReadonlyArray<{
  230. node: Node,
  231. key: string,
  232. index?: number,
  233. }>;
  234. export type TraversalHandler<T> = (
  235. this: undefined, node: Node, parent: TraversalAncestors, type: T
  236. ) => void;
  237. export type TraversalHandlers<T> = {
  238. enter?: TraversalHandler<T>,
  239. exit?: TraversalHandler<T>,
  240. };`.replace(/(^|\n) {2}/g, "$1"),
  241. // eslint-disable-next-line
  242. `export function traverse<T>(n: Node, h: TraversalHandler<T> | TraversalHandlers<T>, state?: T): void;`,
  243. `export function traverseFast<T>(n: Node, h: TraversalHandler<T>, state?: T): void;`,
  244. // utils/
  245. // cleanJSXElementLiteralChild is not exported
  246. // inherit is not exported
  247. `export function shallowEqual<T extends object>(actual: object, expected: T): actual is T`,
  248. // validators/
  249. // eslint-disable-next-line max-len
  250. `export function buildMatchMemberExpression(match: string, allowPartial?: boolean): (node: Node | null | undefined) => node is MemberExpression`,
  251. // eslint-disable-next-line max-len
  252. `export function is<T extends Node['type']>(type: T, n: Node | null | undefined, required?: undefined): n is Extract<Node, { type: T }>`,
  253. // eslint-disable-next-line max-len
  254. `export function is<T extends Node['type'], P extends Extract<Node, { type: T }>>(type: T, n: Node | null | undefined, required: Partial<P>): n is P`,
  255. // eslint-disable-next-line max-len
  256. `export function is<P extends Node>(type: string, n: Node | null | undefined, required: Partial<P>): n is P`,
  257. `export function is(type: string, n: Node | null | undefined, required?: Partial<Node>): n is Node`,
  258. `export function isBinding(node: Node, parent: Node, grandparent?: Node): boolean`,
  259. // eslint-disable-next-line max-len
  260. `export function isBlockScoped(node: Node): node is FunctionDeclaration | ClassDeclaration | VariableDeclaration`,
  261. `export function isImmutable(node: Node): node is Immutable`,
  262. `export function isLet(node: Node): node is VariableDeclaration`,
  263. `export function isNode(node: object | null | undefined): node is Node`,
  264. `export function isNodesEquivalent<T extends Partial<Node>>(a: T, b: any): b is T`,
  265. `export function isNodesEquivalent(a: any, b: any): boolean`,
  266. `export function isPlaceholderType(placeholderType: Node['type'], targetType: Node['type']): boolean`,
  267. `export function isReferenced(node: Node, parent: Node, grandparent?: Node): boolean`,
  268. `export function isScope(node: Node, parent: Node): node is Scopable`,
  269. `export function isSpecifierDefault(specifier: ModuleSpecifier): boolean`,
  270. `export function isType<T extends Node['type']>(nodetype: string, targetType: T): nodetype is T`,
  271. `export function isType(nodetype: string | null | undefined, targetType: string): boolean`,
  272. `export function isValidES3Identifier(name: string): boolean`,
  273. `export function isValidIdentifier(name: string): boolean`,
  274. `export function isVar(node: Node): node is VariableDeclaration`,
  275. // the MemberExpression implication is incidental, but it follows from the implementation
  276. // eslint-disable-next-line max-len
  277. `export function matchesPattern(node: Node | null | undefined, match: string | ReadonlyArray<string>, allowPartial?: boolean): node is MemberExpression`,
  278. // eslint-disable-next-line max-len
  279. `export function validate<T extends Node, K extends keyof T>(n: Node | null | undefined, key: K, value: T[K]): void;`,
  280. `export function validate(n: Node, key: string, value: any): void;`
  281. );
  282. for (const type in t.DEPRECATED_KEYS) {
  283. code += `/**
  284. * @deprecated Use \`${t.DEPRECATED_KEYS[type]}\`
  285. */
  286. export type ${type} = ${t.DEPRECATED_KEYS[type]};\n
  287. `;
  288. }
  289. for (const type in t.FLIPPED_ALIAS_KEYS) {
  290. const types = t.FLIPPED_ALIAS_KEYS[type];
  291. code += `export type ${type} = ${types
  292. .map(type => `${type}`)
  293. .join(" | ")};\n`;
  294. }
  295. code += "\n";
  296. code += "export interface Aliases {\n";
  297. for (const type in t.FLIPPED_ALIAS_KEYS) {
  298. code += ` ${type}: ${type};\n`;
  299. }
  300. code += "}\n\n";
  301. code += lines.join("\n") + "\n";
  302. //
  303. process.stdout.write(code);
  304. //
  305. function areAllRemainingFieldsNullable(fieldName, fieldNames, fields) {
  306. const index = fieldNames.indexOf(fieldName);
  307. return fieldNames.slice(index).every(_ => isNullable(fields[_]));
  308. }
  309. function hasDefault(field) {
  310. return field.default != null;
  311. }
  312. function isNullable(field) {
  313. return field.optional || hasDefault(field);
  314. }
  315. function sortFieldNames(fields, type) {
  316. return fields.sort((fieldA, fieldB) => {
  317. const indexA = t.BUILDER_KEYS[type].indexOf(fieldA);
  318. const indexB = t.BUILDER_KEYS[type].indexOf(fieldB);
  319. if (indexA === indexB) return fieldA < fieldB ? -1 : 1;
  320. if (indexA === -1) return 1;
  321. if (indexB === -1) return -1;
  322. return indexA - indexB;
  323. });
  324. }