Skip to content

Commit 5744b21

Browse files
Pranav-yadavfacebook-github-bot
authored andcommitted
Extract tryParse (Flow, TypeScript) lambda into parseObjectProperty fn (#35076)
Summary: This PR is a task of #34872 > Extract the content of the tryParse (Flow, TypeScript)lambda into a proper `parseObjectProperty` function into the parsers-commons.js file. also, - added new helper fn `isObjectProperty` in `parsers-commons.js` file. - added tests for `isObjectProperty` and `parseObjectProperty` fn 's ## Changelog [Internal] [Changed] - Extracted the content of the `tryParse` ([Flow](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/flow/modules/index.js#L292-L337), [TypeScript](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/typescript/modules/index.js#L306-L351)) lambda into a proper `parseObjectProperty` fn into the `parsers-commons.js` file. Pull Request resolved: #35076 Test Plan: - run ```bash yarn lint && yarn flow && yarn test-ci ``` - and ensure everything is � <img width="720" alt="image" src="https://user-images.githubusercontent.com/55224033/200105151-360b9b5e-52c7-4586-89b0-6860e9725f6e.png"> Reviewed By: cortinico Differential Revision: D40797241 Pulled By: cipolleschi fbshipit-source-id: 48b8900ead70d5eda2496f9ce044c11a9599a177
1 parent 114098d commit 5744b21

File tree

7 files changed

+348
-143
lines changed

7 files changed

+348
-143
lines changed

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

Lines changed: 209 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,29 @@
1111

1212
'use-strict';
1313

14-
import {assertGenericTypeAnnotationHasExactlyOneTypeParameter} from '../parsers-commons';
15-
import type {ParserType} from '../errors';
16-
const {
14+
import {
15+
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
16+
isObjectProperty,
17+
parseObjectProperty,
1718
wrapNullable,
1819
unwrapNullable,
1920
emitUnionTypeAnnotation,
20-
} = require('../parsers-commons.js');
21-
const {UnsupportedUnionTypeAnnotationParserError} = require('../errors');
21+
} from '../parsers-commons';
22+
import type {ParserType} from '../errors';
2223
import type {UnionTypeAnnotationMemberType} from '../../CodegenSchema';
24+
25+
const {
26+
UnsupportedUnionTypeAnnotationParserError,
27+
UnsupportedObjectPropertyTypeAnnotationParserError,
28+
} = require('../errors');
29+
2330
import {MockedParser} from '../parserMock';
2431

2532
const parser = new MockedParser();
2633

34+
const flowTranslateTypeAnnotation = require('../flow/modules/index');
35+
const typeScriptTranslateTypeAnnotation = require('../typescript/modules/index');
36+
2737
describe('wrapNullable', () => {
2838
describe('when nullable is true', () => {
2939
it('returns nullable type annotation', () => {
@@ -189,6 +199,200 @@ describe('assertGenericTypeAnnotationHasExactlyOneTypeParameter', () => {
189199
});
190200
});
191201

202+
describe('isObjectProperty', () => {
203+
const propertyStub = {
204+
/* type: 'notObjectTypeProperty', */
205+
typeAnnotation: {
206+
typeAnnotation: 'wrongTypeAnnotation',
207+
},
208+
value: 'wrongValue',
209+
name: 'wrongName',
210+
};
211+
212+
describe("when 'language' is 'Flow'", () => {
213+
const language: ParserType = 'Flow';
214+
it("returns 'true' if 'property.type' is 'ObjectTypeProperty'", () => {
215+
const result = isObjectProperty(
216+
{
217+
type: 'ObjectTypeProperty',
218+
...propertyStub,
219+
},
220+
language,
221+
);
222+
expect(result).toEqual(true);
223+
});
224+
225+
it("returns 'true' if 'property.type' is 'ObjectTypeIndexer'", () => {
226+
const result = isObjectProperty(
227+
{
228+
type: 'ObjectTypeIndexer',
229+
...propertyStub,
230+
},
231+
language,
232+
);
233+
expect(result).toEqual(true);
234+
});
235+
236+
it("returns 'false' if 'property.type' is not 'ObjectTypeProperty' or 'ObjectTypeIndexer'", () => {
237+
const result = isObjectProperty(
238+
{
239+
type: 'notObjectTypeProperty',
240+
...propertyStub,
241+
},
242+
language,
243+
);
244+
expect(result).toEqual(false);
245+
});
246+
});
247+
248+
describe("when 'language' is 'TypeScript'", () => {
249+
const language: ParserType = 'TypeScript';
250+
it("returns 'true' if 'property.type' is 'TSPropertySignature'", () => {
251+
const result = isObjectProperty(
252+
{
253+
type: 'TSPropertySignature',
254+
...propertyStub,
255+
},
256+
language,
257+
);
258+
expect(result).toEqual(true);
259+
});
260+
261+
it("returns 'true' if 'property.type' is 'TSIndexSignature'", () => {
262+
const result = isObjectProperty(
263+
{
264+
type: 'TSIndexSignature',
265+
...propertyStub,
266+
},
267+
language,
268+
);
269+
expect(result).toEqual(true);
270+
});
271+
272+
it("returns 'false' if 'property.type' is not 'TSPropertySignature' or 'TSIndexSignature'", () => {
273+
const result = isObjectProperty(
274+
{
275+
type: 'notTSPropertySignature',
276+
...propertyStub,
277+
},
278+
language,
279+
);
280+
expect(result).toEqual(false);
281+
});
282+
});
283+
});
284+
285+
describe('parseObjectProperty', () => {
286+
const moduleName = 'testModuleName';
287+
const types = {['wrongName']: 'wrongType'};
288+
const aliasMap = {};
289+
const tryParse = () => null;
290+
const cxxOnly = false;
291+
const nullable = true;
292+
293+
describe("when 'language' is 'Flow'", () => {
294+
const language: ParserType = 'Flow';
295+
it("throws an 'UnsupportedObjectPropertyTypeAnnotationParserError' error if 'property.type' is not 'ObjectTypeProperty' or 'ObjectTypeIndexer'.", () => {
296+
const property = {
297+
type: 'notObjectTypeProperty',
298+
typeAnnotation: {
299+
type: 'notObjectTypeProperty',
300+
typeAnnotation: 'wrongTypeAnnotation',
301+
},
302+
value: 'wrongValue',
303+
name: 'wrongName',
304+
};
305+
const expected = new UnsupportedObjectPropertyTypeAnnotationParserError(
306+
moduleName,
307+
property,
308+
property.type,
309+
language,
310+
);
311+
expect(() =>
312+
parseObjectProperty(
313+
property,
314+
moduleName,
315+
types,
316+
aliasMap,
317+
tryParse,
318+
cxxOnly,
319+
language,
320+
nullable,
321+
flowTranslateTypeAnnotation,
322+
),
323+
).toThrow(expected);
324+
});
325+
});
326+
327+
describe("when 'language' is 'TypeScript'", () => {
328+
const language: ParserType = 'TypeScript';
329+
it("throws an 'UnsupportedObjectPropertyTypeAnnotationParserError' error if 'property.type' is not 'TSPropertySignature' or 'TSIndexSignature'.", () => {
330+
const property = {
331+
type: 'notTSPropertySignature',
332+
typeAnnotation: {
333+
typeAnnotation: 'wrongTypeAnnotation',
334+
},
335+
value: 'wrongValue',
336+
name: 'wrongName',
337+
};
338+
const expected = new UnsupportedObjectPropertyTypeAnnotationParserError(
339+
moduleName,
340+
property,
341+
property.type,
342+
language,
343+
);
344+
expect(() =>
345+
parseObjectProperty(
346+
property,
347+
moduleName,
348+
types,
349+
aliasMap,
350+
tryParse,
351+
cxxOnly,
352+
language,
353+
nullable,
354+
typeScriptTranslateTypeAnnotation,
355+
),
356+
).toThrow(expected);
357+
});
358+
359+
it("returns a 'NativeModuleBaseTypeAnnotation' object with 'typeAnnotation.type' equal to 'GenericObjectTypeAnnotation', if 'property.type' is 'TSIndexSignature'.", () => {
360+
const property = {
361+
type: 'TSIndexSignature',
362+
typeAnnotation: {
363+
type: 'TSIndexSignature',
364+
typeAnnotation: 'TSIndexSignature',
365+
},
366+
key: {
367+
name: 'testKeyName',
368+
},
369+
value: 'wrongValue',
370+
name: 'wrongName',
371+
parameters: [{name: 'testName'}],
372+
};
373+
const result = parseObjectProperty(
374+
property,
375+
moduleName,
376+
types,
377+
aliasMap,
378+
tryParse,
379+
cxxOnly,
380+
language,
381+
nullable,
382+
typeScriptTranslateTypeAnnotation,
383+
);
384+
const expected = {
385+
name: 'testName',
386+
optional: false,
387+
typeAnnotation: wrapNullable(nullable, {
388+
type: 'GenericObjectTypeAnnotation',
389+
}),
390+
};
391+
expect(result).toEqual(expected);
392+
});
393+
});
394+
});
395+
192396
describe('emitUnionTypeAnnotation', () => {
193397
const hasteModuleName = 'SampleTurboModule';
194398

packages/react-native-codegen/src/parsers/error-utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow strict-local
7+
* @flow strict
88
* @format
99
*/
1010

packages/react-native-codegen/src/parsers/flow/components/schema.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @format
87
* @flow strict
8+
* @format
99
*/
1010

1111
'use strict';

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

Lines changed: 15 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils';
2626
import type {NativeModuleTypeAnnotation} from '../../../CodegenSchema.js';
2727
const {nullGuard} = require('../../parsers-utils');
2828

29-
const {throwIfMoreThanOneModuleRegistryCalls} = require('../../error-utils');
30-
const {visit, isModuleRegistryCall} = require('../../utils');
29+
const {visit, verifyPlatforms, isModuleRegistryCall} = require('../../utils');
3130
const {resolveTypeAnnotation, getTypes} = require('../utils.js');
3231
const {
3332
unwrapNullable,
3433
wrapNullable,
3534
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
35+
parseObjectProperty,
3636
emitUnionTypeAnnotation,
3737
translateDefault,
3838
} = require('../../parsers-commons');
@@ -62,15 +62,13 @@ const {
6262
IncorrectModuleRegistryCallArgumentTypeParserError,
6363
} = require('../../errors.js');
6464

65-
const {verifyPlatforms} = require('../../utils');
66-
6765
const {
6866
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
6967
throwIfModuleInterfaceNotFound,
7068
throwIfModuleInterfaceIsMisnamed,
71-
throwIfPropertyValueTypeIsUnsupported,
7269
throwIfUnusedModuleInterfaceParserError,
7370
throwIfWrongNumberOfCallExpressionArgs,
71+
throwIfMoreThanOneModuleRegistryCalls,
7472
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
7573
throwIfUntypedModule,
7674
throwIfModuleTypeIsUnsupported,
@@ -79,7 +77,6 @@ const {
7977
} = require('../../error-utils');
8078

8179
const {FlowParser} = require('../parser.js');
82-
const {getKeyName} = require('../../parsers-commons');
8380

8481
const language = 'Flow';
8582
const parser = new FlowParser();
@@ -262,61 +259,17 @@ function translateTypeAnnotation(
262259
.map<?NamedShape<Nullable<NativeModuleBaseTypeAnnotation>>>(
263260
property => {
264261
return tryParse(() => {
265-
if (
266-
property.type !== 'ObjectTypeProperty' &&
267-
property.type !== 'ObjectTypeIndexer'
268-
) {
269-
throw new UnsupportedObjectPropertyTypeAnnotationParserError(
270-
hasteModuleName,
271-
property,
272-
property.type,
273-
language,
274-
);
275-
}
276-
277-
const {optional = false} = property;
278-
const name = getKeyName(property, hasteModuleName, language);
279-
if (property.type === 'ObjectTypeIndexer') {
280-
return {
281-
name,
282-
optional,
283-
typeAnnotation: emitObject(nullable),
284-
};
285-
}
286-
const [propertyTypeAnnotation, isPropertyNullable] =
287-
unwrapNullable(
288-
translateTypeAnnotation(
289-
hasteModuleName,
290-
property.value,
291-
types,
292-
aliasMap,
293-
tryParse,
294-
cxxOnly,
295-
),
296-
);
297-
298-
if (
299-
propertyTypeAnnotation.type === 'FunctionTypeAnnotation' ||
300-
propertyTypeAnnotation.type === 'PromiseTypeAnnotation' ||
301-
propertyTypeAnnotation.type === 'VoidTypeAnnotation'
302-
) {
303-
throwIfPropertyValueTypeIsUnsupported(
304-
hasteModuleName,
305-
property.value,
306-
property.key,
307-
propertyTypeAnnotation.type,
308-
language,
309-
);
310-
} else {
311-
return {
312-
name,
313-
optional,
314-
typeAnnotation: wrapNullable(
315-
isPropertyNullable,
316-
propertyTypeAnnotation,
317-
),
318-
};
319-
}
262+
return parseObjectProperty(
263+
property,
264+
hasteModuleName,
265+
types,
266+
aliasMap,
267+
tryParse,
268+
cxxOnly,
269+
language,
270+
nullable,
271+
translateTypeAnnotation,
272+
);
320273
});
321274
},
322275
)
@@ -596,4 +549,5 @@ function buildModuleSchema(
596549

597550
module.exports = {
598551
buildModuleSchema,
552+
flowTranslateTypeAnnotation: translateTypeAnnotation,
599553
};

0 commit comments

Comments
 (0)