Skip to content

Commit 2f25261

Browse files
Pranav-yadavfacebook-github-bot
authored andcommitted
Merge TS & Flow parsers' logic for Partial case to emitPartial fn & commonTypes cases into emitCommonTypes fn (#36450)
Summary: > [Codegen 78 - Assigned to Pranav-yadav] It depends on [Codegen 75][Codegen 76][Codegen 77] Extract the logic that emits Partial values in an emitPartial function, which takes the Parsers as parameter. >[Codegen 79 - Assigned to Pranav-yadav] It depends on [Codegen 78] Extract the basic cases logics (case Stringish, case Int32, case Double, ..., case Partial. `Flow` lines and `TypeScript` lines into a function emitCommonTypes in `parsers-primitives.js`. Make sure that the default case returns `null`. Call this function in the default: case `Flow`, `TypeScript` of the `index.js` file: if the function return something, return that from the default case; otherwise if the `emitCommonTypes` returns `null`, keep the current default implementation (throw the error). ### Changes - merged TS & Flow parsers' logic for `Partial` case to `emitPatial` fn - merged TS & Flow parsers' logic for below cases: - `Stringish` - `Int32` - `Double` - `Float` - `UnsafeObject` - `Object` - `Partial` - into an `emitCommonTypes` fn into `parsers-primitives.js` - add **_tests_** for `emitPartial` and `emitCommonTypes` fn's - add `getAnnotatedElementProperties` fn to parser & impl to both TS & Flow parsers ## Changelog: [INTERNAL] [CHANGED] - Merge TS & Flow parsers' logic for `Partial` case to `emitPatial` fn & `commonTypes` cases into `emitCommonTypes` fn Pull Request resolved: #36450 Test Plan: - `yarn lint && yarn run flow && yarn jest react-native` ⇒ � Reviewed By: rshest Differential Revision: D44132308 Pulled By: cipolleschi fbshipit-source-id: f965e85ecc5d94e57ad85334ce565a55c512fde4
1 parent 4b6b706 commit 2f25261

File tree

8 files changed

+380
-98
lines changed

8 files changed

+380
-98
lines changed

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

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const {
2828
emitString,
2929
emitStringish,
3030
emitMixed,
31+
emitPartial,
32+
emitCommonTypes,
3133
typeAliasResolution,
3234
typeEnumResolution,
3335
Visitor,
@@ -1253,3 +1255,243 @@ describe('Visitor', () => {
12531255
});
12541256
});
12551257
});
1258+
1259+
describe('emitPartial', () => {
1260+
const hasteModuleName = 'SampleTurboModule';
1261+
function emitPartialForUnitTest(
1262+
typeAnnotation: $FlowFixMe,
1263+
nullable: boolean,
1264+
): $FlowFixMe {
1265+
return emitPartial(
1266+
hasteModuleName,
1267+
typeAnnotation,
1268+
/* types: TypeDeclarationMap */
1269+
{},
1270+
/* aliasMap: {...NativeModuleAliasMap} */
1271+
{},
1272+
/* enumMap: {...NativeModuleEnumMap} */
1273+
{},
1274+
/* tryParse: ParserErrorCapturer */
1275+
// $FlowFixMe[missing-local-annot]
1276+
function <T>(_: () => T) {
1277+
return null;
1278+
},
1279+
/* cxxOnly: boolean */
1280+
false,
1281+
nullable,
1282+
parser,
1283+
);
1284+
}
1285+
1286+
describe("when 'typeAnnotation' doesn't have exactly 'one' typeParameter", () => {
1287+
const nullable = false;
1288+
const typeAnnotation = {
1289+
typeParameters: {
1290+
params: [1, 2],
1291+
type: 'TypeParameterInstantiation',
1292+
},
1293+
id: {
1294+
name: 'typeAnnotationName',
1295+
},
1296+
};
1297+
1298+
it('throws an error', () => {
1299+
expect(() => emitPartialForUnitTest(typeAnnotation, nullable)).toThrow(
1300+
'Partials only support annotating exactly one parameter.',
1301+
);
1302+
});
1303+
});
1304+
1305+
describe('when Partial Not annotating type parameter', () => {
1306+
const nullable = false;
1307+
const typeAnnotation = {
1308+
typeParameters: {
1309+
params: [
1310+
{
1311+
id: {
1312+
name: 'TypeDeclaration',
1313+
},
1314+
},
1315+
],
1316+
},
1317+
id: {
1318+
name: 'typeAnnotationName',
1319+
},
1320+
};
1321+
1322+
it('throws an error', () => {
1323+
expect(() => emitPartialForUnitTest(typeAnnotation, nullable)).toThrow(
1324+
'Partials only support annotating a type parameter.',
1325+
);
1326+
});
1327+
});
1328+
});
1329+
1330+
describe('emitCommonTypes', () => {
1331+
const hasteModuleName = 'SampleTurboModule';
1332+
1333+
function emitCommonTypesForUnitTest(
1334+
typeAnnotation: $FlowFixMe,
1335+
nullable: boolean,
1336+
): $FlowFixMe {
1337+
return emitCommonTypes(
1338+
hasteModuleName,
1339+
/* types: TypeDeclarationMap */
1340+
{},
1341+
typeAnnotation,
1342+
/* aliasMap: {...NativeModuleAliasMap} */
1343+
{},
1344+
/* enumMap: {...NativeModuleEnumMap} */
1345+
{},
1346+
/* tryParse: ParserErrorCapturer */
1347+
// $FlowFixMe[missing-local-annot]
1348+
function <T>(_: () => T) {
1349+
return null;
1350+
},
1351+
/* cxxOnly: boolean */
1352+
false,
1353+
nullable,
1354+
parser,
1355+
);
1356+
}
1357+
1358+
describe("when 'typeAnnotation.id.name' is 'Stringish'", () => {
1359+
const typeAnnotation = {
1360+
typeParameters: {
1361+
params: [1, 2],
1362+
type: 'StringTypeAnnotation',
1363+
},
1364+
id: {
1365+
name: 'Stringish',
1366+
},
1367+
};
1368+
const expected = {
1369+
type: 'StringTypeAnnotation',
1370+
};
1371+
const result = emitCommonTypesForUnitTest(typeAnnotation, false);
1372+
1373+
it("returns 'StringTypeAnnotation'", () => {
1374+
expect(result).toEqual(expected);
1375+
});
1376+
});
1377+
1378+
describe("when 'typeAnnotation.id.name' is 'Int32'", () => {
1379+
const typeAnnotation = {
1380+
typeParameters: {
1381+
params: [1, 2],
1382+
type: 'Int32TypeAnnotation',
1383+
},
1384+
id: {
1385+
name: 'Int32',
1386+
},
1387+
};
1388+
const expected = {
1389+
type: 'Int32TypeAnnotation',
1390+
};
1391+
const result = emitCommonTypesForUnitTest(typeAnnotation, false);
1392+
1393+
it("returns 'Int32TypeAnnotation'", () => {
1394+
expect(result).toEqual(expected);
1395+
});
1396+
});
1397+
1398+
describe("when 'typeAnnotation.id.name' is 'Double'", () => {
1399+
const typeAnnotation = {
1400+
typeParameters: {
1401+
params: [1, 2],
1402+
type: 'DoubleTypeAnnotation',
1403+
},
1404+
id: {
1405+
name: 'Double',
1406+
},
1407+
};
1408+
const expected = {
1409+
type: 'DoubleTypeAnnotation',
1410+
};
1411+
const result = emitCommonTypesForUnitTest(typeAnnotation, false);
1412+
1413+
it("returns 'DoubleTypeAnnotation'", () => {
1414+
expect(result).toEqual(expected);
1415+
});
1416+
});
1417+
1418+
describe("when 'typeAnnotation.id.name' is 'Float'", () => {
1419+
const typeAnnotation = {
1420+
typeParameters: {
1421+
params: [1, 2],
1422+
type: 'FloatTypeAnnotation',
1423+
},
1424+
id: {
1425+
name: 'Float',
1426+
},
1427+
};
1428+
const expected = {
1429+
type: 'FloatTypeAnnotation',
1430+
};
1431+
const result = emitCommonTypesForUnitTest(typeAnnotation, false);
1432+
1433+
it("returns 'FloatTypeAnnotation'", () => {
1434+
expect(result).toEqual(expected);
1435+
});
1436+
});
1437+
1438+
describe("when 'typeAnnotation.id.name' is 'UnsafeObject'", () => {
1439+
const typeAnnotation = {
1440+
typeParameters: {
1441+
params: [1, 2],
1442+
type: 'GenericObjectTypeAnnotation',
1443+
},
1444+
id: {
1445+
name: 'UnsafeObject',
1446+
},
1447+
};
1448+
const expected = {
1449+
type: 'GenericObjectTypeAnnotation',
1450+
};
1451+
const result = emitCommonTypesForUnitTest(typeAnnotation, false);
1452+
1453+
it("returns 'GenericObjectTypeAnnotation'", () => {
1454+
expect(result).toEqual(expected);
1455+
});
1456+
});
1457+
1458+
describe("when 'typeAnnotation.id.name' is 'Object'", () => {
1459+
const typeAnnotation = {
1460+
typeParameters: {
1461+
params: [1, 2],
1462+
type: 'GenericObjectTypeAnnotation',
1463+
},
1464+
id: {
1465+
name: 'Object',
1466+
},
1467+
};
1468+
const expected = {
1469+
type: 'GenericObjectTypeAnnotation',
1470+
};
1471+
const result = emitCommonTypesForUnitTest(typeAnnotation, false);
1472+
1473+
it("returns 'GenericObjectTypeAnnotation'", () => {
1474+
expect(result).toEqual(expected);
1475+
});
1476+
});
1477+
1478+
describe("when 'typeAnnotation.id.name' is '$Partial' i.e. Object", () => {
1479+
const typeAnnotation = {
1480+
typeParameters: {
1481+
params: [1],
1482+
type: 'GenericObjectTypeAnnotation',
1483+
},
1484+
id: {
1485+
name: 'Object',
1486+
},
1487+
};
1488+
const expected = {
1489+
type: 'GenericObjectTypeAnnotation',
1490+
};
1491+
const result = emitCommonTypesForUnitTest(typeAnnotation, false);
1492+
1493+
it("returns 'GenericObjectTypeAnnotation'", () => {
1494+
expect(result).toEqual(expected);
1495+
});
1496+
});
1497+
});

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

Lines changed: 14 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,16 @@ const {
3232
const {
3333
emitArrayType,
3434
emitBoolean,
35-
emitDouble,
36-
emitFloat,
3735
emitFunction,
3836
emitNumber,
39-
emitInt32,
4037
emitGenericObject,
41-
emitObject,
4238
emitPromise,
4339
emitRootTag,
4440
emitVoid,
4541
emitString,
46-
emitStringish,
4742
emitMixed,
4843
emitUnion,
44+
emitCommonTypes,
4945
typeAliasResolution,
5046
typeEnumResolution,
5147
} = require('../../parsers-primitives');
@@ -55,11 +51,6 @@ const {
5551
UnsupportedGenericParserError,
5652
} = require('../../errors');
5753

58-
const {
59-
throwIfPartialNotAnnotatingTypeParameter,
60-
throwIfPartialWithMoreParameter,
61-
} = require('../../error-utils');
62-
6354
function translateTypeAnnotation(
6455
hasteModuleName: string,
6556
/**
@@ -132,55 +123,27 @@ function translateTypeAnnotation(
132123

133124
return wrapNullable(nullable || isParamNullable, paramType);
134125
}
135-
case 'Stringish': {
136-
return emitStringish(nullable);
137-
}
138-
case 'Int32': {
139-
return emitInt32(nullable);
140-
}
141-
case 'Double': {
142-
return emitDouble(nullable);
143-
}
144-
case 'Float': {
145-
return emitFloat(nullable);
146-
}
147-
case 'UnsafeObject':
148-
case 'Object': {
149-
return emitGenericObject(nullable);
150-
}
151-
case 'Partial':
152-
case '$Partial': {
153-
throwIfPartialWithMoreParameter(typeAnnotation);
154-
155-
const annotatedElement = parser.extractAnnotatedElement(
156-
typeAnnotation,
157-
types,
158-
);
159-
160-
throwIfPartialNotAnnotatingTypeParameter(
161-
typeAnnotation,
162-
types,
163-
parser,
164-
);
165-
166-
const properties = parser.computePartialProperties(
167-
annotatedElement.right.properties,
126+
default: {
127+
const commonType = emitCommonTypes(
168128
hasteModuleName,
169129
types,
130+
typeAnnotation,
170131
aliasMap,
171132
enumMap,
172133
tryParse,
173134
cxxOnly,
174-
);
175-
176-
return emitObject(nullable, properties);
177-
}
178-
default: {
179-
throw new UnsupportedGenericParserError(
180-
hasteModuleName,
181-
typeAnnotation,
135+
nullable,
182136
parser,
183137
);
138+
139+
if (!commonType) {
140+
throw new UnsupportedGenericParserError(
141+
hasteModuleName,
142+
typeAnnotation,
143+
parser,
144+
);
145+
}
146+
return commonType;
184147
}
185148
}
186149
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@ class FlowParser implements Parser {
313313
componentName: funcArgumentParams[0].value,
314314
};
315315
}
316+
317+
getAnnotatedElementProperties(annotatedElement: $FlowFixMe): $FlowFixMe {
318+
return annotatedElement.right.properties;
319+
}
316320
}
317321

318322
module.exports = {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,11 @@ export interface Parser {
229229
typeArgumentParams: $FlowFixMe,
230230
funcArgumentParams: $FlowFixMe,
231231
): {[string]: string};
232+
233+
/**
234+
* Given a annotatedElement, it returns the properties of annotated element.
235+
* @parameter annotatedElement: the annotated element.
236+
* @returns: the properties of annotated element.
237+
*/
238+
getAnnotatedElementProperties(annotatedElement: $FlowFixMe): $FlowFixMe;
232239
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,8 @@ export class MockedParser implements Parser {
227227
componentName: funcArgumentParams[0].value,
228228
};
229229
}
230+
231+
getAnnotatedElementProperties(annotatedElement: $FlowFixMe): $FlowFixMe {
232+
return annotatedElement.right.properties;
233+
}
230234
}

0 commit comments

Comments
 (0)