Skip to content

Commit a448c6d

Browse files
MaeIgfacebook-github-bot
authored andcommitted
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 #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
1 parent b73dd67 commit a448c6d

File tree

7 files changed

+259
-40
lines changed

7 files changed

+259
-40
lines changed

packages/react-native-codegen/src/parsers/__tests__/parsers-test.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,61 @@ describe('FlowParser', () => {
110110
expect(parser.callExpressionTypeParameters(node)).toBe(null);
111111
});
112112
});
113+
114+
describe('computePartialProperties', () => {
115+
it('returns partial properties', () => {
116+
const properties = [
117+
{
118+
type: 'ObjectTypeProperty',
119+
key: {
120+
type: 'Identifier',
121+
name: 'a',
122+
},
123+
value: {
124+
type: 'StringTypeAnnotation',
125+
range: [],
126+
},
127+
},
128+
{
129+
type: 'ObjectTypeProperty',
130+
key: {
131+
type: 'Identifier',
132+
name: 'b',
133+
},
134+
optional: true,
135+
value: {
136+
type: 'BooleanTypeAnnotation',
137+
range: [],
138+
},
139+
},
140+
];
141+
142+
const expected = [
143+
{
144+
name: 'a',
145+
optional: true,
146+
typeAnnotation: {type: 'StringTypeAnnotation'},
147+
},
148+
{
149+
name: 'b',
150+
optional: true,
151+
typeAnnotation: {type: 'BooleanTypeAnnotation'},
152+
},
153+
];
154+
155+
expect(
156+
parser.computePartialProperties(
157+
properties,
158+
'hasteModuleName',
159+
{},
160+
{},
161+
{},
162+
() => null,
163+
false,
164+
),
165+
).toEqual(expected);
166+
});
167+
});
113168
});
114169

115170
describe('TypeScriptParser', () => {
@@ -202,4 +257,73 @@ describe('TypeScriptParser', () => {
202257
expect(parser.callExpressionTypeParameters(node)).toBe(null);
203258
});
204259
});
260+
261+
describe('computePartialProperties', () => {
262+
it('returns partial properties', () => {
263+
const properties = [
264+
{
265+
type: 'TSPropertySignature',
266+
key: {
267+
type: 'Identifier',
268+
name: 'a',
269+
},
270+
typeAnnotation: {
271+
type: 'TSTypeAnnotation',
272+
typeAnnotation: {
273+
type: 'TSTypeLiteral',
274+
key: {
275+
type: 'Identifier',
276+
name: 'a',
277+
},
278+
members: [],
279+
},
280+
},
281+
},
282+
{
283+
type: 'TSPropertySignature',
284+
key: {
285+
type: 'Identifier',
286+
name: 'b',
287+
},
288+
optional: true,
289+
typeAnnotation: {
290+
type: 'TSTypeAnnotation',
291+
typeAnnotation: {
292+
type: 'TSStringKeyword',
293+
key: {
294+
type: 'Identifier',
295+
name: 'b',
296+
},
297+
members: [],
298+
},
299+
},
300+
},
301+
];
302+
303+
const expected = [
304+
{
305+
name: 'a',
306+
optional: true,
307+
typeAnnotation: {properties: [], type: 'ObjectTypeAnnotation'},
308+
},
309+
{
310+
name: 'b',
311+
optional: true,
312+
typeAnnotation: {type: 'StringTypeAnnotation'},
313+
},
314+
];
315+
316+
expect(
317+
parser.computePartialProperties(
318+
properties,
319+
'hasteModuleName',
320+
{},
321+
{},
322+
{},
323+
() => null,
324+
false,
325+
),
326+
).toEqual(expected);
327+
});
328+
});
205329
});

packages/react-native-codegen/src/parsers/flow/modules/index.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -172,22 +172,15 @@ function translateTypeAnnotation(
172172
parser,
173173
);
174174

175-
const properties = annotatedElement.right.properties.map(prop => {
176-
return {
177-
name: prop.key.name,
178-
optional: true,
179-
typeAnnotation: translateTypeAnnotation(
180-
hasteModuleName,
181-
prop.value,
182-
types,
183-
aliasMap,
184-
enumMap,
185-
tryParse,
186-
cxxOnly,
187-
parser,
188-
),
189-
};
190-
});
175+
const properties = parser.computePartialProperties(
176+
annotatedElement.right.properties,
177+
hasteModuleName,
178+
types,
179+
aliasMap,
180+
enumMap,
181+
tryParse,
182+
cxxOnly,
183+
);
191184

192185
return emitObject(nullable, properties);
193186
}

packages/react-native-codegen/src/parsers/flow/parser.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ import type {
1818
NativeModuleParamTypeAnnotation,
1919
NativeModuleEnumMemberType,
2020
NativeModuleEnumMembers,
21+
NativeModuleAliasMap,
22+
NativeModuleEnumMap,
2123
} from '../../CodegenSchema';
2224
import type {ParserType} from '../errors';
2325
import type {Parser} from '../parser';
24-
import type {TypeDeclarationMap} from '../utils';
26+
import type {ParserErrorCapturer, TypeDeclarationMap} from '../utils';
27+
28+
const {flowTranslateTypeAnnotation} = require('./modules');
2529

2630
// $FlowFixMe[untyped-import] there's no flowtype flow-parser
2731
const flowParser = require('flow-parser');
@@ -257,6 +261,33 @@ class FlowParser implements Parser {
257261
callExpressionTypeParameters(callExpression: $FlowFixMe): $FlowFixMe | null {
258262
return callExpression.typeArguments || null;
259263
}
264+
265+
computePartialProperties(
266+
properties: Array<$FlowFixMe>,
267+
hasteModuleName: string,
268+
types: TypeDeclarationMap,
269+
aliasMap: {...NativeModuleAliasMap},
270+
enumMap: {...NativeModuleEnumMap},
271+
tryParse: ParserErrorCapturer,
272+
cxxOnly: boolean,
273+
): Array<$FlowFixMe> {
274+
return properties.map(prop => {
275+
return {
276+
name: prop.key.name,
277+
optional: true,
278+
typeAnnotation: flowTranslateTypeAnnotation(
279+
hasteModuleName,
280+
prop.value,
281+
types,
282+
aliasMap,
283+
enumMap,
284+
tryParse,
285+
cxxOnly,
286+
this,
287+
),
288+
};
289+
});
290+
}
260291
}
261292

262293
module.exports = {

packages/react-native-codegen/src/parsers/parser.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ import type {
1818
NativeModuleParamTypeAnnotation,
1919
NativeModuleEnumMemberType,
2020
NativeModuleEnumMembers,
21+
NativeModuleAliasMap,
22+
NativeModuleEnumMap,
2123
} from '../CodegenSchema';
2224
import type {ParserType} from './errors';
23-
import type {TypeDeclarationMap} from './utils';
25+
import type {ParserErrorCapturer, TypeDeclarationMap} from './utils';
2426

2527
/**
2628
* This is the main interface for Parsers of various languages.
@@ -161,8 +163,8 @@ export interface Parser {
161163

162164
/**
163165
* Given a typeAnnotation, it returns the annotated element.
164-
* @paramater typeAnnotation: the annotation for a type.
165-
* @paramater types: a map of type declarations.
166+
* @parameter typeAnnotation: the annotation for a type.
167+
* @parameter types: a map of type declarations.
166168
* @returns: the annotated element.
167169
*/
168170
extractAnnotatedElement(
@@ -177,8 +179,29 @@ export interface Parser {
177179

178180
/**
179181
* Given a callExpression, it returns the typeParameters of the callExpression.
180-
* @paramater callExpression: the callExpression.
182+
* @parameter callExpression: the callExpression.
181183
* @returns: the typeParameters of the callExpression or null if it does not exist.
182184
*/
183185
callExpressionTypeParameters(callExpression: $FlowFixMe): $FlowFixMe | null;
186+
187+
/**
188+
* Given an array of properties from a Partial type, it returns an array of remaped properties.
189+
* @parameter properties: properties from a Partial types.
190+
* @parameter hasteModuleName: a string with the native module name.
191+
* @parameter types: a map of type declarations.
192+
* @parameter aliasMap: a map of type aliases.
193+
* @parameter enumMap: a map of type enums.
194+
* @parameter tryParse: a parser error capturer.
195+
* @parameter cxxOnly: a boolean specifying if the module is Cxx only.
196+
* @returns: an array of remaped properties
197+
*/
198+
computePartialProperties(
199+
properties: Array<$FlowFixMe>,
200+
hasteModuleName: string,
201+
types: TypeDeclarationMap,
202+
aliasMap: {...NativeModuleAliasMap},
203+
enumMap: {...NativeModuleEnumMap},
204+
tryParse: ParserErrorCapturer,
205+
cxxOnly: boolean,
206+
): Array<$FlowFixMe>;
184207
}

packages/react-native-codegen/src/parsers/parserMock.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import type {
2020
NativeModuleParamTypeAnnotation,
2121
NativeModuleEnumMemberType,
2222
NativeModuleEnumMembers,
23+
NativeModuleAliasMap,
24+
NativeModuleEnumMap,
2325
} from '../CodegenSchema';
24-
import type {TypeDeclarationMap} from './utils';
26+
import type {ParserErrorCapturer, TypeDeclarationMap} from './utils';
2527

2628
// $FlowFixMe[untyped-import] there's no flowtype flow-parser
2729
const flowParser = require('flow-parser');
@@ -184,4 +186,27 @@ export class MockedParser implements Parser {
184186
callExpressionTypeParameters(callExpression: $FlowFixMe): $FlowFixMe | null {
185187
return callExpression.typeArguments || null;
186188
}
189+
190+
computePartialProperties(
191+
properties: Array<$FlowFixMe>,
192+
hasteModuleName: string,
193+
types: TypeDeclarationMap,
194+
aliasMap: {...NativeModuleAliasMap},
195+
enumMap: {...NativeModuleEnumMap},
196+
tryParse: ParserErrorCapturer,
197+
cxxOnly: boolean,
198+
): Array<$FlowFixMe> {
199+
return [
200+
{
201+
name: 'a',
202+
optional: true,
203+
typeAnnotation: {type: 'StringTypeAnnotation'},
204+
},
205+
{
206+
name: 'b',
207+
optional: true,
208+
typeAnnotation: {type: 'BooleanTypeAnnotation'},
209+
},
210+
];
211+
}
187212
}

packages/react-native-codegen/src/parsers/typescript/modules/index.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -253,23 +253,14 @@ function translateTypeAnnotation(
253253
parser,
254254
);
255255

256-
const properties = annotatedElement.typeAnnotation.members.map(
257-
member => {
258-
return {
259-
name: member.key.name,
260-
optional: true,
261-
typeAnnotation: translateTypeAnnotation(
262-
hasteModuleName,
263-
member.typeAnnotation.typeAnnotation,
264-
types,
265-
aliasMap,
266-
enumMap,
267-
tryParse,
268-
cxxOnly,
269-
parser,
270-
),
271-
};
272-
},
256+
const properties = parser.computePartialProperties(
257+
annotatedElement.typeAnnotation.members,
258+
hasteModuleName,
259+
types,
260+
aliasMap,
261+
enumMap,
262+
tryParse,
263+
cxxOnly,
273264
);
274265

275266
return emitObject(nullable, properties);

packages/react-native-codegen/src/parsers/typescript/parser.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ import type {
1818
NativeModuleParamTypeAnnotation,
1919
NativeModuleEnumMembers,
2020
NativeModuleEnumMemberType,
21+
NativeModuleAliasMap,
22+
NativeModuleEnumMap,
2123
} from '../../CodegenSchema';
2224
import type {ParserType} from '../errors';
2325
import type {Parser} from '../parser';
24-
import type {TypeDeclarationMap} from '../utils';
26+
import type {ParserErrorCapturer, TypeDeclarationMap} from '../utils';
27+
28+
const {typeScriptTranslateTypeAnnotation} = require('./modules');
2529

2630
// $FlowFixMe[untyped-import] Use flow-types for @babel/parser
2731
const babelParser = require('@babel/parser');
@@ -243,7 +247,35 @@ class TypeScriptParser implements Parser {
243247
callExpressionTypeParameters(callExpression: $FlowFixMe): $FlowFixMe | null {
244248
return callExpression.typeParameters || null;
245249
}
250+
251+
computePartialProperties(
252+
properties: Array<$FlowFixMe>,
253+
hasteModuleName: string,
254+
types: TypeDeclarationMap,
255+
aliasMap: {...NativeModuleAliasMap},
256+
enumMap: {...NativeModuleEnumMap},
257+
tryParse: ParserErrorCapturer,
258+
cxxOnly: boolean,
259+
): Array<$FlowFixMe> {
260+
return properties.map(prop => {
261+
return {
262+
name: prop.key.name,
263+
optional: true,
264+
typeAnnotation: typeScriptTranslateTypeAnnotation(
265+
hasteModuleName,
266+
prop.typeAnnotation.typeAnnotation,
267+
types,
268+
aliasMap,
269+
enumMap,
270+
tryParse,
271+
cxxOnly,
272+
this,
273+
),
274+
};
275+
});
276+
}
246277
}
278+
247279
module.exports = {
248280
TypeScriptParser,
249281
};

0 commit comments

Comments
 (0)