Skip to content

Commit

Permalink
refactor: mv translateArrayTypeAnnotation (Flow,TS) fns > `parsers-…
Browse files Browse the repository at this point in the history
…primitives.js` (#35479)

Summary:
This PR is a subtask of #34872
Moved `translateArrayTypeAnnotation` (Flow,TS) fns to `parsers-primitives.js`
- combined `Flow` and `TS` `translateArrayTypeAnnotation` fn 's into common fn
- moved it to `parsers-primitives.js`
- re-organized imports and exports :)

## Changelog

[Internal] [Changed] - Moved `translateArrayTypeAnnotation` (Flow,TS) fns to `parsers-primitives.js`

Pull Request resolved: #35479

Test Plan:
- ensure 👇 is `#00ff00`
`yarn lint && yarn flow && yarn test-ci`

Reviewed By: cipolleschi

Differential Revision: D41548046

Pulled By: GijsWeterings

fbshipit-source-id: 8fd7214f8b1e669ba42f326f814674eec179fed5
  • Loading branch information
Pranav-yadav authored and facebook-github-bot committed Nov 30, 2022
1 parent fc13f4b commit 5a20bd5
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 124 deletions.
64 changes: 4 additions & 60 deletions packages/react-native-codegen/src/parsers/flow/modules/index.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 @@ -19,13 +19,10 @@ import type {
NativeModuleSchema,
Nullable,
} from '../../../CodegenSchema.js';

import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils';

const {nullGuard} = require('../../parsers-utils');
const {visit, isModuleRegistryCall, verifyPlatforms} = require('../../utils');
const {resolveTypeAnnotation, getTypes} = require('../utils.js');

const {
unwrapNullable,
wrapNullable,
Expand All @@ -35,7 +32,6 @@ const {
translateDefault,
buildPropertySchema,
} = require('../../parsers-commons');

const {
emitBoolean,
emitDouble,
Expand All @@ -51,6 +47,7 @@ const {
emitStringish,
emitMixedTypeAnnotation,
typeAliasResolution,
translateArrayTypeAnnotation,
} = require('../../parsers-primitives');

const {
Expand All @@ -61,7 +58,6 @@ const {
const {
throwIfModuleInterfaceNotFound,
throwIfModuleInterfaceIsMisnamed,
throwIfArrayElementTypeAnnotationIsUnsupported,
throwIfUnusedModuleInterfaceParserError,
throwIfWrongNumberOfCallExpressionArgs,
throwIfMoreThanOneModuleRegistryCalls,
Expand All @@ -75,60 +71,6 @@ const {FlowParser} = require('../parser.js');
const language = 'Flow';
const parser = new FlowParser();

function translateArrayTypeAnnotation(
hasteModuleName: string,
types: TypeDeclarationMap,
aliasMap: {...NativeModuleAliasMap},
cxxOnly: boolean,
flowArrayType: 'Array' | '$ReadOnlyArray',
flowElementType: $FlowFixMe,
nullable: boolean,
): Nullable<NativeModuleTypeAnnotation> {
try {
/**
* TODO(T72031674): Migrate all our NativeModule specs to not use
* invalid Array ElementTypes. Then, make the elementType a required
* parameter.
*/
const [elementType, isElementTypeNullable] = unwrapNullable(
translateTypeAnnotation(
hasteModuleName,
flowElementType,
types,
aliasMap,
/**
* TODO(T72031674): Ensure that all ParsingErrors that are thrown
* while parsing the array element don't get captured and collected.
* Why? If we detect any parsing error while parsing the element,
* we should default it to null down the line, here. This is
* the correct behaviour until we migrate all our NativeModule specs
* to be parseable.
*/
nullGuard,
cxxOnly,
),
);

throwIfArrayElementTypeAnnotationIsUnsupported(
hasteModuleName,
flowElementType,
flowArrayType,
elementType.type,
language,
);

return wrapNullable(nullable, {
type: 'ArrayTypeAnnotation',
// $FlowFixMe[incompatible-call]
elementType: wrapNullable(isElementTypeNullable, elementType),
});
} catch (ex) {
return wrapNullable(nullable, {
type: 'ArrayTypeAnnotation',
});
}
}

function translateTypeAnnotation(
hasteModuleName: string,
/**
Expand Down Expand Up @@ -178,6 +120,8 @@ function translateTypeAnnotation(
typeAnnotation.type,
typeAnnotation.typeParameters.params[0],
nullable,
language,
translateTypeAnnotation,
);
}
case '$ReadOnly': {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-codegen/src/parsers/flow/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
* @flow strict
* @format
*/

Expand Down
65 changes: 64 additions & 1 deletion packages/react-native-codegen/src/parsers/parsers-primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@
'use strict';

import type {
Nullable,
BooleanTypeAnnotation,
DoubleTypeAnnotation,
Int32TypeAnnotation,
NativeModuleAliasMap,
NativeModuleBaseTypeAnnotation,
NativeModuleTypeAnnotation,
NativeModuleFloatTypeAnnotation,
NativeModuleFunctionTypeAnnotation,
NativeModuleGenericObjectTypeAnnotation,
NativeModuleMixedTypeAnnotation,
NativeModuleNumberTypeAnnotation,
NativeModulePromiseTypeAnnotation,
NativeModuleTypeAliasTypeAnnotation,
Nullable,
ObjectTypeAnnotation,
ReservedTypeAnnotation,
StringTypeAnnotation,
Expand All @@ -37,9 +38,14 @@ import type {
TypeDeclarationMap,
} from './utils';

const {
throwIfArrayElementTypeAnnotationIsUnsupported,
} = require('./error-utils');
const {nullGuard} = require('./parsers-utils');
const {
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
wrapNullable,
unwrapNullable,
translateFunctionTypeAnnotation,
} = require('./parsers-commons');

Expand Down Expand Up @@ -243,6 +249,62 @@ function emitFloat(
});
}

function translateArrayTypeAnnotation(
hasteModuleName: string,
types: TypeDeclarationMap,
aliasMap: {...NativeModuleAliasMap},
cxxOnly: boolean,
arrayType: 'Array' | 'ReadonlyArray',
elementType: $FlowFixMe,
nullable: boolean,
language: ParserType,
translateTypeAnnotation: $FlowFixMe,
): Nullable<NativeModuleTypeAnnotation> {
try {
/**
* TODO(T72031674): Migrate all our NativeModule specs to not use
* invalid Array ElementTypes. Then, make the elementType a required
* parameter.
*/
const [_elementType, isElementTypeNullable] = unwrapNullable(
translateTypeAnnotation(
hasteModuleName,
elementType,
types,
aliasMap,
/**
* TODO(T72031674): Ensure that all ParsingErrors that are thrown
* while parsing the array element don't get captured and collected.
* Why? If we detect any parsing error while parsing the element,
* we should default it to null down the line, here. This is
* the correct behaviour until we migrate all our NativeModule specs
* to be parseable.
*/
nullGuard,
cxxOnly,
),
);

throwIfArrayElementTypeAnnotationIsUnsupported(
hasteModuleName,
elementType,
arrayType,
_elementType.type,
language,
);

return wrapNullable(nullable, {
type: 'ArrayTypeAnnotation',
// $FlowFixMe[incompatible-call]
elementType: wrapNullable(isElementTypeNullable, _elementType),
});
} catch (ex) {
return wrapNullable(nullable, {
type: 'ArrayTypeAnnotation',
});
}
}

module.exports = {
emitBoolean,
emitDouble,
Expand All @@ -258,4 +320,5 @@ module.exports = {
emitStringish,
emitMixedTypeAnnotation,
typeAliasResolution,
translateArrayTypeAnnotation,
};
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 @@ -22,13 +22,10 @@ import type {

import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils';

const {nullGuard} = require('../../parsers-utils');
const {visit, isModuleRegistryCall, verifyPlatforms} = require('../../utils');
const {resolveTypeAnnotation, getTypes} = require('../utils.js');

const {
unwrapNullable,
wrapNullable,
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
parseObjectProperty,
emitUnionTypeAnnotation,
Expand All @@ -51,10 +48,10 @@ const {
emitStringish,
emitMixedTypeAnnotation,
typeAliasResolution,
translateArrayTypeAnnotation,
} = require('../../parsers-primitives');

const {
UnsupportedArrayElementTypeAnnotationParserError,
UnsupportedGenericParserError,
UnsupportedTypeAnnotationParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
Expand All @@ -69,68 +66,13 @@ const {
throwIfMoreThanOneModuleRegistryCalls,
throwIfMoreThanOneModuleInterfaceParserError,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
throwIfArrayElementTypeAnnotationIsUnsupported,
} = require('../../error-utils');

const {TypeScriptParser} = require('../parser');

const language = 'TypeScript';
const parser = new TypeScriptParser();

function translateArrayTypeAnnotation(
hasteModuleName: string,
types: TypeDeclarationMap,
aliasMap: {...NativeModuleAliasMap},
cxxOnly: boolean,
tsArrayType: 'Array' | 'ReadonlyArray',
tsElementType: $FlowFixMe,
nullable: boolean,
): Nullable<NativeModuleTypeAnnotation> {
try {
/**
* TODO(T72031674): Migrate all our NativeModule specs to not use
* invalid Array ElementTypes. Then, make the elementType a required
* parameter.
*/
const [elementType, isElementTypeNullable] = unwrapNullable(
translateTypeAnnotation(
hasteModuleName,
tsElementType,
types,
aliasMap,
/**
* TODO(T72031674): Ensure that all ParsingErrors that are thrown
* while parsing the array element don't get captured and collected.
* Why? If we detect any parsing error while parsing the element,
* we should default it to null down the line, here. This is
* the correct behaviour until we migrate all our NativeModule specs
* to be parseable.
*/
nullGuard,
cxxOnly,
),
);

throwIfArrayElementTypeAnnotationIsUnsupported(
hasteModuleName,
tsElementType,
tsArrayType,
elementType.type,
language,
);

return wrapNullable(nullable, {
type: 'ArrayTypeAnnotation',
// $FlowFixMe[incompatible-call]
elementType: wrapNullable(isElementTypeNullable, elementType),
});
} catch (ex) {
return wrapNullable(nullable, {
type: 'ArrayTypeAnnotation',
});
}
}

function translateTypeAnnotation(
hasteModuleName: string,
/**
Expand All @@ -155,6 +97,8 @@ function translateTypeAnnotation(
'Array',
typeAnnotation.elementType,
nullable,
language,
translateTypeAnnotation,
);
}
case 'TSTypeOperator': {
Expand All @@ -170,6 +114,8 @@ function translateTypeAnnotation(
'ReadonlyArray',
typeAnnotation.typeAnnotation.elementType,
nullable,
language,
translateTypeAnnotation,
);
} else {
throw new UnsupportedGenericParserError(
Expand Down Expand Up @@ -213,6 +159,8 @@ function translateTypeAnnotation(
typeAnnotation.type,
typeAnnotation.typeParameters.params[0],
nullable,
language,
translateTypeAnnotation,
);
}
case 'Stringish': {
Expand Down
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 Down
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 Down

0 comments on commit 5a20bd5

Please sign in to comment.