Skip to content

Commit 663a018

Browse files
MaeIgfacebook-github-bot
authored andcommitted
Extract getTypeAnnotationFromProperty from buildPropertiesForEvent into specific parsers (#37573)
Summary: This PR aims to remove the duplicated logic in [flow|typescript]/components/events.js files to move it in specific parsers. It is a task of #34872: > [Codegen 114 - Assigned to MaeIg] Add a function getTypeAnnotationFromProperty(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#L174-L177) and parsers/typescript/components/events.js. Use the parsers in the buildPropertiesForEvent. ## Changelog: [Internal] [Changed] - Extract getTypeAnnotationFromProperty from buildPropertiesForEvent into specific parsers <!-- 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: #37573 Test Plan: Flow: <img width="600" alt="image" src="https://github.com/facebook/react-native/assets/40902940/554bb82d-b492-4550-9a84-254fc4f78285"> Eslint: <img width="498" alt="image" src="https://github.com/facebook/react-native/assets/40902940/53a302b7-c2aa-4008-9583-2e3b4cddc14c"> Jest: <img width="395" alt="image" src="https://github.com/facebook/react-native/assets/40902940/c7ff53f1-2be1-4099-b2e6-081128cf5333"> Reviewed By: dmytrorykun Differential Revision: D46190831 Pulled By: cipolleschi fbshipit-source-id: 393a4c4968139ee7061ed4ea524d083af6950e38
1 parent 8ffaede commit 663a018

File tree

7 files changed

+82
-5
lines changed

7 files changed

+82
-5
lines changed

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,41 @@ describe('FlowParser', () => {
306306
});
307307
});
308308

309+
describe('getTypeAnnotationFromProperty', () => {
310+
describe('when property value type is NullableTypeAnnotation', () => {
311+
it('returns typeAnnotation of the value', () => {
312+
const typeAnnotation = {
313+
type: 'StringTypeAnnotation',
314+
};
315+
316+
const property = {
317+
value: {
318+
type: 'NullableTypeAnnotation',
319+
typeAnnotation: typeAnnotation,
320+
},
321+
};
322+
323+
expect(parser.getTypeAnnotationFromProperty(property)).toEqual(
324+
typeAnnotation,
325+
);
326+
});
327+
});
328+
329+
describe('when property value type is not NullableTypeAnnotation', () => {
330+
it('returns the value', () => {
331+
const value = {
332+
type: 'StringTypeAnnotation',
333+
};
334+
335+
const property = {
336+
value: value,
337+
};
338+
339+
expect(parser.getTypeAnnotationFromProperty(property)).toEqual(value);
340+
});
341+
});
342+
});
343+
309344
describe('typeAlias', () => {
310345
it('returns typeAlias Property', () => {
311346
expect(parser.typeAlias).toEqual('TypeAlias');
@@ -601,6 +636,30 @@ describe('TypeScriptParser', () => {
601636
});
602637
});
603638

639+
describe('getTypeAnnotationFromProperty', () => {
640+
it('returns the type annotation', () => {
641+
const typeAnnotation = {
642+
type: 'TSStringKeyword',
643+
key: {
644+
type: 'Identifier',
645+
name: 'b',
646+
},
647+
members: [],
648+
};
649+
650+
const property = {
651+
typeAnnotation: {
652+
type: 'TSTypeAnnotation',
653+
typeAnnotation: typeAnnotation,
654+
},
655+
};
656+
657+
expect(parser.getTypeAnnotationFromProperty(property)).toEqual(
658+
typeAnnotation,
659+
);
660+
});
661+
});
662+
604663
describe('typeAlias', () => {
605664
it('returns typeAlias Property', () => {
606665
expect(parser.typeAlias).toEqual('TSTypeAliasDeclaration');

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,7 @@ function buildPropertiesForEvent(
242242
): NamedShape<EventTypeAnnotation> {
243243
const name = property.key.name;
244244
const optional = parser.isOptionalProperty(property);
245-
let typeAnnotation =
246-
property.value.type === 'NullableTypeAnnotation'
247-
? property.value.typeAnnotation
248-
: property.value;
245+
const typeAnnotation = parser.getTypeAnnotationFromProperty(property);
249246

250247
return getPropertyType(name, optional, typeAnnotation, parser);
251248
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,12 @@ class FlowParser implements Parser {
380380
return getSchemaInfo;
381381
}
382382

383+
getTypeAnnotationFromProperty(property: PropAST): $FlowFixMe {
384+
return property.value.type === 'NullableTypeAnnotation'
385+
? property.value.typeAnnotation
386+
: property.value;
387+
}
388+
383389
getGetTypeAnnotationFN(): GetTypeAnnotationFN {
384390
return getTypeAnnotation;
385391
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,13 @@ export interface Parser {
357357

358358
getGetSchemaInfoFN(): GetSchemaInfoFN;
359359

360+
/**
361+
* Given a property return the type annotation.
362+
* @parameter property
363+
* @returns: the annotation for a type in the AST.
364+
*/
365+
getTypeAnnotationFromProperty(property: PropAST): $FlowFixMe;
366+
360367
getResolvedTypeAnnotation(
361368
typeAnnotation: $FlowFixMe,
362369
types: TypeDeclarationMap,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ export class MockedParser implements Parser {
282282
return property.optional || false;
283283
}
284284

285+
getTypeAnnotationFromProperty(property: PropAST): $FlowFixMe {
286+
return property.typeAnnotation.typeAnnotation;
287+
}
288+
285289
getGetTypeAnnotationFN(): GetTypeAnnotationFN {
286290
return () => {
287291
return {};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ function buildPropertiesForEvent(
260260
): NamedShape<EventTypeAnnotation> {
261261
const name = property.key.name;
262262
const optional = parser.isOptionalProperty(property);
263-
let typeAnnotation = property.typeAnnotation.typeAnnotation;
263+
const typeAnnotation = parser.getTypeAnnotationFromProperty(property);
264264

265265
return getPropertyType(name, optional, typeAnnotation, parser);
266266
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,10 @@ class TypeScriptParser implements Parser {
372372
return getSchemaInfo;
373373
}
374374

375+
getTypeAnnotationFromProperty(property: PropAST): $FlowFixMe {
376+
return property.typeAnnotation.typeAnnotation;
377+
}
378+
375379
getGetTypeAnnotationFN(): GetTypeAnnotationFN {
376380
return getTypeAnnotation;
377381
}

0 commit comments

Comments
 (0)