Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser create getTypeAnnotationName(typeAnnotation) #37580

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -488,6 +502,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 @@ -31,7 +31,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 @@ -81,7 +81,7 @@ function getTypeAnnotationForArray<+T>(

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

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

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

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

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

switch (type) {
Expand Down Expand Up @@ -389,7 +388,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 @@ -43,7 +43,8 @@ function getPropertyType(
typeAnnotation: $FlowFixMe,
parser: Parser,
): NamedShape<EventTypeAnnotation> {
const type = parser.extractTypeFromTypeAnnotation(typeAnnotation);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 🚫 packages/react-native-codegen/src/parsers/flow/components/events.js line 46 – Delete (prettier/prettier)

const type = extractTypeFromTypeAnnotation(typeAnnotation, parser);

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 🚫 packages/react-native-codegen/src/parsers/flow/components/events.js line 106 – Replace ⏎··const·type·=·extractTypeFromTypeAnnotation(typeAnnotation,·parser);⏎ with ··const·type·=·extractTypeFromTypeAnnotation(typeAnnotation,·parser); (prettier/prettier)

const type = extractTypeFromTypeAnnotation(typeAnnotation, parser);


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 🚫 packages/react-native-codegen/src/parsers/flow/components/events.js line 171 – Delete (prettier/prettier)



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

function findEventArgumentsAndType(
parser: Parser,
typeAnnotation: $FlowFixMe,
Expand All @@ -179,7 +192,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 @@ -242,8 +255,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
2 changes: 1 addition & 1 deletion 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
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
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
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 @@ -120,8 +120,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 @@ -377,7 +377,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 @@ -413,7 +413,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,7 @@ function getPropertyType(
const optional = optionalProperty || topLevelType.optional;
const type =
typeAnnotation.type === 'TSTypeReference'
? typeAnnotation.typeName.name
? parser.getTypeAnnotationName(typeAnnotation)
: typeAnnotation.type;

switch (type) {
Expand Down Expand Up @@ -94,7 +94,6 @@ function getPropertyType(
typeAnnotation: extractArrayElementType(typeAnnotation, name, parser),
};
default:
(type: empty);
throw new Error(`Unable to determine event type for "${name}": ${type}`);
}
}
Expand All @@ -104,7 +103,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 @@ -162,6 +161,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 @@ -190,7 +198,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 @@ -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