Skip to content

Commit 5940d25

Browse files
cipolleschifacebook-github-bot
authored andcommitted
Create the Parser interface (#35036)
Summary: Pull Request resolved: #35036 This diff is the base to create a polimorphic behavior for TypeScript and Flow parser. This type will allow to share a lot of code between the parsers and also to keep their differences a part. It will be the base diff/PR for further tasks in the Codegen umbrella Issue ## Changelog: [General][Added] - Parser interface to divide parser logic. Reviewed By: cortinico Differential Revision: D40548707 fbshipit-source-id: e632ba52b00b43e50306e3a792a841e72e8c07f4
1 parent c419b4f commit 5940d25

File tree

6 files changed

+85
-11
lines changed

6 files changed

+85
-11
lines changed

packages/react-native-codegen/src/parsers/errors.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
'use strict';
1212

1313
const invariant = require('invariant');
14-
14+
import type {Parser} from './parser';
1515
export type ParserType = 'Flow' | 'TypeScript';
1616

1717
class ParserError extends Error {
@@ -110,23 +110,22 @@ class UnsupportedTypeAnnotationParserError extends ParserError {
110110
}
111111

112112
class UnsupportedGenericParserError extends ParserError {
113-
+genericName: string;
113+
// +genericName: string;
114114
constructor(
115115
nativeModuleName: string,
116116
genericTypeAnnotation: $FlowFixMe,
117-
language: ParserType,
117+
parser: Parser,
118118
) {
119-
const genericName =
120-
language === 'TypeScript'
121-
? genericTypeAnnotation.typeName.name
122-
: genericTypeAnnotation.id.name;
119+
const genericName = parser.nameForGenericTypeAnnotation(
120+
genericTypeAnnotation,
121+
);
123122
super(
124123
nativeModuleName,
125124
genericTypeAnnotation,
126125
`Unrecognized generic type '${genericName}' in NativeModule spec.`,
127126
);
128127

129-
this.genericName = genericName;
128+
// this.genericName = genericName;
130129
}
131130
}
132131

packages/react-native-codegen/src/parsers/flow/modules/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ const {
8181
throwIfMoreThanOneModuleInterfaceParserError,
8282
} = require('../../error-utils');
8383

84+
const {FlowParser} = require('../parser.js');
85+
8486
const language = 'Flow';
87+
const parser = new FlowParser();
8588

8689
function translateArrayTypeAnnotation(
8790
hasteModuleName: string,
@@ -276,7 +279,7 @@ function translateTypeAnnotation(
276279
throw new UnsupportedGenericParserError(
277280
hasteModuleName,
278281
typeAnnotation,
279-
language,
282+
parser,
280283
);
281284
}
282285
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

packages/react-native-codegen/src/parsers/typescript/modules/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ const {
8080
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
8181
} = require('../../error-utils');
8282

83+
const {TypeScriptParser} = require('../parser');
84+
8385
const language = 'TypeScript';
86+
const parser = new TypeScriptParser();
8487

8588
function translateArrayTypeAnnotation(
8689
hasteModuleName: string,
@@ -205,7 +208,7 @@ function translateTypeAnnotation(
205208
throw new UnsupportedGenericParserError(
206209
hasteModuleName,
207210
typeAnnotation,
208-
language,
211+
parser,
209212
);
210213
}
211214
}
@@ -290,7 +293,7 @@ function translateTypeAnnotation(
290293
throw new UnsupportedGenericParserError(
291294
hasteModuleName,
292295
typeAnnotation,
293-
language,
296+
parser,
294297
);
295298
}
296299
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
};

0 commit comments

Comments
 (0)