File tree 6 files changed +85
-11
lines changed
packages/react-native-codegen/src/parsers
6 files changed +85
-11
lines changed Original file line number Diff line number Diff line change 11
11
'use strict' ;
12
12
13
13
const invariant = require ( 'invariant' ) ;
14
-
14
+ import type { Parser } from './parser' ;
15
15
export type ParserType = 'Flow' | 'TypeScript' ;
16
16
17
17
class ParserError extends Error {
@@ -110,23 +110,22 @@ class UnsupportedTypeAnnotationParserError extends ParserError {
110
110
}
111
111
112
112
class UnsupportedGenericParserError extends ParserError {
113
- + genericName : string ;
113
+ // +genericName: string;
114
114
constructor (
115
115
nativeModuleName : string ,
116
116
genericTypeAnnotation : $FlowFixMe ,
117
- language : ParserType ,
117
+ parser : Parser ,
118
118
) {
119
- const genericName =
120
- language === 'TypeScript'
121
- ? genericTypeAnnotation . typeName . name
122
- : genericTypeAnnotation . id . name ;
119
+ const genericName = parser . nameForGenericTypeAnnotation (
120
+ genericTypeAnnotation ,
121
+ ) ;
123
122
super (
124
123
nativeModuleName ,
125
124
genericTypeAnnotation ,
126
125
`Unrecognized generic type '${ genericName } ' in NativeModule spec.` ,
127
126
) ;
128
127
129
- this . genericName = genericName ;
128
+ // this.genericName = genericName;
130
129
}
131
130
}
132
131
Original file line number Diff line number Diff line change @@ -81,7 +81,10 @@ const {
81
81
throwIfMoreThanOneModuleInterfaceParserError,
82
82
} = require ( '../../error-utils' ) ;
83
83
84
+ const { FlowParser} = require ( '../parser.js' ) ;
85
+
84
86
const language = 'Flow' ;
87
+ const parser = new FlowParser ( ) ;
85
88
86
89
function translateArrayTypeAnnotation (
87
90
hasteModuleName : string ,
@@ -276,7 +279,7 @@ function translateTypeAnnotation(
276
279
throw new UnsupportedGenericParserError (
277
280
hasteModuleName ,
278
281
typeAnnotation ,
279
- language ,
282
+ parser ,
280
283
) ;
281
284
}
282
285
}
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ * @flow strict
8
+ * @format
9
+ */
10
+
11
+ 'use strict' ;
12
+
13
+ import type { Parser } from '../parser' ;
14
+
15
+ class FlowParser implements Parser {
16
+ nameForGenericTypeAnnotation ( typeAnnotation : $FlowFixMe ) : string {
17
+ return typeAnnotation . id . name ;
18
+ }
19
+ }
20
+
21
+ module . exports = {
22
+ FlowParser,
23
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ * @flow strict
8
+ * @format
9
+ */
10
+
11
+ 'use strict' ;
12
+
13
+ /**
14
+ * This is the main interface for Parsers of various languages.
15
+ * It exposes all the methods that contain language-specific logic.
16
+ */
17
+ export interface Parser {
18
+ /**
19
+ * Given a type annotation for a generic type, it returns the type name.
20
+ * @parameter typeAnnotation: the annotation for a type in the AST.
21
+ * @returns : the name of the type.
22
+ */
23
+ nameForGenericTypeAnnotation ( typeAnnotation : $FlowFixMe ) : string ;
24
+ }
Original file line number Diff line number Diff line change @@ -80,7 +80,10 @@ const {
80
80
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
81
81
} = require ( '../../error-utils' ) ;
82
82
83
+ const { TypeScriptParser} = require ( '../parser' ) ;
84
+
83
85
const language = 'TypeScript' ;
86
+ const parser = new TypeScriptParser ( ) ;
84
87
85
88
function translateArrayTypeAnnotation (
86
89
hasteModuleName : string ,
@@ -205,7 +208,7 @@ function translateTypeAnnotation(
205
208
throw new UnsupportedGenericParserError (
206
209
hasteModuleName ,
207
210
typeAnnotation ,
208
- language ,
211
+ parser ,
209
212
) ;
210
213
}
211
214
}
@@ -290,7 +293,7 @@ function translateTypeAnnotation(
290
293
throw new UnsupportedGenericParserError (
291
294
hasteModuleName ,
292
295
typeAnnotation ,
293
- language ,
296
+ parser ,
294
297
) ;
295
298
}
296
299
}
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ * @flow strict
8
+ * @format
9
+ */
10
+
11
+ 'use strict' ;
12
+
13
+ import type { Parser } from '../parser' ;
14
+
15
+ class TypeScriptParser implements Parser {
16
+ nameForGenericTypeAnnotation ( typeAnnotation : $FlowFixMe ) : string {
17
+ return typeAnnotation . typeName . name ;
18
+ }
19
+ }
20
+ module . exports = {
21
+ TypeScriptParser,
22
+ } ;
You can’t perform that action at this time.
0 commit comments