Skip to content

Commit ec66f2e

Browse files
Shubham-TJfacebook-github-bot
authored andcommitted
Codegen 113: add isOptionalProperty in parser (#37134)
Summary: part of codegen issue #34872 Add a function isOptionalProperty(property) in the Parser object and implement it in FlowParser and TypeScriptParser, using the implementation you can find in the [parsers/flow/components/events.js](https://github.com/facebook/react-native/blob/e133100721939108b0f28dfa9f60ac627c804018/packages/react-native-codegen/src/parsers/flow/components/events.js#L172-L173) and [parsers/typescript/components/events.js](https://github.com/facebook/react-native/blob/e133100721939108b0f28dfa9f60ac627c804018/packages/react-native-codegen/src/parsers/typescript/components/events.js#L196). Use the parsers in the buildPropertiesForEvent. bypass-github-export-checks ## Changelog: [Internal][Changed]: add isOptionalProperty in parser and use it in parser events. <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests Pull Request resolved: #37134 Test Plan: `yarn test react-native-codegen` Reviewed By: christophpurrer Differential Revision: D45390880 Pulled By: cipolleschi fbshipit-source-id: bb2575b8602c6a15be0a87817ca1961ae834324e
1 parent c027f0a commit ec66f2e

File tree

8 files changed

+148
-26
lines changed

8 files changed

+148
-26
lines changed

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,41 @@ describe('FlowParser', () => {
270270
).toEqual(expected);
271271
});
272272
});
273+
274+
describe('isOptionalProperty', () => {
275+
it('when property is optional', () => {
276+
const property = {
277+
value: {
278+
type: 'TypeAnnotation',
279+
},
280+
optional: true,
281+
};
282+
283+
expect(parser.isOptionalProperty(property)).toEqual(true);
284+
});
285+
286+
it('when property is not optional', () => {
287+
const property = {
288+
value: {
289+
type: 'TypeAnnotation',
290+
},
291+
optional: false,
292+
};
293+
294+
expect(parser.isOptionalProperty(property)).toEqual(false);
295+
});
296+
297+
it('when property value type is NullableTypeAnnotation', () => {
298+
const property = {
299+
value: {
300+
type: 'NullableTypeAnnotation',
301+
},
302+
optional: false,
303+
};
304+
305+
expect(parser.isOptionalProperty(property)).toEqual(true);
306+
});
307+
});
273308
});
274309

275310
describe('TypeScriptParser', () => {
@@ -531,4 +566,20 @@ describe('TypeScriptParser', () => {
531566
).toEqual(expected);
532567
});
533568
});
569+
570+
describe('isOptionalProperty', () => {
571+
it('when property is optional', () => {
572+
const property = {
573+
optional: true,
574+
};
575+
expect(parser.isOptionalProperty(property)).toEqual(true);
576+
});
577+
578+
it('when property is undefined or not optional', () => {
579+
const property = {
580+
optional: false,
581+
};
582+
expect(parser.isOptionalProperty(property)).toEqual(false);
583+
});
584+
});
534585
});

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

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ function getPropertyType(
2828
name,
2929
optional: boolean,
3030
typeAnnotation: $FlowFixMe,
31+
parser: Parser,
3132
): NamedShape<EventTypeAnnotation> {
3233
const type = extractTypeFromTypeAnnotation(typeAnnotation);
3334

@@ -77,14 +78,17 @@ function getPropertyType(
7778
name,
7879
optional,
7980
typeAnnotation.typeParameters.params[0],
81+
parser,
8082
);
8183
case 'ObjectTypeAnnotation':
8284
return {
8385
name,
8486
optional,
8587
typeAnnotation: {
8688
type: 'ObjectTypeAnnotation',
87-
properties: typeAnnotation.properties.map(buildPropertiesForEvent),
89+
properties: typeAnnotation.properties.map(member =>
90+
buildPropertiesForEvent(member, parser),
91+
),
8892
},
8993
};
9094
case 'UnionTypeAnnotation':
@@ -109,7 +113,7 @@ function getPropertyType(
109113
return {
110114
name,
111115
optional,
112-
typeAnnotation: extractArrayElementType(typeAnnotation, name),
116+
typeAnnotation: extractArrayElementType(typeAnnotation, name, parser),
113117
};
114118
default:
115119
throw new Error(`Unable to determine event type for "${name}": ${type}`);
@@ -119,6 +123,7 @@ function getPropertyType(
119123
function extractArrayElementType(
120124
typeAnnotation: $FlowFixMe,
121125
name: string,
126+
parser: Parser,
122127
): EventTypeAnnotation {
123128
const type = extractTypeFromTypeAnnotation(typeAnnotation);
124129

@@ -146,12 +151,18 @@ function extractArrayElementType(
146151
case 'ObjectTypeAnnotation':
147152
return {
148153
type: 'ObjectTypeAnnotation',
149-
properties: typeAnnotation.properties.map(buildPropertiesForEvent),
154+
properties: typeAnnotation.properties.map(member =>
155+
buildPropertiesForEvent(member, parser),
156+
),
150157
};
151158
case 'ArrayTypeAnnotation':
152159
return {
153160
type: 'ArrayTypeAnnotation',
154-
elementType: extractArrayElementType(typeAnnotation.elementType, name),
161+
elementType: extractArrayElementType(
162+
typeAnnotation.elementType,
163+
name,
164+
parser,
165+
),
155166
};
156167
case '$ReadOnlyArray':
157168
const genericParams = typeAnnotation.typeParameters.params;
@@ -164,7 +175,7 @@ function extractArrayElementType(
164175
}
165176
return {
166177
type: 'ArrayTypeAnnotation',
167-
elementType: extractArrayElementType(genericParams[0], name),
178+
elementType: extractArrayElementType(genericParams[0], name, parser),
168179
};
169180
default:
170181
throw new Error(
@@ -244,18 +255,20 @@ function findEventArgumentsAndType(
244255
}
245256
}
246257

247-
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
248-
* LTI update could not be added via codemod */
249-
function buildPropertiesForEvent(property): NamedShape<EventTypeAnnotation> {
258+
function buildPropertiesForEvent(
259+
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
260+
* LTI update could not be added via codemod */
261+
property,
262+
parser: Parser,
263+
): NamedShape<EventTypeAnnotation> {
250264
const name = property.key.name;
251-
const optional =
252-
property.value.type === 'NullableTypeAnnotation' || property.optional;
265+
const optional = parser.isOptionalProperty(property);
253266
let typeAnnotation =
254267
property.value.type === 'NullableTypeAnnotation'
255268
? property.value.typeAnnotation
256269
: property.value;
257270

258-
return getPropertyType(name, optional, typeAnnotation);
271+
return getPropertyType(name, optional, typeAnnotation, parser);
259272
}
260273

261274
function buildEventSchema(
@@ -299,7 +312,11 @@ function buildEventSchema(
299312
paperTopLevelNameDeprecated,
300313
typeAnnotation: {
301314
type: 'EventTypeAnnotation',
302-
argument: getEventArgument(argumentProps, buildPropertiesForEvent),
315+
argument: getEventArgument(
316+
argumentProps,
317+
buildPropertiesForEvent,
318+
parser,
319+
),
303320
},
304321
};
305322
}
@@ -316,7 +333,11 @@ function buildEventSchema(
316333
bubblingType,
317334
typeAnnotation: {
318335
type: 'EventTypeAnnotation',
319-
argument: getEventArgument(argumentProps, buildPropertiesForEvent),
336+
argument: getEventArgument(
337+
argumentProps,
338+
buildPropertiesForEvent,
339+
parser,
340+
),
320341
},
321342
};
322343
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,12 @@ class FlowParser implements Parser {
337337
nameForArgument(prop: PropAST): $FlowFixMe {
338338
return prop.argument.id.name;
339339
}
340+
341+
isOptionalProperty(property: $FlowFixMe): boolean {
342+
return (
343+
property.value.type === 'NullableTypeAnnotation' || property.optional
344+
);
345+
}
340346
}
341347

342348
module.exports = {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,11 @@ export interface Parser {
269269
* @returns: name property
270270
*/
271271
nameForArgument(prop: PropAST): $FlowFixMe;
272+
273+
/**
274+
* Given a property return if it is optional.
275+
* @parameter property
276+
* @returns: a boolean specifying if the Property is optional
277+
*/
278+
isOptionalProperty(property: $FlowFixMe): boolean;
272279
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,8 @@ export class MockedParser implements Parser {
251251
nameForArgument(prop: PropAST): $FlowFixMe {
252252
return prop.expression.name;
253253
}
254+
255+
isOptionalProperty(property: $FlowFixMe): boolean {
256+
return property.optional || false;
257+
}
254258
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,11 +860,15 @@ function getEventArgument(
860860
argumentProps: PropAST,
861861
buildPropertiesForEvent: (
862862
property: PropAST,
863+
parser: Parser,
863864
) => NamedShape<EventTypeAnnotation>,
865+
parser: Parser,
864866
): ObjectTypeAnnotation<EventTypeAnnotation> {
865867
return {
866868
type: 'ObjectTypeAnnotation',
867-
properties: argumentProps.map(buildPropertiesForEvent),
869+
properties: argumentProps.map(member =>
870+
buildPropertiesForEvent(member, parser),
871+
),
868872
};
869873
}
870874

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

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function getPropertyType(
3232
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
3333
* LTI update could not be added via codemod */
3434
annotation,
35+
parser: Parser,
3536
): NamedShape<EventTypeAnnotation> {
3637
const topLevelType = parseTopLevelType(annotation);
3738
const typeAnnotation = topLevelType.type;
@@ -88,7 +89,9 @@ function getPropertyType(
8889
optional,
8990
typeAnnotation: {
9091
type: 'ObjectTypeAnnotation',
91-
properties: typeAnnotation.members.map(buildPropertiesForEvent),
92+
properties: typeAnnotation.members.map(member =>
93+
buildPropertiesForEvent(member, parser),
94+
),
9295
},
9396
};
9497
case 'TSUnionType':
@@ -112,7 +115,7 @@ function getPropertyType(
112115
return {
113116
name,
114117
optional,
115-
typeAnnotation: extractArrayElementType(typeAnnotation, name),
118+
typeAnnotation: extractArrayElementType(typeAnnotation, name, parser),
116119
};
117120
default:
118121
(type: empty);
@@ -123,12 +126,17 @@ function getPropertyType(
123126
function extractArrayElementType(
124127
typeAnnotation: $FlowFixMe,
125128
name: string,
129+
parser: Parser,
126130
): EventTypeAnnotation {
127131
const type = extractTypeFromTypeAnnotation(typeAnnotation);
128132

129133
switch (type) {
130134
case 'TSParenthesizedType':
131-
return extractArrayElementType(typeAnnotation.typeAnnotation, name);
135+
return extractArrayElementType(
136+
typeAnnotation.typeAnnotation,
137+
name,
138+
parser,
139+
);
132140
case 'TSBooleanKeyword':
133141
return {type: 'BooleanTypeAnnotation'};
134142
case 'TSStringKeyword':
@@ -154,12 +162,18 @@ function extractArrayElementType(
154162
case 'TSTypeLiteral':
155163
return {
156164
type: 'ObjectTypeAnnotation',
157-
properties: typeAnnotation.members.map(buildPropertiesForEvent),
165+
properties: typeAnnotation.members.map(member =>
166+
buildPropertiesForEvent(member, parser),
167+
),
158168
};
159169
case 'TSArrayType':
160170
return {
161171
type: 'ArrayTypeAnnotation',
162-
elementType: extractArrayElementType(typeAnnotation.elementType, name),
172+
elementType: extractArrayElementType(
173+
typeAnnotation.elementType,
174+
name,
175+
parser,
176+
),
163177
};
164178
default:
165179
throw new Error(
@@ -260,14 +274,17 @@ function findEventArgumentsAndType(
260274
}
261275
}
262276

263-
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
264-
* LTI update could not be added via codemod */
265-
function buildPropertiesForEvent(property): NamedShape<EventTypeAnnotation> {
277+
function buildPropertiesForEvent(
278+
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
279+
* LTI update could not be added via codemod */
280+
property,
281+
parser: Parser,
282+
): NamedShape<EventTypeAnnotation> {
266283
const name = property.key.name;
267-
const optional = property.optional || false;
284+
const optional = parser.isOptionalProperty(property);
268285
let typeAnnotation = property.typeAnnotation.typeAnnotation;
269286

270-
return getPropertyType(name, optional, typeAnnotation);
287+
return getPropertyType(name, optional, typeAnnotation, parser);
271288
}
272289

273290
// $FlowFixMe[unclear-type] TODO(T108222691): Use flow-types for @babel/parser
@@ -303,7 +320,11 @@ function buildEventSchema(
303320
paperTopLevelNameDeprecated,
304321
typeAnnotation: {
305322
type: 'EventTypeAnnotation',
306-
argument: getEventArgument(argumentProps, buildPropertiesForEvent),
323+
argument: getEventArgument(
324+
argumentProps,
325+
buildPropertiesForEvent,
326+
parser,
327+
),
307328
},
308329
};
309330
}
@@ -314,7 +335,11 @@ function buildEventSchema(
314335
bubblingType,
315336
typeAnnotation: {
316337
type: 'EventTypeAnnotation',
317-
argument: getEventArgument(argumentProps, buildPropertiesForEvent),
338+
argument: getEventArgument(
339+
argumentProps,
340+
buildPropertiesForEvent,
341+
parser,
342+
),
318343
},
319344
};
320345
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,10 @@ class TypeScriptParser implements Parser {
336336
nameForArgument(prop: PropAST): $FlowFixMe {
337337
return prop.expression.name;
338338
}
339+
340+
isOptionalProperty(property: $FlowFixMe): boolean {
341+
return property.optional || false;
342+
}
339343
}
340344

341345
module.exports = {

0 commit comments

Comments
 (0)