Skip to content

Commit

Permalink
Ensure equivalent Flow and TypeScript turbo module definition generat…
Browse files Browse the repository at this point in the history
…es 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
  • Loading branch information
ZihanChen-MSFT authored and facebook-github-bot committed Jul 27, 2022
1 parent e441504 commit 0ce4ea2
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails oncall+react_native
* @format
*/

'use strict';

const {compareSnaps, compareTsArraySnaps} = require('../compareSnaps.js');

const flowFixtures = require('../../flow/components/__test_fixtures__/fixtures.js');
const flowSnaps = require('../../../../src/parsers/flow/components/__tests__/__snapshots__/component-parser-test.js.snap');
const tsFixtures = require('../../typescript/components/__test_fixtures__/fixtures.js');
const tsSnaps = require('../../../../src/parsers/typescript/components/__tests__/__snapshots__/typescript-component-parser-test.js.snap');
const tsExtraCases = ['ARRAY2_PROP_TYPES_NO_EVENTS'];

compareSnaps(flowFixtures, flowSnaps, [], tsFixtures, tsSnaps, tsExtraCases);
compareTsArraySnaps(tsSnaps, tsExtraCases);
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails oncall+react_native
* @format
*/

'use strict';

const {compareSnaps, compareTsArraySnaps} = require('../compareSnaps.js');

const flowFixtures = require('../../flow/modules/__test_fixtures__/fixtures.js');
const flowSnaps = require('../../../../src/parsers/flow/modules/__tests__/__snapshots__/module-parser-snapshot-test.js.snap');
const tsFixtures = require('../../typescript/modules/__test_fixtures__/fixtures.js');
const tsSnaps = require('../../../../src/parsers/typescript/modules/__tests__/__snapshots__/typescript-module-parser-snapshot-test.js.snap');
const tsExtraCases = [
'NATIVE_MODULE_WITH_ARRAY2_WITH_ALIAS',
'NATIVE_MODULE_WITH_ARRAY2_WITH_UNION_AND_TOUPLE',
'NATIVE_MODULE_WITH_BASIC_ARRAY2',
'NATIVE_MODULE_WITH_COMPLEX_ARRAY2',
];

compareSnaps(flowFixtures, flowSnaps, [], tsFixtures, tsSnaps, tsExtraCases);
compareTsArraySnaps(tsSnaps, tsExtraCases);
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails oncall+react_native
* @format
*/

'use strict';

function compareSnaps(
flowFixtures,
flowSnaps,
flowExtraCases,
tsFixtures,
tsSnaps,
tsExtraCases,
) {
const flowCases = Object.keys(flowFixtures).sort();
const tsCases = Object.keys(tsFixtures).sort();
const commonCases = flowCases.filter(name => tsCases.indexOf(name) !== -1);

describe('RN Codegen Parsers', () => {
it('should not unintentionally contains test case for Flow but not for TypeScript', () => {
expect(
flowCases.filter(name => commonCases.indexOf(name) === -1),
).toEqual(flowExtraCases);
});

it('should not unintentionally contains test case for TypeScript but not for Flow', () => {
expect(tsCases.filter(name => commonCases.indexOf(name) === -1)).toEqual(
tsExtraCases,
);
});

for (const commonCase of commonCases) {
it(`should generate the same snap from Flow and TypeScript for fixture ${commonCase}`, () => {
expect(
flowSnaps[
`RN Codegen Flow Parser can generate fixture ${commonCase}`
],
).toEqual(
tsSnaps[
`RN Codegen TypeScript Parser can generate fixture ${commonCase}`
],
);
});
}
});
}

function compareTsArraySnaps(tsSnaps, tsExtraCases) {
for (const array2Case of tsExtraCases.filter(
name => name.indexOf('ARRAY2') !== -1,
)) {
const arrayCase = array2Case.replace('ARRAY2', 'ARRAY');
it(`should generate the same snap from fixture ${arrayCase} and ${array2Case}`, () => {
expect(
tsSnaps[
`RN Codegen TypeScript Parser can generate fixture ${arrayCase}`
],
).toEqual(
tsSnaps[
`RN Codegen TypeScript Parser can generate fixture ${array2Case}`
],
);
});
}
}

module.exports = {
compareSnaps,
compareTsArraySnaps,
};

0 comments on commit 0ce4ea2

Please sign in to comment.