UNPKG

3.71 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * strict
8 * @format
9 */
10'use strict';
11
12/**
13 * type NAME = any;
14 */
15function anyTypeAlias(name) {
16 return require("@babel/types").typeAlias(require("@babel/types").identifier(name), null, require("@babel/types").anyTypeAnnotation());
17}
18/**
19 * {|
20 * PROPS
21 * |}
22 */
23
24
25function exactObjectTypeAnnotation(props) {
26 var typeAnnotation = require("@babel/types").objectTypeAnnotation(props);
27
28 typeAnnotation.exact = true;
29 return typeAnnotation;
30}
31/**
32 * export type NAME = TYPE
33 */
34
35
36function exportType(name, type) {
37 return require("@babel/types").exportNamedDeclaration(require("@babel/types").typeAlias(require("@babel/types").identifier(name), null, type), [], null);
38}
39/**
40 * import type {NAMES[0], NAMES[1], ...} from 'MODULE';
41 */
42
43
44function importTypes(names, module) {
45 var importDeclaration = require("@babel/types").importDeclaration(names.map(function (name) {
46 return require("@babel/types").importSpecifier(require("@babel/types").identifier(name), require("@babel/types").identifier(name));
47 }), require("@babel/types").stringLiteral(module));
48
49 importDeclaration.importKind = 'type';
50 return importDeclaration;
51}
52/**
53 * Create an intersection type if needed.
54 *
55 * TYPES[0] & TYPES[1] & ...
56 */
57
58
59function intersectionTypeAnnotation(types) {
60 !(types.length > 0) ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'RelayFlowBabelFactories: cannot create an intersection of 0 types') : require("fbjs/lib/invariant")(false) : void 0;
61 return types.length === 1 ? types[0] : require("@babel/types").intersectionTypeAnnotation(types);
62}
63
64function lineComments() {
65 for (var _len = arguments.length, lines = new Array(_len), _key = 0; _key < _len; _key++) {
66 lines[_key] = arguments[_key];
67 }
68
69 /* $FlowFixMe(>=0.70.0) This comment suppresses an error found when
70 * Flow v0.70 was deployed. To see the error delete this comment and
71 * run Flow. */
72 return lines.map(function (line) {
73 return {
74 type: 'CommentLine',
75 value: ' ' + line
76 };
77 });
78}
79/**
80 * $ReadOnlyArray<TYPE>
81 */
82
83
84function readOnlyArrayOfType(thing) {
85 return require("@babel/types").genericTypeAnnotation(require("@babel/types").identifier('$ReadOnlyArray'), require("@babel/types").typeParameterInstantiation([thing]));
86}
87/**
88 * +KEY: VALUE
89 */
90
91
92function readOnlyObjectTypeProperty(key, value) {
93 var prop = require("@babel/types").objectTypeProperty(require("@babel/types").identifier(key), value);
94
95 prop.variance = require("@babel/types").variance('plus');
96 return prop;
97}
98
99function stringLiteralTypeAnnotation(value) {
100 return require("@babel/types").stringLiteralTypeAnnotation(value);
101}
102/**
103 * Create a union type if needed.
104 *
105 * TYPES[0] | TYPES[1] | ...
106 */
107
108
109function unionTypeAnnotation(types) {
110 !(types.length > 0) ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'RelayFlowBabelFactories: cannot create a union of 0 types') : require("fbjs/lib/invariant")(false) : void 0;
111 return types.length === 1 ? types[0] : require("@babel/types").unionTypeAnnotation(types);
112}
113
114module.exports = {
115 anyTypeAlias: anyTypeAlias,
116 exactObjectTypeAnnotation: exactObjectTypeAnnotation,
117 exportType: exportType,
118 importTypes: importTypes,
119 intersectionTypeAnnotation: intersectionTypeAnnotation,
120 lineComments: lineComments,
121 readOnlyArrayOfType: readOnlyArrayOfType,
122 readOnlyObjectTypeProperty: readOnlyObjectTypeProperty,
123 stringLiteralTypeAnnotation: stringLiteralTypeAnnotation,
124 unionTypeAnnotation: unionTypeAnnotation
125};
\No newline at end of file