From 0ce4ea2bf59c9457d6b6c4a18c65bcb561e7853b Mon Sep 17 00:00:00 2001 From: "Zihan Chen (MSFT)" <53799235+ZihanChen-MSFT@users.noreply.github.com> Date: Wed, 27 Jul 2022 08:36:02 -0700 Subject: [PATCH] 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: https://github.com/facebook/react-native/pull/34251 Test Plan: `yarn jest` passed in `packages/react-native-codegen` Reviewed By: dmitryrykun Differential Revision: D38116756 Pulled By: cipolleschi fbshipit-source-id: 476adbd171a35813923f2020c826d21b1fc2eeed --- .../__tests__/checkComponentSnaps-test.js | 22 ++++++ .../__tests__/checkModuleSnaps-test.js | 27 +++++++ .../src/parsers/consistency/compareSnaps.js | 76 +++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 packages/react-native-codegen/src/parsers/consistency/__tests__/checkComponentSnaps-test.js create mode 100644 packages/react-native-codegen/src/parsers/consistency/__tests__/checkModuleSnaps-test.js create mode 100644 packages/react-native-codegen/src/parsers/consistency/compareSnaps.js diff --git a/packages/react-native-codegen/src/parsers/consistency/__tests__/checkComponentSnaps-test.js b/packages/react-native-codegen/src/parsers/consistency/__tests__/checkComponentSnaps-test.js new file mode 100644 index 00000000000000..17f921ebd61c94 --- /dev/null +++ b/packages/react-native-codegen/src/parsers/consistency/__tests__/checkComponentSnaps-test.js @@ -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); diff --git a/packages/react-native-codegen/src/parsers/consistency/__tests__/checkModuleSnaps-test.js b/packages/react-native-codegen/src/parsers/consistency/__tests__/checkModuleSnaps-test.js new file mode 100644 index 00000000000000..fcfaa3ed36b6d3 --- /dev/null +++ b/packages/react-native-codegen/src/parsers/consistency/__tests__/checkModuleSnaps-test.js @@ -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); diff --git a/packages/react-native-codegen/src/parsers/consistency/compareSnaps.js b/packages/react-native-codegen/src/parsers/consistency/compareSnaps.js new file mode 100644 index 00000000000000..2ef09816a841e2 --- /dev/null +++ b/packages/react-native-codegen/src/parsers/consistency/compareSnaps.js @@ -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, +};