Skip to content

Commit

Permalink
Extract the functions to compute partial properties in the Flow and T…
Browse files Browse the repository at this point in the history
…ypeScript parsers. (#36373)

Summary:
This PR aims to extract  the switch(configType) block from the buildSchema function into a separate function in a shared file between typescript and flow. It is a task of #34872:
> [Codegen 77 - assigned to MaeIg] Extract the functions to compute partial properties from the index.js file ([Flow](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/flow/modules/index.js#L180-L194) and [TypeScript](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/typescript/modules/index.js#L261-L278))in the Flow and TypeScript parsers.

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->
[Internal] [Changed] - Extract the functions to compute partial properties in the Flow and TypeScript parsers.

Pull Request resolved: #36373

Test Plan:
yarn flow:
<img width="560" alt="image" src="https://user-images.githubusercontent.com/40902940/222934040-97e88691-c1d6-44a1-b7ee-e500b4698cd2.png">

yarn lint:
<img width="509" alt="image" src="https://user-images.githubusercontent.com/40902940/222934137-05b6f1fd-a755-4323-baac-5954d36fd613.png">

yarn test
<img width="388" alt="image" src="https://user-images.githubusercontent.com/40902940/222934145-76a2bc38-7469-4c30-9301-bc21cfadded4.png">

Reviewed By: cipolleschi

Differential Revision: D43867937

Pulled By: rshest

fbshipit-source-id: f78fe399ff7d7d2cf8f44d1165418566b0a25628
  • Loading branch information
MaeIg authored and facebook-github-bot committed Mar 7, 2023
1 parent b73dd67 commit a448c6d
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 40 deletions.
124 changes: 124 additions & 0 deletions packages/react-native-codegen/src/parsers/__tests__/parsers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,61 @@ describe('FlowParser', () => {
expect(parser.callExpressionTypeParameters(node)).toBe(null);
});
});

describe('computePartialProperties', () => {
it('returns partial properties', () => {
const properties = [
{
type: 'ObjectTypeProperty',
key: {
type: 'Identifier',
name: 'a',
},
value: {
type: 'StringTypeAnnotation',
range: [],
},
},
{
type: 'ObjectTypeProperty',
key: {
type: 'Identifier',
name: 'b',
},
optional: true,
value: {
type: 'BooleanTypeAnnotation',
range: [],
},
},
];

const expected = [
{
name: 'a',
optional: true,
typeAnnotation: {type: 'StringTypeAnnotation'},
},
{
name: 'b',
optional: true,
typeAnnotation: {type: 'BooleanTypeAnnotation'},
},
];

expect(
parser.computePartialProperties(
properties,
'hasteModuleName',
{},
{},
{},
() => null,
false,
),
).toEqual(expected);
});
});
});

describe('TypeScriptParser', () => {
Expand Down Expand Up @@ -202,4 +257,73 @@ describe('TypeScriptParser', () => {
expect(parser.callExpressionTypeParameters(node)).toBe(null);
});
});

describe('computePartialProperties', () => {
it('returns partial properties', () => {
const properties = [
{
type: 'TSPropertySignature',
key: {
type: 'Identifier',
name: 'a',
},
typeAnnotation: {
type: 'TSTypeAnnotation',
typeAnnotation: {
type: 'TSTypeLiteral',
key: {
type: 'Identifier',
name: 'a',
},
members: [],
},
},
},
{
type: 'TSPropertySignature',
key: {
type: 'Identifier',
name: 'b',
},
optional: true,
typeAnnotation: {
type: 'TSTypeAnnotation',
typeAnnotation: {
type: 'TSStringKeyword',
key: {
type: 'Identifier',
name: 'b',
},
members: [],
},
},
},
];

const expected = [
{
name: 'a',
optional: true,
typeAnnotation: {properties: [], type: 'ObjectTypeAnnotation'},
},
{
name: 'b',
optional: true,
typeAnnotation: {type: 'StringTypeAnnotation'},
},
];

expect(
parser.computePartialProperties(
properties,
'hasteModuleName',
{},
{},
{},
() => null,
false,
),
).toEqual(expected);
});
});
});
25 changes: 9 additions & 16 deletions packages/react-native-codegen/src/parsers/flow/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,22 +172,15 @@ function translateTypeAnnotation(
parser,
);

const properties = annotatedElement.right.properties.map(prop => {
return {
name: prop.key.name,
optional: true,
typeAnnotation: translateTypeAnnotation(
hasteModuleName,
prop.value,
types,
aliasMap,
enumMap,
tryParse,
cxxOnly,
parser,
),
};
});
const properties = parser.computePartialProperties(
annotatedElement.right.properties,
hasteModuleName,
types,
aliasMap,
enumMap,
tryParse,
cxxOnly,
);

return emitObject(nullable, properties);
}
Expand Down
33 changes: 32 additions & 1 deletion packages/react-native-codegen/src/parsers/flow/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ import type {
NativeModuleParamTypeAnnotation,
NativeModuleEnumMemberType,
NativeModuleEnumMembers,
NativeModuleAliasMap,
NativeModuleEnumMap,
} from '../../CodegenSchema';
import type {ParserType} from '../errors';
import type {Parser} from '../parser';
import type {TypeDeclarationMap} from '../utils';
import type {ParserErrorCapturer, TypeDeclarationMap} from '../utils';

const {flowTranslateTypeAnnotation} = require('./modules');

// $FlowFixMe[untyped-import] there's no flowtype flow-parser
const flowParser = require('flow-parser');
Expand Down Expand Up @@ -257,6 +261,33 @@ class FlowParser implements Parser {
callExpressionTypeParameters(callExpression: $FlowFixMe): $FlowFixMe | null {
return callExpression.typeArguments || null;
}

computePartialProperties(
properties: Array<$FlowFixMe>,
hasteModuleName: string,
types: TypeDeclarationMap,
aliasMap: {...NativeModuleAliasMap},
enumMap: {...NativeModuleEnumMap},
tryParse: ParserErrorCapturer,
cxxOnly: boolean,
): Array<$FlowFixMe> {
return properties.map(prop => {
return {
name: prop.key.name,
optional: true,
typeAnnotation: flowTranslateTypeAnnotation(
hasteModuleName,
prop.value,
types,
aliasMap,
enumMap,
tryParse,
cxxOnly,
this,
),
};
});
}
}

module.exports = {
Expand Down
31 changes: 27 additions & 4 deletions packages/react-native-codegen/src/parsers/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import type {
NativeModuleParamTypeAnnotation,
NativeModuleEnumMemberType,
NativeModuleEnumMembers,
NativeModuleAliasMap,
NativeModuleEnumMap,
} from '../CodegenSchema';
import type {ParserType} from './errors';
import type {TypeDeclarationMap} from './utils';
import type {ParserErrorCapturer, TypeDeclarationMap} from './utils';

/**
* This is the main interface for Parsers of various languages.
Expand Down Expand Up @@ -161,8 +163,8 @@ export interface Parser {

/**
* Given a typeAnnotation, it returns the annotated element.
* @paramater typeAnnotation: the annotation for a type.
* @paramater types: a map of type declarations.
* @parameter typeAnnotation: the annotation for a type.
* @parameter types: a map of type declarations.
* @returns: the annotated element.
*/
extractAnnotatedElement(
Expand All @@ -177,8 +179,29 @@ export interface Parser {

/**
* Given a callExpression, it returns the typeParameters of the callExpression.
* @paramater callExpression: the callExpression.
* @parameter callExpression: the callExpression.
* @returns: the typeParameters of the callExpression or null if it does not exist.
*/
callExpressionTypeParameters(callExpression: $FlowFixMe): $FlowFixMe | null;

/**
* Given an array of properties from a Partial type, it returns an array of remaped properties.
* @parameter properties: properties from a Partial types.
* @parameter hasteModuleName: a string with the native module name.
* @parameter types: a map of type declarations.
* @parameter aliasMap: a map of type aliases.
* @parameter enumMap: a map of type enums.
* @parameter tryParse: a parser error capturer.
* @parameter cxxOnly: a boolean specifying if the module is Cxx only.
* @returns: an array of remaped properties
*/
computePartialProperties(
properties: Array<$FlowFixMe>,
hasteModuleName: string,
types: TypeDeclarationMap,
aliasMap: {...NativeModuleAliasMap},
enumMap: {...NativeModuleEnumMap},
tryParse: ParserErrorCapturer,
cxxOnly: boolean,
): Array<$FlowFixMe>;
}
27 changes: 26 additions & 1 deletion packages/react-native-codegen/src/parsers/parserMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import type {
NativeModuleParamTypeAnnotation,
NativeModuleEnumMemberType,
NativeModuleEnumMembers,
NativeModuleAliasMap,
NativeModuleEnumMap,
} from '../CodegenSchema';
import type {TypeDeclarationMap} from './utils';
import type {ParserErrorCapturer, TypeDeclarationMap} from './utils';

// $FlowFixMe[untyped-import] there's no flowtype flow-parser
const flowParser = require('flow-parser');
Expand Down Expand Up @@ -184,4 +186,27 @@ export class MockedParser implements Parser {
callExpressionTypeParameters(callExpression: $FlowFixMe): $FlowFixMe | null {
return callExpression.typeArguments || null;
}

computePartialProperties(
properties: Array<$FlowFixMe>,
hasteModuleName: string,
types: TypeDeclarationMap,
aliasMap: {...NativeModuleAliasMap},
enumMap: {...NativeModuleEnumMap},
tryParse: ParserErrorCapturer,
cxxOnly: boolean,
): Array<$FlowFixMe> {
return [
{
name: 'a',
optional: true,
typeAnnotation: {type: 'StringTypeAnnotation'},
},
{
name: 'b',
optional: true,
typeAnnotation: {type: 'BooleanTypeAnnotation'},
},
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,23 +253,14 @@ function translateTypeAnnotation(
parser,
);

const properties = annotatedElement.typeAnnotation.members.map(
member => {
return {
name: member.key.name,
optional: true,
typeAnnotation: translateTypeAnnotation(
hasteModuleName,
member.typeAnnotation.typeAnnotation,
types,
aliasMap,
enumMap,
tryParse,
cxxOnly,
parser,
),
};
},
const properties = parser.computePartialProperties(
annotatedElement.typeAnnotation.members,
hasteModuleName,
types,
aliasMap,
enumMap,
tryParse,
cxxOnly,
);

return emitObject(nullable, properties);
Expand Down
34 changes: 33 additions & 1 deletion packages/react-native-codegen/src/parsers/typescript/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ import type {
NativeModuleParamTypeAnnotation,
NativeModuleEnumMembers,
NativeModuleEnumMemberType,
NativeModuleAliasMap,
NativeModuleEnumMap,
} from '../../CodegenSchema';
import type {ParserType} from '../errors';
import type {Parser} from '../parser';
import type {TypeDeclarationMap} from '../utils';
import type {ParserErrorCapturer, TypeDeclarationMap} from '../utils';

const {typeScriptTranslateTypeAnnotation} = require('./modules');

// $FlowFixMe[untyped-import] Use flow-types for @babel/parser
const babelParser = require('@babel/parser');
Expand Down Expand Up @@ -243,7 +247,35 @@ class TypeScriptParser implements Parser {
callExpressionTypeParameters(callExpression: $FlowFixMe): $FlowFixMe | null {
return callExpression.typeParameters || null;
}

computePartialProperties(
properties: Array<$FlowFixMe>,
hasteModuleName: string,
types: TypeDeclarationMap,
aliasMap: {...NativeModuleAliasMap},
enumMap: {...NativeModuleEnumMap},
tryParse: ParserErrorCapturer,
cxxOnly: boolean,
): Array<$FlowFixMe> {
return properties.map(prop => {
return {
name: prop.key.name,
optional: true,
typeAnnotation: typeScriptTranslateTypeAnnotation(
hasteModuleName,
prop.typeAnnotation.typeAnnotation,
types,
aliasMap,
enumMap,
tryParse,
cxxOnly,
this,
),
};
});
}
}

module.exports = {
TypeScriptParser,
};

0 comments on commit a448c6d

Please sign in to comment.