Skip to content

Commit 67eb494

Browse files
Randall71facebook-github-bot
authored andcommitted
(codegen 133) - Parser extractTypeFromTypeAnnotation (#37752)
Summary: > [Codegen 133 - Assigned to Randall71] Create an extractTypeFromTypeAnnotation(typeAnnotation) function 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#L193-L197) and [this code for TypeScript](https://github.com/facebook/react-native/blob/d8ced6f8953cd896471983714e722caf50783960/packages/react-native-codegen/src/parsers/typescript/components/events.js#L189-L193). Replace the invocation of that function with the one from the parser This is part of #34872 ## Changelog: <!-- 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: #37752 Test Plan: yarn jest packages/react-native-codegen > new tests written, as well as coverage from existing tests Reviewed By: cipolleschi Differential Revision: D46520245 Pulled By: rshest fbshipit-source-id: 8c5d8ca8b46fb44399089c6b694998874463ecc8
1 parent 942bd61 commit 67eb494

File tree

7 files changed

+78
-15
lines changed

7 files changed

+78
-15
lines changed

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,31 @@ describe('FlowParser', () => {
358358
expect(parser.interfaceDeclaration).toEqual('InterfaceDeclaration');
359359
});
360360
});
361+
362+
describe('extractTypeFromTypeAnnotation', () => {
363+
it('should return the name if typeAnnotation is GenericTypeAnnotation', () => {
364+
const typeAnnotation = {
365+
type: 'GenericTypeAnnotation',
366+
id: {
367+
name: 'SomeType',
368+
},
369+
};
370+
371+
expect(parser.extractTypeFromTypeAnnotation(typeAnnotation)).toEqual(
372+
'SomeType',
373+
);
374+
});
375+
376+
it('should return the type if typeAnnotation is not GenericTypeAnnotation', () => {
377+
const typeAnnotation = {
378+
type: 'SomeOtherType',
379+
};
380+
381+
expect(parser.extractTypeFromTypeAnnotation(typeAnnotation)).toEqual(
382+
'SomeOtherType',
383+
);
384+
});
385+
});
361386
});
362387

363388
describe('TypeScriptParser', () => {
@@ -677,4 +702,29 @@ describe('TypeScriptParser', () => {
677702
expect(parser.interfaceDeclaration).toEqual('TSInterfaceDeclaration');
678703
});
679704
});
705+
706+
describe('extractTypeFromTypeAnnotation', () => {
707+
it('should return the name if typeAnnotation is TSTypeReference', () => {
708+
const typeAnnotation = {
709+
type: 'TSTypeReference',
710+
typeName: {
711+
name: 'SomeType',
712+
},
713+
};
714+
715+
expect(parser.extractTypeFromTypeAnnotation(typeAnnotation)).toEqual(
716+
'SomeType',
717+
);
718+
});
719+
720+
it('should return the type if typeAnnotation is not TSTypeReference', () => {
721+
const typeAnnotation = {
722+
type: 'SomeOtherType',
723+
};
724+
725+
expect(parser.extractTypeFromTypeAnnotation(typeAnnotation)).toEqual(
726+
'SomeOtherType',
727+
);
728+
});
729+
});
680730
});

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function getPropertyType(
4343
typeAnnotation: $FlowFixMe,
4444
parser: Parser,
4545
): NamedShape<EventTypeAnnotation> {
46-
const type = extractTypeFromTypeAnnotation(typeAnnotation);
46+
const type = parser.extractTypeFromTypeAnnotation(typeAnnotation);
4747

4848
switch (type) {
4949
case 'BooleanTypeAnnotation':
@@ -102,7 +102,7 @@ function extractArrayElementType(
102102
name: string,
103103
parser: Parser,
104104
): EventTypeAnnotation {
105-
const type = extractTypeFromTypeAnnotation(typeAnnotation);
105+
const type = parser.extractTypeFromTypeAnnotation(typeAnnotation);
106106

107107
switch (type) {
108108
case 'BooleanTypeAnnotation':
@@ -167,12 +167,6 @@ function prettify(jsonObject: $FlowFixMe): string {
167167
return JSON.stringify(jsonObject, null, 2);
168168
}
169169

170-
function extractTypeFromTypeAnnotation(typeAnnotation: $FlowFixMe): string {
171-
return typeAnnotation.type === 'GenericTypeAnnotation'
172-
? typeAnnotation.id.name
173-
: typeAnnotation.type;
174-
}
175-
176170
function findEventArgumentsAndType(
177171
parser: Parser,
178172
typeAnnotation: $FlowFixMe,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,12 @@ class FlowParser implements Parser {
533533
genericTypeAnnotationErrorMessage(typeAnnotation: $FlowFixMe): string {
534534
return `A non GenericTypeAnnotation must be a type declaration ('${this.typeAlias}') or enum ('${this.enumDeclaration}'). Instead, got the unsupported ${typeAnnotation.type}.`;
535535
}
536+
537+
extractTypeFromTypeAnnotation(typeAnnotation: $FlowFixMe): string {
538+
return typeAnnotation.type === 'GenericTypeAnnotation'
539+
? typeAnnotation.id.name
540+
: typeAnnotation.type;
541+
}
536542
}
537543

538544
module.exports = {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,4 +400,11 @@ export interface Parser {
400400
* Given a unsupported typeAnnotation, returns an error message.
401401
*/
402402
genericTypeAnnotationErrorMessage(typeAnnotation: $FlowFixMe): string;
403+
404+
/**
405+
* Given a type annotation, it extracts the type.
406+
* @parameter typeAnnotation: the annotation for a type in the AST.
407+
* @returns: the extracted type.
408+
*/
409+
extractTypeFromTypeAnnotation(typeAnnotation: $FlowFixMe): string;
403410
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,4 +472,10 @@ export class MockedParser implements Parser {
472472
genericTypeAnnotationErrorMessage(typeAnnotation: $FlowFixMe): string {
473473
return `A non GenericTypeAnnotation must be a type declaration ('${this.typeAlias}') or enum ('${this.enumDeclaration}'). Instead, got the unsupported ${typeAnnotation.type}.`;
474474
}
475+
476+
extractTypeFromTypeAnnotation(typeAnnotation: $FlowFixMe): string {
477+
return typeAnnotation.type === 'GenericTypeAnnotation'
478+
? typeAnnotation.id.name
479+
: typeAnnotation.type;
480+
}
475481
}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function extractArrayElementType(
104104
name: string,
105105
parser: Parser,
106106
): EventTypeAnnotation {
107-
const type = extractTypeFromTypeAnnotation(typeAnnotation);
107+
const type = parser.extractTypeFromTypeAnnotation(typeAnnotation);
108108

109109
switch (type) {
110110
case 'TSParenthesizedType':
@@ -162,12 +162,6 @@ function extractArrayElementType(
162162
}
163163
}
164164

165-
function extractTypeFromTypeAnnotation(typeAnnotation: $FlowFixMe): string {
166-
return typeAnnotation.type === 'TSTypeReference'
167-
? typeAnnotation.typeName.name
168-
: typeAnnotation.type;
169-
}
170-
171165
function findEventArgumentsAndType(
172166
parser: Parser,
173167
typeAnnotation: $FlowFixMe,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,12 @@ class TypeScriptParser implements Parser {
547547
genericTypeAnnotationErrorMessage(typeAnnotation: $FlowFixMe): string {
548548
return `A non GenericTypeAnnotation must be a type declaration ('${this.typeAlias}'), an interface ('${this.interfaceDeclaration}'), or enum ('${this.enumDeclaration}'). Instead, got the unsupported ${typeAnnotation.type}.`;
549549
}
550+
551+
extractTypeFromTypeAnnotation(typeAnnotation: $FlowFixMe): string {
552+
return typeAnnotation.type === 'TSTypeReference'
553+
? typeAnnotation.typeName.name
554+
: typeAnnotation.type;
555+
}
550556
}
551557

552558
module.exports = {

0 commit comments

Comments
 (0)