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.

flow.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import t from "../../lib/index.js";
  2. import stringifyValidator from "../utils/stringifyValidator.js";
  3. import toFunctionName from "../utils/toFunctionName.js";
  4. const NODE_PREFIX = "BabelNode";
  5. let code = `// NOTE: This file is autogenerated. Do not modify.
  6. // See packages/babel-types/scripts/generators/flow.js for script used.
  7. declare class ${NODE_PREFIX}Comment {
  8. value: string;
  9. start: number;
  10. end: number;
  11. loc: ${NODE_PREFIX}SourceLocation;
  12. }
  13. declare class ${NODE_PREFIX}CommentBlock extends ${NODE_PREFIX}Comment {
  14. type: "CommentBlock";
  15. }
  16. declare class ${NODE_PREFIX}CommentLine extends ${NODE_PREFIX}Comment {
  17. type: "CommentLine";
  18. }
  19. declare class ${NODE_PREFIX}SourceLocation {
  20. start: {
  21. line: number;
  22. column: number;
  23. };
  24. end: {
  25. line: number;
  26. column: number;
  27. };
  28. }
  29. declare class ${NODE_PREFIX} {
  30. leadingComments?: Array<${NODE_PREFIX}Comment>;
  31. innerComments?: Array<${NODE_PREFIX}Comment>;
  32. trailingComments?: Array<${NODE_PREFIX}Comment>;
  33. start: ?number;
  34. end: ?number;
  35. loc: ?${NODE_PREFIX}SourceLocation;
  36. extra?: { [string]: mixed };
  37. }\n\n`;
  38. //
  39. const lines = [];
  40. for (const type in t.NODE_FIELDS) {
  41. const fields = t.NODE_FIELDS[type];
  42. const struct = ['type: "' + type + '";'];
  43. const args = [];
  44. const builderNames = t.BUILDER_KEYS[type];
  45. Object.keys(t.NODE_FIELDS[type])
  46. .sort((fieldA, fieldB) => {
  47. const indexA = t.BUILDER_KEYS[type].indexOf(fieldA);
  48. const indexB = t.BUILDER_KEYS[type].indexOf(fieldB);
  49. if (indexA === indexB) return fieldA < fieldB ? -1 : 1;
  50. if (indexA === -1) return 1;
  51. if (indexB === -1) return -1;
  52. return indexA - indexB;
  53. })
  54. .forEach(fieldName => {
  55. const field = fields[fieldName];
  56. let suffix = "";
  57. if (field.optional || field.default != null) suffix += "?";
  58. let typeAnnotation = "any";
  59. const validate = field.validate;
  60. if (validate) {
  61. typeAnnotation = stringifyValidator(validate, NODE_PREFIX);
  62. }
  63. if (typeAnnotation) {
  64. suffix += ": " + typeAnnotation;
  65. }
  66. if (builderNames.includes(fieldName)) {
  67. args.push(t.toBindingIdentifierName(fieldName) + suffix);
  68. }
  69. if (t.isValidIdentifier(fieldName)) {
  70. struct.push(fieldName + suffix + ";");
  71. }
  72. });
  73. code += `declare class ${NODE_PREFIX}${type} extends ${NODE_PREFIX} {
  74. ${struct.join("\n ").trim()}
  75. }\n\n`;
  76. // Flow chokes on super() and import() :/
  77. if (type !== "Super" && type !== "Import") {
  78. lines.push(
  79. `declare export function ${toFunctionName(type)}(${args.join(
  80. ", "
  81. )}): ${NODE_PREFIX}${type};`
  82. );
  83. } else {
  84. const functionName = toFunctionName(type);
  85. lines.push(
  86. `declare function _${functionName}(${args.join(
  87. ", "
  88. )}): ${NODE_PREFIX}${type};`,
  89. `declare export { _${functionName} as ${functionName} }`
  90. );
  91. }
  92. }
  93. for (const typeName of t.TYPES) {
  94. const isDeprecated = !!t.DEPRECATED_KEYS[typeName];
  95. const realName = isDeprecated ? t.DEPRECATED_KEYS[typeName] : typeName;
  96. let decl = `declare export function is${typeName}(node: ?Object, opts?: ?Object): boolean`;
  97. if (t.NODE_FIELDS[realName]) {
  98. decl += ` %checks (node instanceof ${NODE_PREFIX}${realName})`;
  99. }
  100. lines.push(decl);
  101. lines.push(
  102. `declare export function assert${typeName}(node: ?Object, opts?: ?Object): void`
  103. );
  104. }
  105. lines.push(
  106. `declare export var VISITOR_KEYS: { [type: string]: string[] }`,
  107. // assert/
  108. `declare export function assertNode(obj: any): void`,
  109. // builders/
  110. // eslint-disable-next-line max-len
  111. `declare export function createTypeAnnotationBasedOnTypeof(type: 'string' | 'number' | 'undefined' | 'boolean' | 'function' | 'object' | 'symbol'): ${NODE_PREFIX}TypeAnnotation`,
  112. // eslint-disable-next-line max-len
  113. `declare export function createUnionTypeAnnotation(types: Array<${NODE_PREFIX}FlowType>): ${NODE_PREFIX}UnionTypeAnnotation`,
  114. // eslint-disable-next-line max-len
  115. `declare export function createFlowUnionType(types: Array<${NODE_PREFIX}FlowType>): ${NODE_PREFIX}UnionTypeAnnotation`,
  116. // this smells like "internal API"
  117. // eslint-disable-next-line max-len
  118. `declare export function buildChildren(node: { children: Array<${NODE_PREFIX}JSXText | ${NODE_PREFIX}JSXExpressionContainer | ${NODE_PREFIX}JSXSpreadChild | ${NODE_PREFIX}JSXElement | ${NODE_PREFIX}JSXFragment | ${NODE_PREFIX}JSXEmptyExpression> }): Array<${NODE_PREFIX}JSXText | ${NODE_PREFIX}JSXExpressionContainer | ${NODE_PREFIX}JSXSpreadChild | ${NODE_PREFIX}JSXElement | ${NODE_PREFIX}JSXFragment>`,
  119. // clone/
  120. `declare export function clone<T>(n: T): T;`,
  121. `declare export function cloneDeep<T>(n: T): T;`,
  122. `declare export function cloneDeepWithoutLoc<T>(n: T): T;`,
  123. `declare export function cloneNode<T>(n: T, deep?: boolean, withoutLoc?: boolean): T;`,
  124. `declare export function cloneWithoutLoc<T>(n: T): T;`,
  125. // comments/
  126. `declare type CommentTypeShorthand = 'leading' | 'inner' | 'trailing'`,
  127. // eslint-disable-next-line max-len
  128. `declare export function addComment<T: BabelNode>(node: T, type: CommentTypeShorthand, content: string, line?: boolean): T`,
  129. // eslint-disable-next-line max-len
  130. `declare export function addComments<T: BabelNode>(node: T, type: CommentTypeShorthand, comments: Array<Comment>): T`,
  131. `declare export function inheritInnerComments(node: BabelNode, parent: BabelNode): void`,
  132. `declare export function inheritLeadingComments(node: BabelNode, parent: BabelNode): void`,
  133. `declare export function inheritsComments<T: BabelNode>(node: T, parent: BabelNode): void`,
  134. `declare export function inheritTrailingComments(node: BabelNode, parent: BabelNode): void`,
  135. `declare export function removeComments<T: BabelNode>(node: T): T`,
  136. // converters/
  137. `declare export function ensureBlock(node: ${NODE_PREFIX}, key: string): ${NODE_PREFIX}BlockStatement`,
  138. `declare export function toBindingIdentifierName(name?: ?string): string`,
  139. // eslint-disable-next-line max-len
  140. `declare export function toBlock(node: ${NODE_PREFIX}Statement | ${NODE_PREFIX}Expression, parent?: ${NODE_PREFIX}Function | null): ${NODE_PREFIX}BlockStatement`,
  141. // eslint-disable-next-line max-len
  142. `declare export function toComputedKey(node: ${NODE_PREFIX}Method | ${NODE_PREFIX}Property, key?: ${NODE_PREFIX}Expression | ${NODE_PREFIX}Identifier): ${NODE_PREFIX}Expression`,
  143. // eslint-disable-next-line max-len
  144. `declare export function toExpression(node: ${NODE_PREFIX}ExpressionStatement | ${NODE_PREFIX}Expression | ${NODE_PREFIX}Class | ${NODE_PREFIX}Function): ${NODE_PREFIX}Expression`,
  145. `declare export function toIdentifier(name?: ?string): string`,
  146. // eslint-disable-next-line max-len
  147. `declare export function toKeyAlias(node: ${NODE_PREFIX}Method | ${NODE_PREFIX}Property, key?: ${NODE_PREFIX}): string`,
  148. // toSequenceExpression relies on types that aren't declared in flow
  149. // eslint-disable-next-line max-len
  150. `declare export function toStatement(node: ${NODE_PREFIX}Statement | ${NODE_PREFIX}Class | ${NODE_PREFIX}Function | ${NODE_PREFIX}AssignmentExpression, ignore?: boolean): ${NODE_PREFIX}Statement | void`,
  151. `declare export function valueToNode(value: any): ${NODE_PREFIX}Expression`,
  152. // modifications/
  153. // eslint-disable-next-line max-len
  154. `declare export function removeTypeDuplicates(types: Array<${NODE_PREFIX}FlowType>): Array<${NODE_PREFIX}FlowType>`,
  155. // eslint-disable-next-line max-len
  156. `declare export function appendToMemberExpression(member: ${NODE_PREFIX}MemberExpression, append: ${NODE_PREFIX}, computed?: boolean): ${NODE_PREFIX}MemberExpression`,
  157. // eslint-disable-next-line max-len
  158. `declare export function inherits<T: BabelNode>(child: T, parent: ${NODE_PREFIX} | null | void): T`,
  159. // eslint-disable-next-line max-len
  160. `declare export function prependToMemberExpression(member: ${NODE_PREFIX}MemberExpression, prepend: ${NODE_PREFIX}Expression): ${NODE_PREFIX}MemberExpression`,
  161. `declare export function removeProperties<T>(n: T, opts: ?{}): void;`,
  162. `declare export function removePropertiesDeep<T>(n: T, opts: ?{}): T;`,
  163. // retrievers/
  164. // eslint-disable-next-line max-len
  165. `declare export var getBindingIdentifiers: {
  166. (node: ${NODE_PREFIX}, duplicates?: boolean, outerOnly?: boolean): { [key: string]: ${NODE_PREFIX}Identifier | Array<${NODE_PREFIX}Identifier> },
  167. keys: { [type: string]: string[] }
  168. }`,
  169. // eslint-disable-next-line max-len
  170. `declare export function getOuterBindingIdentifiers(node: BabelNode, duplicates?: boolean): { [key: string]: ${NODE_PREFIX}Identifier | Array<${NODE_PREFIX}Identifier> }`,
  171. // traverse/
  172. `declare type TraversalAncestors = Array<{
  173. node: BabelNode,
  174. key: string,
  175. index?: number,
  176. }>;
  177. declare type TraversalHandler<T> = (BabelNode, TraversalAncestors, T) => void;
  178. declare type TraversalHandlers<T> = {
  179. enter?: TraversalHandler<T>,
  180. exit?: TraversalHandler<T>,
  181. };`.replace(/(^|\n) {2}/g, "$1"),
  182. // eslint-disable-next-line
  183. `declare export function traverse<T>(n: BabelNode, TraversalHandler<T> | TraversalHandlers<T>, state?: T): void;`,
  184. `declare export function traverseFast<T>(n: BabelNode, h: TraversalHandler<T>, state?: T): void;`,
  185. // utils/
  186. // cleanJSXElementLiteralChild is not exported
  187. // inherit is not exported
  188. `declare export function shallowEqual(actual: Object, expected: Object): boolean`,
  189. // validators/
  190. // eslint-disable-next-line max-len
  191. `declare export function buildMatchMemberExpression(match: string, allowPartial?: boolean): (?BabelNode) => boolean`,
  192. `declare export function is(type: string, n: BabelNode, opts: Object): boolean;`,
  193. `declare export function isBinding(node: BabelNode, parent: BabelNode, grandparent?: BabelNode): boolean`,
  194. `declare export function isBlockScoped(node: BabelNode): boolean`,
  195. `declare export function isImmutable(node: BabelNode): boolean`,
  196. `declare export function isLet(node: BabelNode): boolean`,
  197. `declare export function isNode(node: ?Object): boolean`,
  198. `declare export function isNodesEquivalent(a: any, b: any): boolean`,
  199. `declare export function isPlaceholderType(placeholderType: string, targetType: string): boolean`,
  200. `declare export function isReferenced(node: BabelNode, parent: BabelNode, grandparent?: BabelNode): boolean`,
  201. `declare export function isScope(node: BabelNode, parent: BabelNode): boolean`,
  202. `declare export function isSpecifierDefault(specifier: BabelNodeModuleSpecifier): boolean`,
  203. `declare export function isType(nodetype: ?string, targetType: string): boolean`,
  204. `declare export function isValidES3Identifier(name: string): boolean`,
  205. `declare export function isValidES3Identifier(name: string): boolean`,
  206. `declare export function isValidIdentifier(name: string): boolean`,
  207. `declare export function isVar(node: BabelNode): boolean`,
  208. // eslint-disable-next-line max-len
  209. `declare export function matchesPattern(node: ?BabelNode, match: string | Array<string>, allowPartial?: boolean): boolean`,
  210. `declare export function validate(n: BabelNode, key: string, value: mixed): void;`
  211. );
  212. for (const type in t.FLIPPED_ALIAS_KEYS) {
  213. const types = t.FLIPPED_ALIAS_KEYS[type];
  214. code += `type ${NODE_PREFIX}${type} = ${types
  215. .map(type => `${NODE_PREFIX}${type}`)
  216. .join(" | ")};\n`;
  217. }
  218. code += `\ndeclare module "@babel/types" {
  219. ${lines.join("\n").replace(/\n/g, "\n ").trim()}
  220. }\n`;
  221. //
  222. process.stdout.write(code);