Skip to content

Commit 0ce4ea2

Browse files
ZihanChen-MSFTfacebook-github-bot
authored andcommitted
Ensure equivalent Flow and TypeScript turbo module definition generates the same output (#34251)
Summary: Flow and TypeScript are two very similar programming languages. They are both recognizable as inputs for turbo module codegen. It is reasonable to require equivalent Flow and TypeScript turbo module definition generates the same output. I add some test cases to ensure this. Glad that no functional inconsistency is found here. ## Changelog [General] [Changed] - codegen: ensure equivalent Flow and TypeScript TM definition generates the same output Pull Request resolved: #34251 Test Plan: `yarn jest` passed in `packages/react-native-codegen` Reviewed By: dmitryrykun Differential Revision: D38116756 Pulled By: cipolleschi fbshipit-source-id: 476adbd171a35813923f2020c826d21b1fc2eeed
1 parent e441504 commit 0ce4ea2

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @emails oncall+react_native
8+
* @format
9+
*/
10+
11+
'use strict';
12+
13+
const {compareSnaps, compareTsArraySnaps} = require('../compareSnaps.js');
14+
15+
const flowFixtures = require('../../flow/components/__test_fixtures__/fixtures.js');
16+
const flowSnaps = require('../../../../src/parsers/flow/components/__tests__/__snapshots__/component-parser-test.js.snap');
17+
const tsFixtures = require('../../typescript/components/__test_fixtures__/fixtures.js');
18+
const tsSnaps = require('../../../../src/parsers/typescript/components/__tests__/__snapshots__/typescript-component-parser-test.js.snap');
19+
const tsExtraCases = ['ARRAY2_PROP_TYPES_NO_EVENTS'];
20+
21+
compareSnaps(flowFixtures, flowSnaps, [], tsFixtures, tsSnaps, tsExtraCases);
22+
compareTsArraySnaps(tsSnaps, tsExtraCases);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @emails oncall+react_native
8+
* @format
9+
*/
10+
11+
'use strict';
12+
13+
const {compareSnaps, compareTsArraySnaps} = require('../compareSnaps.js');
14+
15+
const flowFixtures = require('../../flow/modules/__test_fixtures__/fixtures.js');
16+
const flowSnaps = require('../../../../src/parsers/flow/modules/__tests__/__snapshots__/module-parser-snapshot-test.js.snap');
17+
const tsFixtures = require('../../typescript/modules/__test_fixtures__/fixtures.js');
18+
const tsSnaps = require('../../../../src/parsers/typescript/modules/__tests__/__snapshots__/typescript-module-parser-snapshot-test.js.snap');
19+
const tsExtraCases = [
20+
'NATIVE_MODULE_WITH_ARRAY2_WITH_ALIAS',
21+
'NATIVE_MODULE_WITH_ARRAY2_WITH_UNION_AND_TOUPLE',
22+
'NATIVE_MODULE_WITH_BASIC_ARRAY2',
23+
'NATIVE_MODULE_WITH_COMPLEX_ARRAY2',
24+
];
25+
26+
compareSnaps(flowFixtures, flowSnaps, [], tsFixtures, tsSnaps, tsExtraCases);
27+
compareTsArraySnaps(tsSnaps, tsExtraCases);
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @emails oncall+react_native
8+
* @format
9+
*/
10+
11+
'use strict';
12+
13+
function compareSnaps(
14+
flowFixtures,
15+
flowSnaps,
16+
flowExtraCases,
17+
tsFixtures,
18+
tsSnaps,
19+
tsExtraCases,
20+
) {
21+
const flowCases = Object.keys(flowFixtures).sort();
22+
const tsCases = Object.keys(tsFixtures).sort();
23+
const commonCases = flowCases.filter(name => tsCases.indexOf(name) !== -1);
24+
25+
describe('RN Codegen Parsers', () => {
26+
it('should not unintentionally contains test case for Flow but not for TypeScript', () => {
27+
expect(
28+
flowCases.filter(name => commonCases.indexOf(name) === -1),
29+
).toEqual(flowExtraCases);
30+
});
31+
32+
it('should not unintentionally contains test case for TypeScript but not for Flow', () => {
33+
expect(tsCases.filter(name => commonCases.indexOf(name) === -1)).toEqual(
34+
tsExtraCases,
35+
);
36+
});
37+
38+
for (const commonCase of commonCases) {
39+
it(`should generate the same snap from Flow and TypeScript for fixture ${commonCase}`, () => {
40+
expect(
41+
flowSnaps[
42+
`RN Codegen Flow Parser can generate fixture ${commonCase}`
43+
],
44+
).toEqual(
45+
tsSnaps[
46+
`RN Codegen TypeScript Parser can generate fixture ${commonCase}`
47+
],
48+
);
49+
});
50+
}
51+
});
52+
}
53+
54+
function compareTsArraySnaps(tsSnaps, tsExtraCases) {
55+
for (const array2Case of tsExtraCases.filter(
56+
name => name.indexOf('ARRAY2') !== -1,
57+
)) {
58+
const arrayCase = array2Case.replace('ARRAY2', 'ARRAY');
59+
it(`should generate the same snap from fixture ${arrayCase} and ${array2Case}`, () => {
60+
expect(
61+
tsSnaps[
62+
`RN Codegen TypeScript Parser can generate fixture ${arrayCase}`
63+
],
64+
).toEqual(
65+
tsSnaps[
66+
`RN Codegen TypeScript Parser can generate fixture ${array2Case}`
67+
],
68+
);
69+
});
70+
}
71+
}
72+
73+
module.exports = {
74+
compareSnaps,
75+
compareTsArraySnaps,
76+
};

0 commit comments

Comments
 (0)