Skip to content
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 @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
* @flow strict
*/

'use strict';
Expand Down
32 changes: 4 additions & 28 deletions packages/react-native-codegen/src/parsers/flow/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const {
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
emitMixedTypeAnnotation,
emitUnionTypeAnnotation,
translateDefault,
} = require('../../parsers-commons');
const {
emitBoolean,
Expand All @@ -59,9 +60,7 @@ const {
const {
UnnamedFunctionParamParserError,
UnsupportedArrayElementTypeAnnotationParserError,
UnsupportedGenericParserError,
UnsupportedTypeAnnotationParserError,
UnsupportedEnumDeclarationParserError,
UnsupportedObjectPropertyTypeAnnotationParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');
Expand Down Expand Up @@ -250,34 +249,11 @@ function translateTypeAnnotation(
return emitObject(nullable);
}
default: {
const maybeEumDeclaration = types[typeAnnotation.id.name];
if (
maybeEumDeclaration &&
maybeEumDeclaration.type === 'EnumDeclaration'
) {
const memberType = maybeEumDeclaration.body.type
.replace('EnumNumberBody', 'NumberTypeAnnotation')
.replace('EnumStringBody', 'StringTypeAnnotation');
if (
memberType === 'NumberTypeAnnotation' ||
memberType === 'StringTypeAnnotation'
) {
return wrapNullable(nullable, {
type: 'EnumDeclaration',
memberType: memberType,
});
} else {
throw new UnsupportedEnumDeclarationParserError(
hasteModuleName,
typeAnnotation,
memberType,
language,
);
}
}
throw new UnsupportedGenericParserError(
return translateDefault(
hasteModuleName,
typeAnnotation,
types,
nullable,
parser,
);
}
Expand Down
15 changes: 15 additions & 0 deletions packages/react-native-codegen/src/parsers/flow/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,24 @@

'use strict';

import type {ParserType} from '../errors';
import type {Parser} from '../parser';

class FlowParser implements Parser {
getMaybeEnumMemberType(maybeEnumDeclaration: $FlowFixMe): string {
return maybeEnumDeclaration.body.type
.replace('EnumNumberBody', 'NumberTypeAnnotation')
.replace('EnumStringBody', 'StringTypeAnnotation');
}

isEnumDeclaration(maybeEnumDeclaration: $FlowFixMe): boolean {
return maybeEnumDeclaration.type === 'EnumDeclaration';
}

language(): ParserType {
return 'Flow';
}

nameForGenericTypeAnnotation(typeAnnotation: $FlowFixMe): string {
return typeAnnotation.id.name;
}
Expand Down
18 changes: 18 additions & 0 deletions packages/react-native-codegen/src/parsers/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,29 @@

'use strict';

import type {ParserType} from './errors';

/**
* This is the main interface for Parsers of various languages.
* It exposes all the methods that contain language-specific logic.
*/
export interface Parser {
/**
* Given a type declaration, it possibly returns the name of the Enum type.
* @parameter maybeEnumDeclaration: an object possibly containing an Enum declaration.
* @returns: the name of the Enum type.
*/
getMaybeEnumMemberType(maybeEnumDeclaration: $FlowFixMe): string;
/**
* Given a type declaration, it returns a boolean specifying if is an Enum declaration.
* @parameter maybeEnumDeclaration: an object possibly containing an Enum declaration.
* @returns: a boolean specifying if is an Enum declaration.
*/
isEnumDeclaration(maybeEnumDeclaration: $FlowFixMe): boolean;
/**
* @returns: the Parser language.
*/
language(): ParserType;
/**
* Given a type annotation for a generic type, it returns the type name.
* @parameter typeAnnotation: the annotation for a type in the AST.
Expand Down
46 changes: 46 additions & 0 deletions packages/react-native-codegen/src/parsers/parsers-commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ const {
UnsupportedObjectPropertyTypeAnnotationParserError,
} = require('./errors');
const invariant = require('invariant');
import type {TypeDeclarationMap} from './utils';
const {
UnsupportedEnumDeclarationParserError,
UnsupportedGenericParserError,
} = require('./errors');
import type {Parser} from './parser';
import type {NativeModuleEnumDeclaration} from '../CodegenSchema';

function wrapModuleSchema(
nativeModuleSchema: NativeModuleSchema,
Expand Down Expand Up @@ -157,6 +164,44 @@ function emitUnionTypeAnnotation(
});
}

function translateDefault(
hasteModuleName: string,
typeAnnotation: $FlowFixMe,
types: TypeDeclarationMap,
nullable: boolean,
parser: Parser,
): Nullable<NativeModuleEnumDeclaration> {
const maybeEnumDeclaration =
types[parser.nameForGenericTypeAnnotation(typeAnnotation)];

if (maybeEnumDeclaration && parser.isEnumDeclaration(maybeEnumDeclaration)) {
const memberType = parser.getMaybeEnumMemberType(maybeEnumDeclaration);

if (
memberType === 'NumberTypeAnnotation' ||
memberType === 'StringTypeAnnotation'
) {
return wrapNullable(nullable, {
type: 'EnumDeclaration',
memberType: memberType,
});
} else {
throw new UnsupportedEnumDeclarationParserError(
hasteModuleName,
typeAnnotation,
memberType,
parser.language(),
);
}
}

throw new UnsupportedGenericParserError(
hasteModuleName,
typeAnnotation,
parser,
);
}

function getKeyName(
propertyOrIndex: $FlowFixMe,
hasteModuleName: string,
Expand Down Expand Up @@ -190,4 +235,5 @@ module.exports = {
emitMixedTypeAnnotation,
emitUnionTypeAnnotation,
getKeyName,
translateDefault,
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
* @flow strict
*/

'use strict';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const {
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
emitMixedTypeAnnotation,
emitUnionTypeAnnotation,
translateDefault,
} = require('../../parsers-commons');
const {
emitBoolean,
Expand All @@ -63,7 +64,6 @@ const {
UnsupportedArrayElementTypeAnnotationParserError,
UnsupportedGenericParserError,
UnsupportedTypeAnnotationParserError,
UnsupportedEnumDeclarationParserError,
UnsupportedObjectPropertyTypeAnnotationParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');
Expand Down Expand Up @@ -264,36 +264,11 @@ function translateTypeAnnotation(
return emitObject(nullable);
}
default: {
const maybeEumDeclaration = types[typeAnnotation.typeName.name];
if (
maybeEumDeclaration &&
maybeEumDeclaration.type === 'TSEnumDeclaration'
) {
const memberType = maybeEumDeclaration.members[0].initializer
? maybeEumDeclaration.members[0].initializer.type
.replace('NumericLiteral', 'NumberTypeAnnotation')
.replace('StringLiteral', 'StringTypeAnnotation')
: 'StringTypeAnnotation';
if (
memberType === 'NumberTypeAnnotation' ||
memberType === 'StringTypeAnnotation'
) {
return wrapNullable(nullable, {
type: 'EnumDeclaration',
memberType: memberType,
});
} else {
throw new UnsupportedEnumDeclarationParserError(
hasteModuleName,
typeAnnotation,
memberType,
language,
);
}
}
throw new UnsupportedGenericParserError(
return translateDefault(
hasteModuleName,
typeAnnotation,
types,
nullable,
parser,
);
}
Expand Down
19 changes: 19 additions & 0 deletions packages/react-native-codegen/src/parsers/typescript/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,28 @@

'use strict';

import type {ParserType} from '../errors';
import type {Parser} from '../parser';

class TypeScriptParser implements Parser {
getMaybeEnumMemberType(maybeEnumDeclaration: $FlowFixMe): string {
if (maybeEnumDeclaration.members[0].initializer) {
return maybeEnumDeclaration.members[0].initializer.type
.replace('NumericLiteral', 'NumberTypeAnnotation')
.replace('StringLiteral', 'StringTypeAnnotation');
}

return 'StringTypeAnnotation';
}

isEnumDeclaration(maybeEnumDeclaration: $FlowFixMe): boolean {
return maybeEnumDeclaration.type === 'TSEnumDeclaration';
}

language(): ParserType {
return 'TypeScript';
}

nameForGenericTypeAnnotation(typeAnnotation: $FlowFixMe): string {
return typeAnnotation.typeName.name;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-codegen/src/parsers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Had to update this because the TypeDeclarationMap import inside packages/react-native-codegen/src/parsers/parsers-commons.js was complaining about dependencies of a '@flow strict' module must also be '@flow strict'!

* @flow strict
* @format
*/

Expand Down