mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Extract the functions to compute partial properties in the Flow and TypeScript 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 https://github.com/facebook/react-native/issues/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: https://github.com/facebook/react-native/pull/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
This commit is contained in:
committed by
Facebook GitHub Bot
parent
b73dd6726d
commit
a448c6d80f
@@ -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', () => {
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
+32
-1
@@ -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');
|
||||
@@ -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 = {
|
||||
|
||||
+27
-4
@@ -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.
|
||||
@@ -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(
|
||||
@@ -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>;
|
||||
}
|
||||
|
||||
+26
-1
@@ -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');
|
||||
@@ -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'},
|
||||
},
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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');
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user