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
97 changes: 5 additions & 92 deletions packages/react-native-codegen/src/parsers/flow/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const {
emitString,
emitStringish,
typeAliasResolution,
translateFunctionTypeAnnotation,
} = require('../../parsers-primitives');

const {
Expand Down Expand Up @@ -355,6 +356,8 @@ function translateTypeAnnotation(
aliasMap,
tryParse,
cxxOnly,
translateTypeAnnotation,
language,
);
return emitFunction(nullable, translateFunctionTypeAnnotationValue);
}
Expand Down Expand Up @@ -385,98 +388,6 @@ function translateTypeAnnotation(
}
}

function translateFunctionTypeAnnotation(
hasteModuleName: string,
// TODO(T71778680): This is a FunctionTypeAnnotation. Type this.
flowFunctionTypeAnnotation: $FlowFixMe,
types: TypeDeclarationMap,
aliasMap: {...NativeModuleAliasMap},
tryParse: ParserErrorCapturer,
cxxOnly: boolean,
): NativeModuleFunctionTypeAnnotation {
type Param = NamedShape<Nullable<NativeModuleParamTypeAnnotation>>;
const params: Array<Param> = [];

for (const flowParam of (flowFunctionTypeAnnotation.params: $ReadOnlyArray<$FlowFixMe>)) {
const parsedParam = tryParse(() => {
if (flowParam.name == null) {
throw new UnnamedFunctionParamParserError(
flowParam,
hasteModuleName,
language,
);
}

const paramName = flowParam.name.name;
const [paramTypeAnnotation, isParamTypeAnnotationNullable] =
unwrapNullable(
translateTypeAnnotation(
hasteModuleName,
flowParam.typeAnnotation,
types,
aliasMap,
tryParse,
cxxOnly,
),
);

if (
paramTypeAnnotation.type === 'VoidTypeAnnotation' ||
paramTypeAnnotation.type === 'PromiseTypeAnnotation'
) {
return throwIfUnsupportedFunctionParamTypeAnnotationParserError(
hasteModuleName,
flowParam.typeAnnotation,
paramName,
paramTypeAnnotation.type,
);
}

return {
name: flowParam.name.name,
optional: flowParam.optional,
typeAnnotation: wrapNullable(
isParamTypeAnnotationNullable,
paramTypeAnnotation,
),
};
});

if (parsedParam != null) {
params.push(parsedParam);
}
}

const [returnTypeAnnotation, isReturnTypeAnnotationNullable] = unwrapNullable(
translateTypeAnnotation(
hasteModuleName,
flowFunctionTypeAnnotation.returnType,
types,
aliasMap,
tryParse,
cxxOnly,
),
);

throwIfUnsupportedFunctionReturnTypeAnnotationParserError(
hasteModuleName,
flowFunctionTypeAnnotation,
'FunctionTypeAnnotation',
language,
cxxOnly,
returnTypeAnnotation.type,
);

return {
type: 'FunctionTypeAnnotation',
returnTypeAnnotation: wrapNullable(
isReturnTypeAnnotationNullable,
returnTypeAnnotation,
),
params,
};
}

function buildPropertySchema(
hasteModuleName: string,
// TODO(T71778680): This is an ObjectTypeProperty containing either:
Expand Down Expand Up @@ -516,6 +427,8 @@ function buildPropertySchema(
aliasMap,
tryParse,
cxxOnly,
translateTypeAnnotation,
language,
),
),
};
Expand Down
148 changes: 147 additions & 1 deletion packages/react-native-codegen/src/parsers/parsers-primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,25 @@ import type {
StringTypeAnnotation,
VoidTypeAnnotation,
NativeModuleFloatTypeAnnotation,
NativeModuleParamTypeAnnotation,
NamedShape,
} from '../CodegenSchema';
import type {ParserType} from './errors';
import type {TypeAliasResolutionStatus} from './utils';
import type {
ParserErrorCapturer,
TypeAliasResolutionStatus,
TypeDeclarationMap,
} from './utils';

const {UnnamedFunctionParamParserError} = require('./errors');

const {
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
} = require('./error-utils');

const {
unwrapNullable,
wrapNullable,
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
} = require('./parsers-commons');
Expand Down Expand Up @@ -180,6 +194,137 @@ function emitFloat(
});
}

function getTypeAnnotationParameters(
typeAnnotation: $FlowFixMe,
Copy link
Contributor

Choose a reason for hiding this comment

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

@gabrieldonadel The amount of $FlowFixMes increased quite a bit after the refactoring, is it something that could be avoided?..

Copy link
Collaborator Author

@gabrieldonadel gabrieldonadel Nov 4, 2022

Choose a reason for hiding this comment

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

That's because FunctionTypeAnnotation is still not typed yet and as most of these functions use some property of it and I'm not sure how to type it. There are two TODOs tho, of which I'm not 100% aware of the details, TODO(T71778680) and TODO(T108222691)

language: ParserType,
): $ReadOnlyArray<$FlowFixMe> {
return language === 'Flow'
? typeAnnotation.params
: typeAnnotation.parameters;
}

function getFunctionNameFromParameter(
param: NamedShape<Nullable<NativeModuleParamTypeAnnotation>>,
language: ParserType,
) {
return language === 'Flow' ? param.name : param.typeAnnotation;
}

function getParameterName(param: $FlowFixMe, language: ParserType): string {
return language === 'Flow' ? param.name.name : param.name;
}

function getParameterTypeAnnotation(param: $FlowFixMe, language: ParserType) {
return language === 'Flow'
? param.typeAnnotation
: param.typeAnnotation.typeAnnotation;
}

function getTypeAnnotationReturnType(
typeAnnotation: $FlowFixMe,
language: ParserType,
) {
return language === 'Flow'
? typeAnnotation.returnType
: typeAnnotation.typeAnnotation.typeAnnotation;
}

function translateFunctionTypeAnnotation(
hasteModuleName: string,
// TODO(T108222691): Use flow-types for @babel/parser
// TODO(T71778680): This is a FunctionTypeAnnotation. Type this.
typeAnnotation: $FlowFixMe,
types: TypeDeclarationMap,
aliasMap: {...NativeModuleAliasMap},
tryParse: ParserErrorCapturer,
cxxOnly: boolean,
translateTypeAnnotation: $FlowFixMe,
language: ParserType,
): NativeModuleFunctionTypeAnnotation {
type Param = NamedShape<Nullable<NativeModuleParamTypeAnnotation>>;
const params: Array<Param> = [];

for (const param of getTypeAnnotationParameters(typeAnnotation, language)) {
const parsedParam = tryParse(() => {
if (getFunctionNameFromParameter(param, language) == null) {
throw new UnnamedFunctionParamParserError(
param,
hasteModuleName,
language,
);
}

const paramName = getParameterName(param, language);

const [paramTypeAnnotation, isParamTypeAnnotationNullable] =
unwrapNullable(
translateTypeAnnotation(
hasteModuleName,
getParameterTypeAnnotation(param, language),
types,
aliasMap,
tryParse,
cxxOnly,
),
);

if (
paramTypeAnnotation.type === 'VoidTypeAnnotation' ||
paramTypeAnnotation.type === 'PromiseTypeAnnotation'
) {
return throwIfUnsupportedFunctionParamTypeAnnotationParserError(
hasteModuleName,
param.typeAnnotation,
paramName,
paramTypeAnnotation.type,
);
}

return {
name: paramName,
optional: Boolean(param.optional),
typeAnnotation: wrapNullable(
isParamTypeAnnotationNullable,
paramTypeAnnotation,
),
};
});

if (parsedParam != null) {
params.push(parsedParam);
}
}

const [returnTypeAnnotation, isReturnTypeAnnotationNullable] = unwrapNullable(
translateTypeAnnotation(
hasteModuleName,
getTypeAnnotationReturnType(typeAnnotation, language),
types,
aliasMap,
tryParse,
cxxOnly,
),
);

throwIfUnsupportedFunctionReturnTypeAnnotationParserError(
hasteModuleName,
typeAnnotation,
'FunctionTypeAnnotation',
language,
cxxOnly,
returnTypeAnnotation.type,
);

return {
type: 'FunctionTypeAnnotation',
returnTypeAnnotation: wrapNullable(
isReturnTypeAnnotationNullable,
returnTypeAnnotation,
),
params,
};
}

module.exports = {
emitBoolean,
emitDouble,
Expand All @@ -194,4 +339,5 @@ module.exports = {
emitString,
emitStringish,
typeAliasResolution,
translateFunctionTypeAnnotation,
};
Loading