Skip to content

Commit

Permalink
Chore: mv translateFunctionTypeAnnotation fn > parsers-commons.js (
Browse files Browse the repository at this point in the history
…#35343)

Summary:
This PR should solve the `"Circular Dependencies"` problem/issue because of which some PRs are getting blocked as discussed here #35288 (comment)

- also moved below helpers to `parsers-commons.js`;
- `getTypeAnnotationParameters`
- `getFunctionNameFromParameter`
- `getParameterName`
- `getParameterTypeAnnotation`
- `getTypeAnnotationReturnType`

<3

## Changelog

[INTERNAL] [CHANGED] - Moved `translateFunctionTypeAnnotation` fn from `parsers-primitives.js` to `parsers-commons.js` also, moved it's helpers

Pull Request resolved: #35343

Test Plan:
- ensure 👇 is `#00ff00`

`yarn lint && yarn flow && yarn test-ci`

Reviewed By: christophpurrer

Differential Revision: D41273191

Pulled By: rshest

fbshipit-source-id: cc1839a91579e7914f05516a90b280a776510c9d
  • Loading branch information
Pranav-yadav authored and facebook-github-bot committed Nov 15, 2022
1 parent 49d5e7c commit adee7be
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const {
parseObjectProperty,
emitUnionTypeAnnotation,
translateDefault,
translateFunctionTypeAnnotation,
} = require('../../parsers-commons');
const {
emitBoolean,
Expand All @@ -49,7 +50,6 @@ const {
emitStringish,
emitMixedTypeAnnotation,
typeAliasResolution,
translateFunctionTypeAnnotation,
} = require('../../parsers-primitives');

const {
Expand Down
146 changes: 145 additions & 1 deletion packages/react-native-codegen/src/parsers/parsers-commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import type {
NativeModuleEnumDeclaration,
NativeModuleSchema,
NativeModuleTypeAnnotation,
NativeModuleFunctionTypeAnnotation,
NativeModuleParamTypeAnnotation,
NativeModuleUnionTypeAnnotation,
Nullable,
SchemaType,
Expand All @@ -25,15 +27,21 @@ import type {ParserType} from './errors';
import type {Parser} from './parser';
import type {ParserErrorCapturer, TypeDeclarationMap} from './utils';

const {throwIfPropertyValueTypeIsUnsupported} = require('./error-utils');
const {
throwIfPropertyValueTypeIsUnsupported,
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
} = require('./error-utils');
const {
MissingTypeParameterGenericParserError,
MoreThanOneTypeParameterGenericParserError,
UnsupportedEnumDeclarationParserError,
UnsupportedGenericParserError,
UnsupportedObjectPropertyTypeAnnotationParserError,
UnsupportedUnionTypeAnnotationParserError,
UnnamedFunctionParamParserError,
} = require('./errors');

const invariant = require('invariant');

function wrapModuleSchema(
Expand Down Expand Up @@ -259,6 +267,141 @@ function translateDefault(
);
}

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

function getFunctionNameFromParameter(
param: NamedShape<Nullable<NativeModuleParamTypeAnnotation>>,
language: ParserType,
): $FlowFixMe {
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,
): $FlowFixMe {
return language === 'Flow'
? param.typeAnnotation
: param.typeAnnotation.typeAnnotation;
}

function getTypeAnnotationReturnType(
typeAnnotation: $FlowFixMe,
language: ParserType,
): $FlowFixMe {
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<$FlowFixMe>(
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<$FlowFixMe>(
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 = {
wrapModuleSchema,
unwrapNullable,
Expand All @@ -268,4 +411,5 @@ module.exports = {
parseObjectProperty,
emitUnionTypeAnnotation,
translateDefault,
translateFunctionTypeAnnotation,
};
144 changes: 2 additions & 142 deletions packages/react-native-codegen/src/parsers/parsers-primitives.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
* @flow strict
* @format
*/

Expand All @@ -14,15 +14,13 @@ import type {
BooleanTypeAnnotation,
DoubleTypeAnnotation,
Int32TypeAnnotation,
NamedShape,
NativeModuleAliasMap,
NativeModuleBaseTypeAnnotation,
NativeModuleFloatTypeAnnotation,
NativeModuleFunctionTypeAnnotation,
NativeModuleGenericObjectTypeAnnotation,
NativeModuleMixedTypeAnnotation,
NativeModuleNumberTypeAnnotation,
NativeModuleParamTypeAnnotation,
NativeModulePromiseTypeAnnotation,
NativeModuleTypeAliasTypeAnnotation,
Nullable,
Expand All @@ -39,15 +37,10 @@ import type {
TypeDeclarationMap,
} from './utils';

const {
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
} = require('./error-utils');
const {UnnamedFunctionParamParserError} = require('./errors');
const {
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
unwrapNullable,
wrapNullable,
translateFunctionTypeAnnotation,
} = require('./parsers-commons');

function emitBoolean(nullable: boolean): Nullable<BooleanTypeAnnotation> {
Expand Down Expand Up @@ -221,138 +214,6 @@ function emitFloat(
});
}

function getTypeAnnotationParameters(
typeAnnotation: $FlowFixMe,
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<$FlowFixMe>(
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<$FlowFixMe>(
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 @@ -368,5 +229,4 @@ module.exports = {
emitStringish,
emitMixedTypeAnnotation,
typeAliasResolution,
translateFunctionTypeAnnotation,
};
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const {
parseObjectProperty,
emitUnionTypeAnnotation,
translateDefault,
translateFunctionTypeAnnotation,
} = require('../../parsers-commons');
const {
emitBoolean,
Expand All @@ -48,7 +49,6 @@ const {
emitStringish,
emitMixedTypeAnnotation,
typeAliasResolution,
translateFunctionTypeAnnotation,
} = require('../../parsers-primitives');
const {
UnsupportedArrayElementTypeAnnotationParserError,
Expand Down

0 comments on commit adee7be

Please sign in to comment.