Skip to content

Commit e22d1a1

Browse files
cloudpresserfacebook-github-bot
authored andcommitted
Parser create getTypeAnnotationName(typeAnnotation) (#37580)
Summary: > [Codegen 134 - Assigned to cloudpresser] Create a function getTypeAnnotationName(typeAnnotation) in the Parser base class. Implement it using [this code for Flow](https://github.com/facebook/react-native/blob/d8ced6f8953cd896471983714e722caf50783960/packages/react-native-codegen/src/parsers/flow/components/events.js#L211) and [this code for Typescript](https://github.com/facebook/react-native/blob/d8ced6f8953cd896471983714e722caf50783960/packages/react-native-codegen/src/parsers/typescript/components/events.js#L223). Replace the callsites with the new function. This is part of #34872 bypass-github-export-checks ## Changelog: [INTERNAL] [ADDED] - getTypeAnnotationName(typeAnnotation) in parser Pull Request resolved: #37580 Test Plan: `yarn jest packages/react-native-codegen` > new tests written, as well as coverage from existing tests Reviewed By: rshest Differential Revision: D46439051 Pulled By: cipolleschi fbshipit-source-id: c0ccddc11b56d77788b4957381fbbaa82d992b01
1 parent c27e9e6 commit e22d1a1

File tree

14 files changed

+82
-43
lines changed

14 files changed

+82
-43
lines changed

packages/react-native-codegen/src/parsers/__tests__/parsers-test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,20 @@ describe('FlowParser', () => {
125125
});
126126
});
127127

128+
describe('getTypeAnnotationName', () => {
129+
it('returns type annotation name', () => {
130+
const typeAnnotation = {
131+
id: {
132+
name: 'StringTypeAnnotation',
133+
},
134+
};
135+
136+
expect(parser.getTypeAnnotationName(typeAnnotation)).toBe(
137+
'StringTypeAnnotation',
138+
);
139+
});
140+
});
141+
128142
describe('computePartialProperties', () => {
129143
it('returns partial properties', () => {
130144
const properties = [
@@ -551,6 +565,19 @@ describe('TypeScriptParser', () => {
551565
});
552566
});
553567

568+
describe('getTypeAnnotationName', () => {
569+
it('returns type annotation name', () => {
570+
const typeAnnotation = {
571+
type: 'TSTypeReference',
572+
typeName: {
573+
name: 'Foo',
574+
},
575+
};
576+
577+
expect(parser.getTypeAnnotationName(typeAnnotation)).toEqual('Foo');
578+
});
579+
});
580+
554581
describe('computePartialProperties', () => {
555582
it('returns partial properties', () => {
556583
const properties = [

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,7 @@ class UnsupportedGenericParserError extends ParserError {
117117
genericTypeAnnotation: $FlowFixMe,
118118
parser: Parser,
119119
) {
120-
const genericName = parser.nameForGenericTypeAnnotation(
121-
genericTypeAnnotation,
122-
);
120+
const genericName = parser.getTypeAnnotationName(genericTypeAnnotation);
123121
super(
124122
nativeModuleName,
125123
genericTypeAnnotation,
@@ -136,9 +134,7 @@ class MissingTypeParameterGenericParserError extends ParserError {
136134
genericTypeAnnotation: $FlowFixMe,
137135
parser: Parser,
138136
) {
139-
const genericName = parser.nameForGenericTypeAnnotation(
140-
genericTypeAnnotation,
141-
);
137+
const genericName = parser.getTypeAnnotationName(genericTypeAnnotation);
142138

143139
super(
144140
nativeModuleName,
@@ -154,9 +150,7 @@ class MoreThanOneTypeParameterGenericParserError extends ParserError {
154150
genericTypeAnnotation: $FlowFixMe,
155151
parser: Parser,
156152
) {
157-
const genericName = parser.nameForGenericTypeAnnotation(
158-
genericTypeAnnotation,
159-
);
153+
const genericName = parser.getTypeAnnotationName(genericTypeAnnotation);
160154

161155
super(
162156
nativeModuleName,

packages/react-native-codegen/src/parsers/flow/components/componentsUtils.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function getTypeAnnotationForArray<+T>(
3232

3333
if (
3434
extractedTypeAnnotation.type === 'GenericTypeAnnotation' &&
35-
extractedTypeAnnotation.id.name === 'WithDefault'
35+
parser.getTypeAnnotationName(extractedTypeAnnotation) === 'WithDefault'
3636
) {
3737
throw new Error(
3838
'Nested defaults such as "$ReadOnlyArray<WithDefault<boolean, false>>" are not supported, please declare defaults at the top level of value definitions as in "WithDefault<$ReadOnlyArray<boolean>, false>"',
@@ -82,7 +82,7 @@ function getTypeAnnotationForArray<+T>(
8282

8383
const type =
8484
extractedTypeAnnotation.type === 'GenericTypeAnnotation'
85-
? extractedTypeAnnotation.id.name
85+
? parser.getTypeAnnotationName(extractedTypeAnnotation)
8686
: extractedTypeAnnotation.type;
8787

8888
switch (type) {
@@ -170,7 +170,6 @@ function getTypeAnnotationForArray<+T>(
170170
);
171171
}
172172
default:
173-
(type: empty);
174173
throw new Error(`Unknown property type for "${name}": ${type}`);
175174
}
176175
}
@@ -220,7 +219,7 @@ function getTypeAnnotation<+T>(
220219

221220
if (
222221
typeAnnotation.type === 'GenericTypeAnnotation' &&
223-
typeAnnotation.id.name === '$ReadOnlyArray'
222+
parser.getTypeAnnotationName(typeAnnotation) === '$ReadOnlyArray'
224223
) {
225224
return {
226225
type: 'ArrayTypeAnnotation',
@@ -237,7 +236,7 @@ function getTypeAnnotation<+T>(
237236

238237
if (
239238
typeAnnotation.type === 'GenericTypeAnnotation' &&
240-
typeAnnotation.id.name === '$ReadOnly'
239+
parser.getTypeAnnotationName(typeAnnotation) === '$ReadOnly'
241240
) {
242241
return {
243242
type: 'ObjectTypeAnnotation',
@@ -253,7 +252,7 @@ function getTypeAnnotation<+T>(
253252

254253
const type =
255254
typeAnnotation.type === 'GenericTypeAnnotation'
256-
? typeAnnotation.id.name
255+
? parser.getTypeAnnotationName(typeAnnotation)
257256
: typeAnnotation.type;
258257

259258
switch (type) {
@@ -379,7 +378,6 @@ function getTypeAnnotation<+T>(
379378
type: 'MixedTypeAnnotation',
380379
};
381380
default:
382-
(type: empty);
383381
throw new Error(
384382
`Unknown property type for "${name}": "${type}" in the State`,
385383
);

packages/react-native-codegen/src/parsers/flow/components/events.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function getPropertyType(
4545
typeAnnotation: $FlowFixMe,
4646
parser: Parser,
4747
): NamedShape<EventTypeAnnotation> {
48-
const type = parser.extractTypeFromTypeAnnotation(typeAnnotation);
48+
const type = extractTypeFromTypeAnnotation(typeAnnotation, parser);
4949

5050
switch (type) {
5151
case 'BooleanTypeAnnotation':
@@ -94,7 +94,7 @@ function extractArrayElementType(
9494
name: string,
9595
parser: Parser,
9696
): EventTypeAnnotation {
97-
const type = parser.extractTypeFromTypeAnnotation(typeAnnotation);
97+
const type = extractTypeFromTypeAnnotation(typeAnnotation, parser);
9898

9999
switch (type) {
100100
case 'BooleanTypeAnnotation':
@@ -163,6 +163,15 @@ function prettify(jsonObject: $FlowFixMe): string {
163163
return JSON.stringify(jsonObject, null, 2);
164164
}
165165

166+
function extractTypeFromTypeAnnotation(
167+
typeAnnotation: $FlowFixMe,
168+
parser: Parser,
169+
): string {
170+
return typeAnnotation.type === 'GenericTypeAnnotation'
171+
? parser.getTypeAnnotationName(typeAnnotation)
172+
: typeAnnotation.type;
173+
}
174+
166175
function findEventArgumentsAndType(
167176
parser: Parser,
168177
typeAnnotation: $FlowFixMe,
@@ -175,7 +184,7 @@ function findEventArgumentsAndType(
175184
paperTopLevelNameDeprecated: ?$FlowFixMe,
176185
} {
177186
throwIfEventHasNoName(typeAnnotation, parser);
178-
const name = typeAnnotation.id.name;
187+
const name = parser.getTypeAnnotationName(typeAnnotation);
179188
if (name === '$ReadOnly') {
180189
return {
181190
argumentProps: typeAnnotation.typeParameters.params[0].properties,
@@ -236,8 +245,8 @@ function buildEventSchema(
236245

237246
if (
238247
typeAnnotation.type !== 'GenericTypeAnnotation' ||
239-
(typeAnnotation.id.name !== 'BubblingEventHandler' &&
240-
typeAnnotation.id.name !== 'DirectEventHandler')
248+
(parser.getTypeAnnotationName(typeAnnotation) !== 'BubblingEventHandler' &&
249+
parser.getTypeAnnotationName(typeAnnotation) !== 'DirectEventHandler')
241250
) {
242251
return null;
243252
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function translateTypeAnnotation(
6464

6565
switch (typeAnnotation.type) {
6666
case 'GenericTypeAnnotation': {
67-
switch (typeAnnotation.id.name) {
67+
switch (parser.getTypeAnnotationName(typeAnnotation)) {
6868
case 'RootTag': {
6969
return emitRootTag(nullable);
7070
}

packages/react-native-codegen/src/parsers/flow/parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class FlowParser implements Parser {
9999
return 'Flow';
100100
}
101101

102-
nameForGenericTypeAnnotation(typeAnnotation: $FlowFixMe): string {
102+
getTypeAnnotationName(typeAnnotation: $FlowFixMe): string {
103103
return typeAnnotation?.id?.name;
104104
}
105105

@@ -421,7 +421,7 @@ class FlowParser implements Parser {
421421
break;
422422
}
423423

424-
const typeAnnotationName = this.nameForGenericTypeAnnotation(node);
424+
const typeAnnotationName = this.getTypeAnnotationName(node);
425425
const resolvedTypeAnnotation = types[typeAnnotationName];
426426
if (resolvedTypeAnnotation == null) {
427427
break;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ export interface Parser {
127127
*/
128128
language(): ParserType;
129129
/**
130-
* Given a type annotation for a generic type, it returns the type name.
130+
* Given a type annotation, it returns the type name.
131131
* @parameter typeAnnotation: the annotation for a type in the AST.
132132
* @returns: the name of the type.
133133
*/
134-
nameForGenericTypeAnnotation(typeAnnotation: $FlowFixMe): string;
134+
getTypeAnnotationName(typeAnnotation: $FlowFixMe): string;
135135
/**
136136
* Given a type arguments, it returns a boolean specifying if the Module is Invalid.
137137
* @parameter typeArguments: the type arguments.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ export class MockedParser implements Parser {
9696
return 'Flow';
9797
}
9898

99-
nameForGenericTypeAnnotation(typeAnnotation: $FlowFixMe): string {
100-
return typeAnnotation.id.name;
99+
getTypeAnnotationName(typeAnnotation: $FlowFixMe): string {
100+
return typeAnnotation?.id?.name;
101101
}
102102

103103
checkIfInvalidModule(typeArguments: $FlowFixMe): boolean {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ function getCommandTypeNameAndOptionsExpression(
818818
}
819819

820820
return {
821-
commandTypeName: parser.nameForGenericTypeAnnotation(typeArgumentParam),
821+
commandTypeName: parser.getTypeAnnotationName(typeArgumentParam),
822822
commandOptionsExpression: callExpression.arguments[0],
823823
};
824824
}
@@ -998,7 +998,7 @@ function getTypeResolutionStatus(
998998
return {
999999
successful: true,
10001000
type,
1001-
name: parser.nameForGenericTypeAnnotation(typeAnnotation),
1001+
name: parser.getTypeAnnotationName(typeAnnotation),
10021002
};
10031003
}
10041004

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ function emitCommonTypes(
611611
}
612612

613613
const genericTypeAnnotationName =
614-
parser.nameForGenericTypeAnnotation(typeAnnotation);
614+
parser.getTypeAnnotationName(typeAnnotation);
615615

616616
const emitter = typeMap[genericTypeAnnotationName];
617617
if (!emitter) {

0 commit comments

Comments
 (0)