expo-type-information 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/build/cli.js +1 -1
  2. package/build/cli.js.map +1 -1
  3. package/build/commands/commandUtils.js +2 -2
  4. package/build/commands/commandUtils.js.map +1 -1
  5. package/build/commands/generateJSXIntrinsicsCommand.js +3 -1
  6. package/build/commands/generateJSXIntrinsicsCommand.js.map +1 -1
  7. package/build/commands/generateMocksForFileCommand.js +3 -1
  8. package/build/commands/generateMocksForFileCommand.js.map +1 -1
  9. package/build/commands/generateModuleTypesCommand.js +3 -1
  10. package/build/commands/generateModuleTypesCommand.js.map +1 -1
  11. package/build/commands/generateViewTypesCommand.js +3 -1
  12. package/build/commands/generateViewTypesCommand.js.map +1 -1
  13. package/build/commands/inlineModulesInterfaceCommand.js +5 -2
  14. package/build/commands/inlineModulesInterfaceCommand.js.map +1 -1
  15. package/build/commands/moduleInterfaceCommand.js +7 -1
  16. package/build/commands/moduleInterfaceCommand.js.map +1 -1
  17. package/build/commands/shortModuleInterfaceCommand.js +1 -1
  18. package/build/commands/shortModuleInterfaceCommand.js.map +1 -1
  19. package/build/commands/typeInformationCommand.js +3 -1
  20. package/build/commands/typeInformationCommand.js.map +1 -1
  21. package/build/index.d.ts +1 -1
  22. package/build/index.js.map +1 -1
  23. package/build/mockgen.d.ts +21 -0
  24. package/build/mockgen.js +21 -0
  25. package/build/mockgen.js.map +1 -1
  26. package/build/typeInformation.d.ts +113 -11
  27. package/build/typeInformation.js +21 -8
  28. package/build/typeInformation.js.map +1 -1
  29. package/build/typescriptGeneration.d.ts +50 -0
  30. package/build/typescriptGeneration.js +41 -0
  31. package/build/typescriptGeneration.js.map +1 -1
  32. package/package.json +2 -2
  33. package/src/cli.ts +1 -2
  34. package/src/commands/commandUtils.ts +2 -2
  35. package/src/commands/generateJSXIntrinsicsCommand.ts +6 -4
  36. package/src/commands/generateMocksForFileCommand.ts +4 -4
  37. package/src/commands/generateModuleTypesCommand.ts +4 -4
  38. package/src/commands/generateViewTypesCommand.ts +4 -4
  39. package/src/commands/inlineModulesInterfaceCommand.ts +10 -3
  40. package/src/commands/moduleInterfaceCommand.ts +7 -1
  41. package/src/commands/shortModuleInterfaceCommand.ts +1 -1
  42. package/src/commands/typeInformationCommand.ts +4 -4
  43. package/src/index.ts +1 -0
  44. package/src/mockgen.ts +21 -0
  45. package/src/typeInformation.ts +119 -11
  46. package/src/typescriptGeneration.ts +50 -0
@@ -8,6 +8,9 @@ import {
8
8
  } from './swift/sourcekittenTypeInformation';
9
9
  import { taskAll } from './utils';
10
10
 
11
+ /**
12
+ * Represents the kind of a parsed identifier from a native file.
13
+ */
11
14
  export enum IdentifierKind {
12
15
  BASIC,
13
16
  ENUM,
@@ -15,40 +18,85 @@ export enum IdentifierKind {
15
18
  CLASS,
16
19
  }
17
20
 
21
+ /**
22
+ * Represents a parametrized type, that is a generic type with specified parameters e.g. Map<string, number>.
23
+ */
18
24
  export type ParametrizedType = {
19
25
  name: TypeIdentifier;
20
26
  types: Type[];
21
27
  };
22
28
 
29
+ /**
30
+ * Represents an argument passed to a function or constructor.
31
+ */
23
32
  export type Argument = { name: string | undefined; type: Type };
33
+
34
+ /**
35
+ * Represents a single field within a record or a struct.
36
+ */
24
37
  export type Field = Argument;
25
38
 
39
+ /**
40
+ * Represents a struct or dictionary-like record consisting of named fields.
41
+ */
26
42
  export type RecordType = {
27
43
  name: string;
28
44
  fields: Field[];
29
45
  };
30
46
 
47
+ /**
48
+ * Represents a single case inside an enum declaration.
49
+ */
31
50
  export type EnumCase = string;
32
51
 
52
+ /**
53
+ * Represents an enum type, containing its name and all associated cases.
54
+ */
33
55
  export type EnumType = {
34
56
  name: string;
35
57
  cases: EnumCase[];
36
58
  };
37
59
 
60
+ /**
61
+ * Represents a union or a sum type where a value can be one of several different types.
62
+ */
38
63
  export type SumType = {
39
64
  types: Type[];
40
65
  };
41
66
 
67
+ /**
68
+ * Represents a dictionary type, defining the explicit types for its keys and values.
69
+ */
42
70
  export type DictionaryType = {
43
71
  key: Type;
44
72
  value: Type;
45
73
  };
46
74
 
75
+ /**
76
+ * Represents an optional type that can also resolve to null or undefined.
77
+ * > **Note:** The information that this type is optional is implicit and exists only in the type system and on the parent type. There is no field on the `OptionalType` object that explicitly indicates that.
78
+ */
47
79
  export type OptionalType = Type;
80
+
81
+ /**
82
+ * Represents a list or array of a specific type.
83
+ * > **Note:** The information that this type is array is implicit and exists only in the type system and on the parent type. There is no field on the `ArrayType` object that explicitly indicates that.
84
+ */
48
85
  export type ArrayType = Type;
86
+
87
+ /**
88
+ * Represents a type identifier as a string reference.
89
+ */
49
90
  export type TypeIdentifier = string;
91
+
92
+ /**
93
+ * Represents an anonymous type, a one that is not named instead written directly in the code, such as inline generics, arrays, or optionals.
94
+ */
50
95
  export type AnonymousType = ParametrizedType | SumType | OptionalType | DictionaryType | ArrayType;
51
96
 
97
+ /**
98
+ * Categorizes the type node within the abstract syntax tree.
99
+ */
52
100
  export enum TypeKind {
53
101
  BASIC,
54
102
  IDENTIFIER,
@@ -59,6 +107,9 @@ export enum TypeKind {
59
107
  DICTIONARY,
60
108
  }
61
109
 
110
+ /**
111
+ * Represents a basic type that is not user defined.
112
+ */
62
113
  export enum BasicType {
63
114
  ANY,
64
115
  STRING,
@@ -66,32 +117,55 @@ export enum BasicType {
66
117
  BOOLEAN,
67
118
  VOID,
68
119
  UNDEFINED,
120
+ /** Represents a type that couldn't be resolved */
69
121
  UNRESOLVED,
70
122
  }
71
123
 
124
+ /**
125
+ * Represents an abstract type node.
126
+ */
72
127
  export type Type = {
73
128
  kind: TypeKind;
74
129
  type: BasicType | TypeIdentifier | AnonymousType;
75
130
  };
76
131
 
132
+ /**
133
+ * Represents a DSL property declaration.
134
+ */
77
135
  export type PropertyDeclaration = ConstantDeclaration;
136
+
137
+ /**
138
+ * Represents a DSL view declaration.
139
+ */
78
140
  export type ViewDeclaration = ModuleClassDeclaration;
141
+
142
+ /**
143
+ * Represents a DSL event declaration.
144
+ */
79
145
  export type EventDeclaration = string;
80
146
 
81
147
  /**
82
- * Retain information of where the thing was defined in the file.
148
+ * Retains information of where the thing was defined in the file.
83
149
  * As collecting type information is written in asynchronous way it is non-deterministic.
84
- * To make it deterministic we just sort the declaration by the definitionOffset, maintianing the same ordering as in original file.
150
+ * To make it deterministic we just sort the declaration by the definitionOffset, maintaining the same ordering as in original file.
151
+ * @header TypeInfoTypes
85
152
  */
86
153
  export type DefinitionOffset = {
87
154
  definitionOffset: number;
88
155
  };
89
156
 
157
+ /**
158
+ * Represents a DSL constant declaration.
159
+ */
90
160
  export type ConstantDeclaration = {
91
161
  name: string;
92
162
  type: Type;
93
163
  } & DefinitionOffset;
94
164
 
165
+ /**
166
+ * Represents a DSL function declaration.
167
+ * @hideType
168
+ */
95
169
  export type FunctionDeclaration = {
96
170
  name: string;
97
171
  returnType: Type;
@@ -99,15 +173,24 @@ export type FunctionDeclaration = {
99
173
  parameters: Type[];
100
174
  } & DefinitionOffset;
101
175
 
176
+ /**
177
+ * Represents a DSL prop declaration.
178
+ */
102
179
  export type PropDeclaration = {
103
180
  name: string;
104
181
  arguments: Argument[];
105
182
  } & DefinitionOffset;
106
183
 
184
+ /**
185
+ * Represents a DSL class constructor declaration.
186
+ */
107
187
  export type ConstructorDeclaration = {
108
188
  arguments: Argument[];
109
189
  } & DefinitionOffset;
110
190
 
191
+ /**
192
+ * Represents a DSL native class declaration.
193
+ */
111
194
  export type ClassDeclaration = {
112
195
  name: string;
113
196
  constructor: ConstructorDeclaration | null;
@@ -116,6 +199,9 @@ export type ClassDeclaration = {
116
199
  properties: PropertyDeclaration[];
117
200
  } & DefinitionOffset;
118
201
 
202
+ /**
203
+ * Represents a DSL module declaration.
204
+ */
119
205
  export type ModuleClassDeclaration = {
120
206
  name: string;
121
207
  constructor: ConstructorDeclaration | null;
@@ -129,15 +215,27 @@ export type ModuleClassDeclaration = {
129
215
  events: EventDeclaration[];
130
216
  } & DefinitionOffset;
131
217
 
218
+ /**
219
+ * Represents a definition of an identifier.
220
+ */
132
221
  export type IdentifierDefinition = {
133
222
  kind: IdentifierKind;
134
223
  definition: string | RecordType | EnumType | ClassDeclaration;
135
224
  };
136
225
 
226
+ /**
227
+ * Maps type identifier strings to their definition objects.
228
+ */
137
229
  export type TypeIdentifierDefinitionMap = Map<string, IdentifierDefinition>;
138
230
 
231
+ /**
232
+ * Serialized version of the `TypeIdentifierDefinitionMap`.
233
+ */
139
234
  export type TypeIdentifierDefinitionList = [string, IdentifierDefinition][];
140
235
 
236
+ /**
237
+ * Serialized version of the `FileTypeInformation`, suitable for JSON storage or testing environments.
238
+ */
141
239
  export type FileTypeInformationSerialized = {
142
240
  usedTypeIdentifiersList: string[];
143
241
  declaredTypeIdentifiersList: string[];
@@ -149,9 +247,10 @@ export type FileTypeInformationSerialized = {
149
247
  };
150
248
 
151
249
  /**
152
- * FileTypeInformation object abstracts over type related information in a file.
250
+ * `FileTypeInformation` object abstracts over type related information in a file.
153
251
  * The abstraction is closely related to Typescript and expo NativeModules (both to be independent of the actual native side
154
252
  * and to give accurate information about what and how we can use the given module).
253
+ * @header TypeInfoTypes
155
254
  */
156
255
  export type FileTypeInformation = {
157
256
  /**
@@ -190,9 +289,10 @@ export type FileTypeInformation = {
190
289
  };
191
290
 
192
291
  /**
193
- * Used for testing purposes, maps Sets and Maps to Arrays and returns FileTypeInformationSerialized object which can be written to a JSON.
194
- * @param param0 FileTypeInformation object to serialize.
195
- * @returns FileTypeInformationSerialized object.
292
+ * Used for testing purposes, maps Sets and Maps to Arrays and returns `FileTypeInformationSerialized` object which can be written to a JSON.
293
+ * @param fileTypeinformation `FileTypeInformation` object to serialize.
294
+ * @returns a `FileTypeInformationSerialized` object.
295
+ * @header TypeInformationAbstraction
196
296
  */
197
297
  export function serializeTypeInformation({
198
298
  usedTypeIdentifiers,
@@ -215,9 +315,10 @@ export function serializeTypeInformation({
215
315
  }
216
316
 
217
317
  /**
218
- * Used for testing purposes, maps Arrays to Sets and Maps depending on the field and returns FileTypeInformation object.
219
- * @param param0 FileTypeInformationSerialized object to deserialize.
220
- * @returns FileTypeInformation object.
318
+ * Used for testing purposes, maps Arrays to Sets and Maps depending on the field and returns `FileTypeInformation` object.
319
+ * @param fileTypeinformationSerialized `FileTypeInformationSerialized` object to deserialize.
320
+ * @returns `FileTypeInformation` object.
321
+ * @header TypeInformationAbstraction
221
322
  */
222
323
  export function deserializeTypeInformation({
223
324
  usedTypeIdentifiersList,
@@ -241,7 +342,7 @@ export function deserializeTypeInformation({
241
342
 
242
343
  /**
243
344
  * Defines the level of type inference to apply when extracting type information.
244
- * Note: In case where type inference is on, it may take more then twice the time to compute the type information.
345
+ * > **Note:** In case where type inference is on, it may take more then twice the time to compute the type information.
245
346
  */
246
347
  export enum TypeInferenceOption {
247
348
  /** No type inference will be performed. */
@@ -252,12 +353,18 @@ export enum TypeInferenceOption {
252
353
  PREPROCESS_AND_INFERENCE,
253
354
  }
254
355
 
356
+ /**
357
+ * Defines an input option for extracting type information directly from a raw string of source code.
358
+ */
255
359
  export type StringInputOption = {
256
360
  type: 'string';
257
361
  fileContent: string;
258
362
  language: 'Swift';
259
363
  };
260
364
 
365
+ /**
366
+ * Defines an input option for extracting type information from a set of physical files.
367
+ */
261
368
  export type FileInputOption = {
262
369
  type: 'file';
263
370
  inputFileAbsolutePaths: string[];
@@ -297,7 +404,8 @@ async function withTempFile<T>(content: string, fn: (filePath: string) => Promis
297
404
  * If a raw string is provided, or if the `PREPROCESS_AND_INFERENCE` inference option is selected,
298
405
  * the function will create a temporary file with the (optionally preprocessed) content to facilitate parsing.
299
406
  * @param options - Configuration object containing the input source (file or string) and the desired level of type inference.
300
- * @returns A promise that resolves to a `FileTypeInformation` object if the input was parsed successfully. Otherwise, it returns `null`.
407
+ * @returns A promise that resolves to a `FileTypeInformation` object if the input was parsed successfully. Otherwise, it resolves to `null`.
408
+ * @header TypeInformationAbstraction
301
409
  */
302
410
  export async function getFileTypeInformation({
303
411
  input,
@@ -40,8 +40,17 @@ const voidKeywordType = () => ts.factory.createKeywordTypeNode(ts.SyntaxKind.Voi
40
40
 
41
41
  const newlineIdentifier = () => ts.factory.createIdentifier('\n\n');
42
42
 
43
+ /**
44
+ * A helper type which contains the generated file content and name.
45
+ */
43
46
  export type OutputFile = {
47
+ /**
48
+ * @field Generated file content.
49
+ */
44
50
  content: string;
51
+ /**
52
+ * @field Generated file base name (e.g. `ExpoSettings.types.ts`).
53
+ */
45
54
  name: string;
46
55
  };
47
56
 
@@ -999,6 +1008,14 @@ async function tsNodesToString(elements: ts.Node[]): Promise<string> {
999
1008
  return await prettifyCode(printedTs, 'typescript');
1000
1009
  }
1001
1010
 
1011
+ /**
1012
+ * Helper function that takes a file content string and formats it using `prettier` formatter.
1013
+ * @param text Content of a JavaScript/TypeScript file to format.
1014
+ * @param parser An option of which parser to use to format the file.
1015
+ * @default "babel"
1016
+ * @returns A promise which resolves to the `text` string after formatting using `prettier` with the given `parser`.
1017
+ * @header TypescriptGeneration
1018
+ */
1002
1019
  export async function prettifyCode(text: string, parser: 'babel' | 'typescript' = 'babel') {
1003
1020
  return await prettier.format(text, {
1004
1021
  parser,
@@ -1009,6 +1026,12 @@ export async function prettifyCode(text: string, parser: 'babel' | 'typescript'
1009
1026
  });
1010
1027
  }
1011
1028
 
1029
+ /**
1030
+ * Generates the TypeScript string content for a native View's type declaration file.
1031
+ * @param fileTypeInformation The abstracted type information of an Expo module.
1032
+ * @returns A promise that resolves to a string containing the TypeScript declaration file content or `null` if the generation has failed.
1033
+ * @header TypescriptGeneration
1034
+ */
1012
1035
  export async function generateViewTypesFileContent(
1013
1036
  fileTypeInformation: FileTypeInformation
1014
1037
  ): Promise<string | null> {
@@ -1019,6 +1042,12 @@ export async function generateViewTypesFileContent(
1019
1042
  return tsNodesToString(buildViewDeclarationNodes(ctx));
1020
1043
  }
1021
1044
 
1045
+ /**
1046
+ * Generates the TypeScript string content for a native View's type declaration file which mounts the View props on the global `JSXIntrinsics`.
1047
+ * @param fileTypeInformation The abstracted type information of an Expo module.
1048
+ * @returns A promise that resolves to a string containing the TypeScript declaration file content or `null` if the generation has failed.
1049
+ * @header TypescriptGeneration
1050
+ */
1022
1051
  export async function generateJSXIntrinsicsFileContent(
1023
1052
  fileTypeInformation: FileTypeInformation
1024
1053
  ): Promise<string | null> {
@@ -1029,6 +1058,12 @@ export async function generateJSXIntrinsicsFileContent(
1029
1058
  return tsNodesToString(buildJSXIntrinsicsViewNodes(ctx));
1030
1059
  }
1031
1060
 
1061
+ /**
1062
+ * Generates the TypeScript string content for a native module type declaration file.
1063
+ * @param fileTypeInformation The abstracted type information of an Expo module.
1064
+ * @returns A promise that resolves to a string containing the TypeScript module declaration file content or `null` if the generation has failed.
1065
+ * @header TypescriptGeneration
1066
+ */
1032
1067
  export async function generateModuleTypesFileContent(
1033
1068
  fileTypeInformation: FileTypeInformation
1034
1069
  ): Promise<string | null> {
@@ -1039,6 +1074,13 @@ export async function generateModuleTypesFileContent(
1039
1074
  return tsNodesToString(buildModuleDeclarationNodes(ctx));
1040
1075
  }
1041
1076
 
1077
+ /**
1078
+ * Generates a short TypeScript interface for an Expo module. This creates the content for two files: a volatile generated file containing raw type definitions,
1079
+ * and a stable user-facing file that wraps and exports the native module methods in new functions.
1080
+ * @param fileTypeInformation The abstracted type information of an Expo module.
1081
+ * @returns A promise that resolves to an object containing the string contents for both the volatile generated file and the stable TypeScript interface file.
1082
+ * @header TypescriptGeneration
1083
+ */
1042
1084
  export async function generateConciseTsInterface(
1043
1085
  fileTypeInformation: FileTypeInformation
1044
1086
  ): Promise<{
@@ -1062,6 +1104,14 @@ export async function generateConciseTsInterface(
1062
1104
  };
1063
1105
  }
1064
1106
 
1107
+ /**
1108
+ * Generates a full, multi-file TypeScript interface for an Expo module.
1109
+ * The generated interface is separated into a file with type definitions, a file which wraps the native module, a file for each view defined in a module and an index file which reexports all definitions from the other files.
1110
+ *
1111
+ * @param fileTypeInformation The abstracted type information of an Expo module.
1112
+ * @returns A promise that resolves to an object containing the string contents for all of the generated files or `null` if the generation has failed.
1113
+ * @header TypescriptGeneration
1114
+ */
1065
1115
  export async function generateFullTsInterface(fileTypeInformation: FileTypeInformation): Promise<{
1066
1116
  moduleTypesFile: OutputFile;
1067
1117
  moduleViewsFiles: OutputFile[];