Skip to content

Commit

Permalink
Parser create getTypeAnnotationName(typeAnnotation) (#37580)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cloudpresser authored and facebook-github-bot committed Aug 4, 2023
1 parent c27e9e6 commit e22d1a1
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@ describe('FlowParser', () => {
});
});

describe('getTypeAnnotationName', () => {
it('returns type annotation name', () => {
const typeAnnotation = {
id: {
name: 'StringTypeAnnotation',
},
};

expect(parser.getTypeAnnotationName(typeAnnotation)).toBe(
'StringTypeAnnotation',
);
});
});

describe('computePartialProperties', () => {
it('returns partial properties', () => {
const properties = [
Expand Down Expand Up @@ -551,6 +565,19 @@ describe('TypeScriptParser', () => {
});
});

describe('getTypeAnnotationName', () => {
it('returns type annotation name', () => {
const typeAnnotation = {
type: 'TSTypeReference',
typeName: {
name: 'Foo',
},
};

expect(parser.getTypeAnnotationName(typeAnnotation)).toEqual('Foo');
});
});

describe('computePartialProperties', () => {
it('returns partial properties', () => {
const properties = [
Expand Down
12 changes: 3 additions & 9 deletions packages/react-native-codegen/src/parsers/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ class UnsupportedGenericParserError extends ParserError {
genericTypeAnnotation: $FlowFixMe,
parser: Parser,
) {
const genericName = parser.nameForGenericTypeAnnotation(
genericTypeAnnotation,
);
const genericName = parser.getTypeAnnotationName(genericTypeAnnotation);
super(
nativeModuleName,
genericTypeAnnotation,
Expand All @@ -136,9 +134,7 @@ class MissingTypeParameterGenericParserError extends ParserError {
genericTypeAnnotation: $FlowFixMe,
parser: Parser,
) {
const genericName = parser.nameForGenericTypeAnnotation(
genericTypeAnnotation,
);
const genericName = parser.getTypeAnnotationName(genericTypeAnnotation);

super(
nativeModuleName,
Expand All @@ -154,9 +150,7 @@ class MoreThanOneTypeParameterGenericParserError extends ParserError {
genericTypeAnnotation: $FlowFixMe,
parser: Parser,
) {
const genericName = parser.nameForGenericTypeAnnotation(
genericTypeAnnotation,
);
const genericName = parser.getTypeAnnotationName(genericTypeAnnotation);

super(
nativeModuleName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function getTypeAnnotationForArray<+T>(

if (
extractedTypeAnnotation.type === 'GenericTypeAnnotation' &&
extractedTypeAnnotation.id.name === 'WithDefault'
parser.getTypeAnnotationName(extractedTypeAnnotation) === 'WithDefault'
) {
throw new Error(
'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>"',
Expand Down Expand Up @@ -82,7 +82,7 @@ function getTypeAnnotationForArray<+T>(

const type =
extractedTypeAnnotation.type === 'GenericTypeAnnotation'
? extractedTypeAnnotation.id.name
? parser.getTypeAnnotationName(extractedTypeAnnotation)
: extractedTypeAnnotation.type;

switch (type) {
Expand Down Expand Up @@ -170,7 +170,6 @@ function getTypeAnnotationForArray<+T>(
);
}
default:
(type: empty);
throw new Error(`Unknown property type for "${name}": ${type}`);
}
}
Expand Down Expand Up @@ -220,7 +219,7 @@ function getTypeAnnotation<+T>(

if (
typeAnnotation.type === 'GenericTypeAnnotation' &&
typeAnnotation.id.name === '$ReadOnlyArray'
parser.getTypeAnnotationName(typeAnnotation) === '$ReadOnlyArray'
) {
return {
type: 'ArrayTypeAnnotation',
Expand All @@ -237,7 +236,7 @@ function getTypeAnnotation<+T>(

if (
typeAnnotation.type === 'GenericTypeAnnotation' &&
typeAnnotation.id.name === '$ReadOnly'
parser.getTypeAnnotationName(typeAnnotation) === '$ReadOnly'
) {
return {
type: 'ObjectTypeAnnotation',
Expand All @@ -253,7 +252,7 @@ function getTypeAnnotation<+T>(

const type =
typeAnnotation.type === 'GenericTypeAnnotation'
? typeAnnotation.id.name
? parser.getTypeAnnotationName(typeAnnotation)
: typeAnnotation.type;

switch (type) {
Expand Down Expand Up @@ -379,7 +378,6 @@ function getTypeAnnotation<+T>(
type: 'MixedTypeAnnotation',
};
default:
(type: empty);
throw new Error(
`Unknown property type for "${name}": "${type}" in the State`,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function getPropertyType(
typeAnnotation: $FlowFixMe,
parser: Parser,
): NamedShape<EventTypeAnnotation> {
const type = parser.extractTypeFromTypeAnnotation(typeAnnotation);
const type = extractTypeFromTypeAnnotation(typeAnnotation, parser);

switch (type) {
case 'BooleanTypeAnnotation':
Expand Down Expand Up @@ -94,7 +94,7 @@ function extractArrayElementType(
name: string,
parser: Parser,
): EventTypeAnnotation {
const type = parser.extractTypeFromTypeAnnotation(typeAnnotation);
const type = extractTypeFromTypeAnnotation(typeAnnotation, parser);

switch (type) {
case 'BooleanTypeAnnotation':
Expand Down Expand Up @@ -163,6 +163,15 @@ function prettify(jsonObject: $FlowFixMe): string {
return JSON.stringify(jsonObject, null, 2);
}

function extractTypeFromTypeAnnotation(
typeAnnotation: $FlowFixMe,
parser: Parser,
): string {
return typeAnnotation.type === 'GenericTypeAnnotation'
? parser.getTypeAnnotationName(typeAnnotation)
: typeAnnotation.type;
}

function findEventArgumentsAndType(
parser: Parser,
typeAnnotation: $FlowFixMe,
Expand All @@ -175,7 +184,7 @@ function findEventArgumentsAndType(
paperTopLevelNameDeprecated: ?$FlowFixMe,
} {
throwIfEventHasNoName(typeAnnotation, parser);
const name = typeAnnotation.id.name;
const name = parser.getTypeAnnotationName(typeAnnotation);
if (name === '$ReadOnly') {
return {
argumentProps: typeAnnotation.typeParameters.params[0].properties,
Expand Down Expand Up @@ -236,8 +245,8 @@ function buildEventSchema(

if (
typeAnnotation.type !== 'GenericTypeAnnotation' ||
(typeAnnotation.id.name !== 'BubblingEventHandler' &&
typeAnnotation.id.name !== 'DirectEventHandler')
(parser.getTypeAnnotationName(typeAnnotation) !== 'BubblingEventHandler' &&
parser.getTypeAnnotationName(typeAnnotation) !== 'DirectEventHandler')
) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function translateTypeAnnotation(

switch (typeAnnotation.type) {
case 'GenericTypeAnnotation': {
switch (typeAnnotation.id.name) {
switch (parser.getTypeAnnotationName(typeAnnotation)) {
case 'RootTag': {
return emitRootTag(nullable);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native-codegen/src/parsers/flow/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class FlowParser implements Parser {
return 'Flow';
}

nameForGenericTypeAnnotation(typeAnnotation: $FlowFixMe): string {
getTypeAnnotationName(typeAnnotation: $FlowFixMe): string {
return typeAnnotation?.id?.name;
}

Expand Down Expand Up @@ -421,7 +421,7 @@ class FlowParser implements Parser {
break;
}

const typeAnnotationName = this.nameForGenericTypeAnnotation(node);
const typeAnnotationName = this.getTypeAnnotationName(node);
const resolvedTypeAnnotation = types[typeAnnotationName];
if (resolvedTypeAnnotation == null) {
break;
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native-codegen/src/parsers/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ export interface Parser {
*/
language(): ParserType;
/**
* Given a type annotation for a generic type, it returns the type name.
* Given a type annotation, it returns the type name.
* @parameter typeAnnotation: the annotation for a type in the AST.
* @returns: the name of the type.
*/
nameForGenericTypeAnnotation(typeAnnotation: $FlowFixMe): string;
getTypeAnnotationName(typeAnnotation: $FlowFixMe): string;
/**
* Given a type arguments, it returns a boolean specifying if the Module is Invalid.
* @parameter typeArguments: the type arguments.
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native-codegen/src/parsers/parserMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ export class MockedParser implements Parser {
return 'Flow';
}

nameForGenericTypeAnnotation(typeAnnotation: $FlowFixMe): string {
return typeAnnotation.id.name;
getTypeAnnotationName(typeAnnotation: $FlowFixMe): string {
return typeAnnotation?.id?.name;
}

checkIfInvalidModule(typeArguments: $FlowFixMe): boolean {
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native-codegen/src/parsers/parsers-commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ function getCommandTypeNameAndOptionsExpression(
}

return {
commandTypeName: parser.nameForGenericTypeAnnotation(typeArgumentParam),
commandTypeName: parser.getTypeAnnotationName(typeArgumentParam),
commandOptionsExpression: callExpression.arguments[0],
};
}
Expand Down Expand Up @@ -998,7 +998,7 @@ function getTypeResolutionStatus(
return {
successful: true,
type,
name: parser.nameForGenericTypeAnnotation(typeAnnotation),
name: parser.getTypeAnnotationName(typeAnnotation),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ function emitCommonTypes(
}

const genericTypeAnnotationName =
parser.nameForGenericTypeAnnotation(typeAnnotation);
parser.getTypeAnnotationName(typeAnnotation);

const emitter = typeMap[genericTypeAnnotationName];
if (!emitter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ function detectArrayType<T>(
// Covers: Array<T> and ReadonlyArray<T>
if (
typeAnnotation.type === 'TSTypeReference' &&
(typeAnnotation.typeName.name === 'ReadonlyArray' ||
typeAnnotation.typeName.name === 'Array')
(parser.getTypeAnnotationName(typeAnnotation) === 'ReadonlyArray' ||
parser.getTypeAnnotationName(typeAnnotation) === 'Array')
) {
return {
type: 'ArrayTypeAnnotation',
Expand Down Expand Up @@ -378,7 +378,7 @@ function getTypeAnnotation<T>(
const type =
typeAnnotation.type === 'TSTypeReference' ||
typeAnnotation.type === 'TSTypeAliasDeclaration'
? typeAnnotation.typeName.name
? parser.getTypeAnnotationName(typeAnnotation)
: typeAnnotation.type;

const common = getCommonTypeAnnotation(
Expand Down Expand Up @@ -414,7 +414,6 @@ function getTypeAnnotation<T>(
`Cannot use "${type}" type annotation for "${name}": must use a specific function type like BubblingEventHandler, or DirectEventHandler`,
);
default:
(type: empty);
throw new Error(`Unknown prop type for "${name}": "${type}"`);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ function getPropertyType(
const topLevelType = parseTopLevelType(annotation);
const typeAnnotation = topLevelType.type;
const optional = optionalProperty || topLevelType.optional;
const type = parser.extractTypeFromTypeAnnotation(typeAnnotation);
const type =
typeAnnotation.type === 'TSTypeReference'
? parser.getTypeAnnotationName(typeAnnotation)
: typeAnnotation.type;

switch (type) {
case 'TSBooleanKeyword':
Expand Down Expand Up @@ -92,7 +95,7 @@ function extractArrayElementType(
name: string,
parser: Parser,
): EventTypeAnnotation {
const type = parser.extractTypeFromTypeAnnotation(typeAnnotation);
const type = extractTypeFromTypeAnnotation(typeAnnotation, parser);

switch (type) {
case 'TSParenthesizedType':
Expand Down Expand Up @@ -154,6 +157,15 @@ function extractArrayElementType(
}
}

function extractTypeFromTypeAnnotation(
typeAnnotation: $FlowFixMe,
parser: Parser,
): string {
return typeAnnotation.type === 'TSTypeReference'
? parser.getTypeAnnotationName(typeAnnotation)
: typeAnnotation.type;
}

function findEventArgumentsAndType(
parser: Parser,
typeAnnotation: $FlowFixMe,
Expand Down Expand Up @@ -182,7 +194,7 @@ function findEventArgumentsAndType(
}
throwIfEventHasNoName(typeAnnotation, parser);
const name = typeAnnotation.typeName.name;
const name = parser.getTypeAnnotationName(typeAnnotation);
if (name === 'Readonly') {
return findEventArgumentsAndType(
parser,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ function translateTypeAnnotation(
}
case 'TSTypeReference': {
return translateTypeReferenceAnnotation(
typeAnnotation.typeName.name,
parser.getTypeAnnotationName(typeAnnotation),
nullable,
typeAnnotation,
hasteModuleName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class TypeScriptParser implements Parser {
return 'TypeScript';
}

nameForGenericTypeAnnotation(typeAnnotation: $FlowFixMe): string {
getTypeAnnotationName(typeAnnotation: $FlowFixMe): string {
return typeAnnotation?.typeName?.name;
}

Expand Down Expand Up @@ -413,7 +413,7 @@ class TypeScriptParser implements Parser {
break;
}

const typeAnnotationName = this.nameForGenericTypeAnnotation(node);
const typeAnnotationName = this.getTypeAnnotationName(node);
const resolvedTypeAnnotation = types[typeAnnotationName];
if (resolvedTypeAnnotation == null) {
break;
Expand Down Expand Up @@ -447,7 +447,7 @@ class TypeScriptParser implements Parser {
return false;
}
const eventNames = new Set(['BubblingEventHandler', 'DirectEventHandler']);
return eventNames.has(typeAnnotation.typeName.name);
return eventNames.has(this.getTypeAnnotationName(typeAnnotation));
}

isProp(name: string, typeAnnotation: $FlowFixMe): boolean {
Expand All @@ -457,7 +457,7 @@ class TypeScriptParser implements Parser {
const isStyle =
name === 'style' &&
typeAnnotation.type === 'GenericTypeAnnotation' &&
typeAnnotation.typeName.name === 'ViewStyleProp';
this.getTypeAnnotationName(typeAnnotation) === 'ViewStyleProp';
return !isStyle;
}

Expand Down

0 comments on commit e22d1a1

Please sign in to comment.