forked from react/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
436 lines (384 loc) · 12.7 KB
/
Copy pathparser.js
File metadata and controls
436 lines (384 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
*/
'use strict';
import type {
ExtendsPropsShape,
NamedShape,
NativeModuleAliasMap,
NativeModuleEnumMap,
NativeModuleEnumMember,
NativeModuleEnumMemberType,
NativeModuleParamTypeAnnotation,
NativeModuleUnionTypeAnnotationMemberType,
Nullable,
PropTypeAnnotation,
SchemaType,
} from '../CodegenSchema';
import type {ParserType} from './errors';
import type {
ASTNode,
ParserErrorCapturer,
PropAST,
TypeDeclarationMap,
TypeResolutionStatus,
} from './utils';
export type GetTypeAnnotationFN = (
name: string,
annotation: $FlowFixMe | ASTNode,
defaultValue: $FlowFixMe | void,
withNullDefault: boolean,
types: TypeDeclarationMap,
parser: Parser,
buildSchema: (
property: PropAST,
types: TypeDeclarationMap,
parser: Parser,
) => $FlowFixMe,
) => $FlowFixMe;
export type SchemaInfo = {
name: string,
optional: boolean,
typeAnnotation: $FlowFixMe,
defaultValue: $FlowFixMe,
withNullDefault: boolean,
};
export type GetSchemaInfoFN = (
property: PropAST,
types: TypeDeclarationMap,
parser: Parser,
) => ?SchemaInfo;
export type BuildSchemaFN<T> = (
property: PropAST,
types: TypeDeclarationMap,
parser: Parser,
) => ?NamedShape<T>;
export type ResolveTypeAnnotationFN = (
typeAnnotation: $FlowFixMe,
types: TypeDeclarationMap,
parser: Parser,
) => {
nullable: boolean,
typeAnnotation: $FlowFixMe,
typeResolutionStatus: TypeResolutionStatus,
};
/**
* This is the main interface for Parsers of various languages.
* It exposes all the methods that contain language-specific logic.
*/
export interface Parser {
/**
* This is the TypeParameterInstantiation value
*/
typeParameterInstantiation: string;
/**
* TypeAlias property of the Parser
*/
typeAlias: string;
/**
* enumDeclaration Property of the Parser
*/
enumDeclaration: string;
/**
* InterfaceDeclaration property of the Parser
*/
interfaceDeclaration: string;
/**
* This is the NullLiteralTypeAnnotation value
*/
nullLiteralTypeAnnotation: string;
/**
* UndefinedLiteralTypeAnnotation property of the Parser
*/
undefinedLiteralTypeAnnotation: string;
/**
* Given a declaration, it returns true if it is a property
*/
isProperty(property: $FlowFixMe): boolean;
/**
* Given a property declaration, it returns the key name.
* @parameter property: an object containing a property declaration.
* @parameter hasteModuleName: a string with the native module name.
* @returns: the key name.
* @throws if property does not contain a property declaration.
*/
getKeyName(property: $FlowFixMe, hasteModuleName: string): string;
/**
* @returns: the Parser language.
*/
language(): ParserType;
/**
* Given a type annotation, it returns the type name.
* @parameter typeAnnotation: the annotation for a type in the AST.
* @returns: the name of the type.
*/
getTypeAnnotationName(typeAnnotation: $FlowFixMe): string;
/**
* Given a type arguments, it returns a boolean specifying if the Module is Invalid.
* @parameter typeArguments: the type arguments.
* @returns: a boolean specifying if the Module is Invalid.
*/
checkIfInvalidModule(typeArguments: $FlowFixMe): boolean;
/**
* Given a union annotation members types, it returns an array of remaped members names without duplicates.
* @parameter membersTypes: union annotation members types
* @returns: an array of remaped members names without duplicates.
*/
remapUnionTypeAnnotationMemberNames(
types: $FlowFixMe,
): NativeModuleUnionTypeAnnotationMemberType[];
/**
* Given the name of a file, it returns a Schema.
* @parameter filename: the name of the file.
* @returns: the Schema of the file.
*/
parseFile(filename: string): SchemaType;
/**
* Given the content of a file, it returns a Schema.
* @parameter contents: the content of the file.
* @parameter filename: the name of the file.
* @returns: the Schema of the file.
*/
parseString(contents: string, filename: ?string): SchemaType;
/**
* Given the name of a file, it returns a Schema.
* @parameter filename: the name of the file.
* @returns: the Schema of the file.
*/
parseModuleFixture(filename: string): SchemaType;
/**
* Given the content of a file, it returns an AST.
* @parameter contents: the content of the file.
* @parameter filename: the name of the file, if available.
* @throws if there is a syntax error.
* @returns: the AST of the file.
*/
getAst(contents: string, filename?: ?string): $FlowFixMe;
/**
* Given a FunctionTypeAnnotation, it returns an array of its parameters.
* @parameter functionTypeAnnotation: a FunctionTypeAnnotation
* @returns: the parameters of the FunctionTypeAnnotation.
*/
getFunctionTypeAnnotationParameters(
functionTypeAnnotation: $FlowFixMe,
): $ReadOnlyArray<$FlowFixMe>;
/**
* Given a parameter, it returns the function name of the parameter.
* @parameter parameter: a parameter of a FunctionTypeAnnotation.
* @returns: the function name of the parameter.
*/
getFunctionNameFromParameter(
parameter: NamedShape<Nullable<NativeModuleParamTypeAnnotation>>,
): $FlowFixMe;
/**
* Given a parameter, it returns its name.
* @parameter parameter: a parameter of a FunctionTypeAnnotation.
* @returns: the name of the parameter.
*/
getParameterName(parameter: $FlowFixMe): string;
/**
* Given a parameter, it returns its typeAnnotation.
* @parameter parameter: a parameter of a FunctionTypeAnnotation.
* @returns: the typeAnnotation of the parameter.
*/
getParameterTypeAnnotation(param: $FlowFixMe): $FlowFixMe;
/**
* Given a FunctionTypeAnnotation, it returns its returnType.
* @parameter functionTypeAnnotation: a FunctionTypeAnnotation
* @returns: the returnType of the FunctionTypeAnnotation.
*/
getFunctionTypeAnnotationReturnType(
functionTypeAnnotation: $FlowFixMe,
): $FlowFixMe;
/**
* Calculates an enum's members type
*/
parseEnumMembersType(typeAnnotation: $FlowFixMe): NativeModuleEnumMemberType;
/**
* Throws if enum mebers are not supported
*/
validateEnumMembersSupported(
typeAnnotation: $FlowFixMe,
enumMembersType: NativeModuleEnumMemberType,
): void;
/**
* Calculates enum's members
*/
parseEnumMembers(
typeAnnotation: $FlowFixMe,
): $ReadOnlyArray<NativeModuleEnumMember>;
/**
* Given a node, it returns true if it is a module interface
*/
isModuleInterface(node: $FlowFixMe): boolean;
/**
* Given a type name, it returns true if it is a generic type annotation
*/
isGenericTypeAnnotation(type: $FlowFixMe): boolean;
/**
* Given a typeAnnotation, it returns the annotated element.
* @parameter typeAnnotation: the annotation for a type.
* @parameter types: a map of type declarations.
* @returns: the annotated element.
*/
extractAnnotatedElement(
typeAnnotation: $FlowFixMe,
types: TypeDeclarationMap,
): $FlowFixMe;
/**
* Given the AST, returns the TypeDeclarationMap
*/
getTypes(ast: $FlowFixMe): TypeDeclarationMap;
/**
* Given a callExpression, it returns the typeParameters of the callExpression.
* @parameter callExpression: the callExpression.
* @returns: the typeParameters of the callExpression or null if it does not exist.
*/
callExpressionTypeParameters(callExpression: $FlowFixMe): $FlowFixMe | null;
/**
* Given an array of properties from a Partial type, it returns an array of remaped properties.
* @parameter properties: properties from a Partial types.
* @parameter hasteModuleName: a string with the native module name.
* @parameter types: a map of type declarations.
* @parameter aliasMap: a map of type aliases.
* @parameter enumMap: a map of type enums.
* @parameter tryParse: a parser error capturer.
* @parameter cxxOnly: a boolean specifying if the module is Cxx only.
* @returns: an array of remaped properties
*/
computePartialProperties(
properties: Array<$FlowFixMe>,
hasteModuleName: string,
types: TypeDeclarationMap,
aliasMap: {...NativeModuleAliasMap},
enumMap: {...NativeModuleEnumMap},
tryParse: ParserErrorCapturer,
cxxOnly: boolean,
): Array<$FlowFixMe>;
/**
* Given a propertyValueType, it returns a boolean specifying if the property is a function type annotation.
* @parameter propertyValueType: the propertyValueType.
* @returns: a boolean specifying if the property is a function type annotation.
*/
functionTypeAnnotation(propertyValueType: string): boolean;
/**
* Given a declaration, it returns the typeArgumentParams of the declaration.
* @parameter declaration: the declaration.
* @returns: the typeArgumentParams of the declaration.
*/
getTypeArgumentParamsFromDeclaration(declaration: $FlowFixMe): $FlowFixMe;
/**
* Given a typeArgumentParams and funcArgumentParams it returns a native component type.
* @parameter typeArgumentParams: the typeArgumentParams.
* @parameter funcArgumentParams: the funcArgumentParams.
* @returns: a native component type.
*/
getNativeComponentType(
typeArgumentParams: $FlowFixMe,
funcArgumentParams: $FlowFixMe,
): {[string]: string};
/**
* Given a annotatedElement, it returns the properties of annotated element.
* @parameter annotatedElement: the annotated element.
* @returns: the properties of annotated element.
*/
getAnnotatedElementProperties(annotatedElement: $FlowFixMe): $FlowFixMe;
/**
* Given a typeAlias, it returns an array of properties.
* @parameter typeAlias: the type alias.
* @returns: an array of properties.
*/
bodyProperties(typeAlias: $FlowFixMe): $ReadOnlyArray<$FlowFixMe>;
/**
* Given a keyword convert it to TypeAnnotation.
* @parameter keyword
* @returns: converted TypeAnnotation to Keywords
*/
convertKeywordToTypeAnnotation(keyword: string): string;
/**
* Given a prop return its arguments.
* @parameter prop
* @returns: Arguments of the prop
*/
argumentForProp(prop: PropAST): $FlowFixMe;
/**
* Given a prop return its name.
* @parameter prop
* @returns: name property
*/
nameForArgument(prop: PropAST): $FlowFixMe;
/**
* Given a property return if it is optional.
* @parameter property
* @returns: a boolean specifying if the Property is optional
*/
isOptionalProperty(property: $FlowFixMe): boolean;
getGetTypeAnnotationFN(): GetTypeAnnotationFN;
getGetSchemaInfoFN(): GetSchemaInfoFN;
/**
* Given a property return the type annotation.
* @parameter property
* @returns: the annotation for a type in the AST.
*/
getTypeAnnotationFromProperty(property: PropAST): $FlowFixMe;
getResolvedTypeAnnotation(
typeAnnotation: $FlowFixMe,
types: TypeDeclarationMap,
parser: Parser,
): {
nullable: boolean,
typeAnnotation: $FlowFixMe,
typeResolutionStatus: TypeResolutionStatus,
};
getResolveTypeAnnotationFN(): ResolveTypeAnnotationFN;
getProps(
typeDefinition: $ReadOnlyArray<PropAST>,
types: TypeDeclarationMap,
): {
props: $ReadOnlyArray<NamedShape<PropTypeAnnotation>>,
extendsProps: $ReadOnlyArray<ExtendsPropsShape>,
};
getProperties(typeName: string, types: TypeDeclarationMap): $FlowFixMe;
/**
* Given a typeAlias, it returns the next node.
*/
nextNodeForTypeAlias(typeAnnotation: $FlowFixMe): $FlowFixMe;
/**
* Given an enum Declaration, it returns the next node.
*/
nextNodeForEnum(typeAnnotation: $FlowFixMe): $FlowFixMe;
/**
* Given a unsupported typeAnnotation, returns an error message.
*/
genericTypeAnnotationErrorMessage(typeAnnotation: $FlowFixMe): string;
/**
* Given a type annotation, it extracts the type.
* @parameter typeAnnotation: the annotation for a type in the AST.
* @returns: the extracted type.
*/
extractTypeFromTypeAnnotation(typeAnnotation: $FlowFixMe): string;
/**
* Given a typeAnnotation return the properties of an object.
* @parameter property
* @returns: the properties of an object represented by a type annotation.
*/
getObjectProperties(typeAnnotation: $FlowFixMe): $FlowFixMe;
/**
* Given a option return the literal value.
* @parameter option
* @returns: the literal value of an union represented.
*/
getLiteralValue(option: $FlowFixMe): $FlowFixMe;
/**
* Given a type annotation, it returns top level name in the AST if it exists else returns null.
* @parameter typeAnnotation: the annotation for a type in the AST.
* @returns: the top level name properties in the AST if it exists else null.
*/
getPaperTopLevelNameDeprecated(typeAnnotation: $FlowFixMe): $FlowFixMe;
}