From 24efebf83d638ef75670646dbadef540d3870648 Mon Sep 17 00:00:00 2001 From: Antoine Doubovetzky Date: Sat, 8 Oct 2022 08:22:58 -0700 Subject: [PATCH] Extracts modules/utils.js files from the flow and typescript folders in parsers-commons (#34898) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: This PR is a task from https://github.com/facebook/react-native/issues/34872 _Extract the modules/utils.js from the flow and typescript folders in a shared parsers-commons.js file. Then, have the two parsers use the same wrapModuleSchema function for modules._ (`wrapModuleSchema` is a copy-paste mistake, in this case it is `wrapNullable` and `unwrapNullable`) ## Changelog [Internal] [Changed] - Extracts Codegen's modules/utils.js files from the flow and typescript folders in parsers-commons Pull Request resolved: https://github.com/facebook/react-native/pull/34898 Test Plan: I ran `yarn jest react-native-codegen`: Capture d’écran 2022-10-07 à 21 29 48 Reviewed By: cipolleschi Differential Revision: D40193740 Pulled By: cipolleschi fbshipit-source-id: 02cbacc215fe5dd9bdd0839d8796587ab2821906 --- .../generators/modules/GenerateModuleCpp.js | 2 +- .../src/generators/modules/GenerateModuleH.js | 2 +- .../modules/GenerateModuleJavaSpec.js | 2 +- .../modules/GenerateModuleJniCpp.js | 2 +- .../GenerateModuleObjCpp/StructCollector.js | 2 +- .../header/serializeConstantsStruct.js | 2 +- .../header/serializeRegularStruct.js | 2 +- .../GenerateModuleObjCpp/serializeMethod.js | 2 +- .../__tests__/module-parser-e2e-test.js | 2 +- .../src/parsers/flow/modules/index.js | 2 +- .../src/parsers/flow/modules/utils.js | 45 ------------------- .../src/parsers/parsers-commons.js | 33 +++++++++++++- .../typescript-module-parser-e2e-test.js | 2 +- .../src/parsers/typescript/modules/index.js | 2 +- .../src/parsers/typescript/modules/utils.js | 45 ------------------- 15 files changed, 44 insertions(+), 103 deletions(-) delete mode 100644 packages/react-native-codegen/src/parsers/flow/modules/utils.js delete mode 100644 packages/react-native-codegen/src/parsers/typescript/modules/utils.js diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleCpp.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleCpp.js index b32b60c6f0e45a..1846ebc682677a 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleCpp.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleCpp.js @@ -22,7 +22,7 @@ import type { import type {AliasResolver} from './Utils'; const {createAliasResolver, getModules} = require('./Utils'); -const {unwrapNullable} = require('../../parsers/flow/modules/utils'); +const {unwrapNullable} = require('../../parsers/parsers-commons'); type FilesOutput = Map; diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleH.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleH.js index 7c80bad095e1f8..dad386c4c9e7a9 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleH.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleH.js @@ -21,7 +21,7 @@ import type { import type {AliasResolver} from './Utils'; const {createAliasResolver, getModules} = require('./Utils'); const {indent} = require('../Utils'); -const {unwrapNullable} = require('../../parsers/flow/modules/utils'); +const {unwrapNullable} = require('../../parsers/parsers-commons'); type FilesOutput = Map; diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js index 32443237f3a8e2..0715932adbbeb3 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js @@ -22,7 +22,7 @@ import type { import type {AliasResolver} from './Utils'; const {createAliasResolver, getModules} = require('./Utils'); -const {unwrapNullable} = require('../../parsers/flow/modules/utils'); +const {unwrapNullable} = require('../../parsers/parsers-commons'); type FilesOutput = Map; diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleJniCpp.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleJniCpp.js index aa56ed608c872b..2703c2517227ad 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleJniCpp.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleJniCpp.js @@ -22,7 +22,7 @@ import type { import type {AliasResolver} from './Utils'; const {createAliasResolver, getModules} = require('./Utils'); -const {unwrapNullable} = require('../../parsers/flow/modules/utils'); +const {unwrapNullable} = require('../../parsers/parsers-commons'); type FilesOutput = Map; diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/StructCollector.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/StructCollector.js index a30fa5400ecf70..d3926812c3bd0d 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/StructCollector.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/StructCollector.js @@ -32,7 +32,7 @@ const {capitalize} = require('../../Utils'); const { unwrapNullable, wrapNullable, -} = require('../../../parsers/flow/modules/utils'); +} = require('../../../parsers/parsers-commons'); type StructContext = 'CONSTANTS' | 'REGULAR'; diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/header/serializeConstantsStruct.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/header/serializeConstantsStruct.js index 019724049a15bc..d030fdcf442a9a 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/header/serializeConstantsStruct.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/header/serializeConstantsStruct.js @@ -17,7 +17,7 @@ import type {Nullable} from '../../../../CodegenSchema'; import type {StructTypeAnnotation, ConstantsStruct} from '../StructCollector'; import type {StructSerilizationOutput} from './serializeStruct'; -const {unwrapNullable} = require('../../../../parsers/flow/modules/utils'); +const {unwrapNullable} = require('../../../../parsers/parsers-commons'); const StructTemplate = ({ hasteModuleName, diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/header/serializeRegularStruct.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/header/serializeRegularStruct.js index e17c30005d411b..9cc4aee79ab486 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/header/serializeRegularStruct.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/header/serializeRegularStruct.js @@ -17,7 +17,7 @@ import type {Nullable} from '../../../../CodegenSchema'; import type {StructTypeAnnotation, RegularStruct} from '../StructCollector'; import type {StructSerilizationOutput} from './serializeStruct'; -const {unwrapNullable} = require('../../../../parsers/flow/modules/utils'); +const {unwrapNullable} = require('../../../../parsers/parsers-commons'); const StructTemplate = ({ hasteModuleName, diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js index ead5482a824d4b..91bcba56ec2ef9 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js @@ -27,7 +27,7 @@ const {capitalize} = require('../../Utils'); const { wrapNullable, unwrapNullable, -} = require('../../../parsers/flow/modules/utils'); +} = require('../../../parsers/parsers-commons'); const ProtocolMethodTemplate = ({ returnObjCType, diff --git a/packages/react-native-codegen/src/parsers/flow/modules/__tests__/module-parser-e2e-test.js b/packages/react-native-codegen/src/parsers/flow/modules/__tests__/module-parser-e2e-test.js index 25240c0a26beee..c40bbf8ecac3f4 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/__tests__/module-parser-e2e-test.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/__tests__/module-parser-e2e-test.js @@ -16,7 +16,7 @@ import type { } from '../../../../CodegenSchema'; const {parseString} = require('../../index.js'); -const {unwrapNullable} = require('../utils'); +const {unwrapNullable} = require('../../../parsers-commons'); const { UnsupportedFlowGenericParserError, UnsupportedFlowTypeAnnotationParserError, diff --git a/packages/react-native-codegen/src/parsers/flow/modules/index.js b/packages/react-native-codegen/src/parsers/flow/modules/index.js index 4ced736958f7e6..7150ab2296cec5 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -32,7 +32,7 @@ const { visit, isModuleRegistryCall, } = require('../utils.js'); -const {unwrapNullable, wrapNullable} = require('./utils'); +const {unwrapNullable, wrapNullable} = require('../../parsers-commons'); const { IncorrectlyParameterizedFlowGenericParserError, MisnamedModuleFlowInterfaceParserError, diff --git a/packages/react-native-codegen/src/parsers/flow/modules/utils.js b/packages/react-native-codegen/src/parsers/flow/modules/utils.js deleted file mode 100644 index b013f76c8ce59e..00000000000000 --- a/packages/react-native-codegen/src/parsers/flow/modules/utils.js +++ /dev/null @@ -1,45 +0,0 @@ -/** - * 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. - * - * @flow strict - * @format - */ - -'use strict'; - -import type { - NativeModuleTypeAnnotation, - Nullable, -} from '../../../CodegenSchema.js'; - -function unwrapNullable<+T: NativeModuleTypeAnnotation>( - x: Nullable, -): [T, boolean] { - if (x.type === 'NullableTypeAnnotation') { - return [x.typeAnnotation, true]; - } - - return [x, false]; -} - -function wrapNullable<+T: NativeModuleTypeAnnotation>( - nullable: boolean, - typeAnnotation: T, -): Nullable { - if (!nullable) { - return typeAnnotation; - } - - return { - type: 'NullableTypeAnnotation', - typeAnnotation, - }; -} - -module.exports = { - unwrapNullable, - wrapNullable, -}; diff --git a/packages/react-native-codegen/src/parsers/parsers-commons.js b/packages/react-native-codegen/src/parsers/parsers-commons.js index 3e856f4dd7332f..d77fbfacd724c1 100644 --- a/packages/react-native-codegen/src/parsers/parsers-commons.js +++ b/packages/react-native-codegen/src/parsers/parsers-commons.js @@ -10,7 +10,12 @@ 'use strict'; -import type {SchemaType, NativeModuleSchema} from '../CodegenSchema.js'; +import type { + SchemaType, + NativeModuleSchema, + NativeModuleTypeAnnotation, + Nullable, +} from '../CodegenSchema.js'; function wrapModuleSchema( nativeModuleSchema: NativeModuleSchema, @@ -23,6 +28,32 @@ function wrapModuleSchema( }; } +function unwrapNullable<+T: NativeModuleTypeAnnotation>( + x: Nullable, +): [T, boolean] { + if (x.type === 'NullableTypeAnnotation') { + return [x.typeAnnotation, true]; + } + + return [x, false]; +} + +function wrapNullable<+T: NativeModuleTypeAnnotation>( + nullable: boolean, + typeAnnotation: T, +): Nullable { + if (!nullable) { + return typeAnnotation; + } + + return { + type: 'NullableTypeAnnotation', + typeAnnotation, + }; +} + module.exports = { wrapModuleSchema, + unwrapNullable, + wrapNullable, }; diff --git a/packages/react-native-codegen/src/parsers/typescript/modules/__tests__/typescript-module-parser-e2e-test.js b/packages/react-native-codegen/src/parsers/typescript/modules/__tests__/typescript-module-parser-e2e-test.js index 99b7702f5fd640..17aa2b1dbc64bc 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/__tests__/typescript-module-parser-e2e-test.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/__tests__/typescript-module-parser-e2e-test.js @@ -16,7 +16,7 @@ import type { } from '../../../../CodegenSchema'; const {parseString} = require('../../index.js'); -const {unwrapNullable} = require('../utils'); +const {unwrapNullable} = require('../../../parsers-commons'); const { UnsupportedTypeScriptGenericParserError, UnsupportedTypeScriptTypeAnnotationParserError, diff --git a/packages/react-native-codegen/src/parsers/typescript/modules/index.js b/packages/react-native-codegen/src/parsers/typescript/modules/index.js index 893d6e69520fd1..76b8aaf3ed931c 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -32,7 +32,7 @@ const { visit, isModuleRegistryCall, } = require('../utils.js'); -const {unwrapNullable, wrapNullable} = require('./utils'); +const {unwrapNullable, wrapNullable} = require('../../parsers-commons'); const { IncorrectlyParameterizedTypeScriptGenericParserError, MisnamedModuleTypeScriptInterfaceParserError, diff --git a/packages/react-native-codegen/src/parsers/typescript/modules/utils.js b/packages/react-native-codegen/src/parsers/typescript/modules/utils.js deleted file mode 100644 index b013f76c8ce59e..00000000000000 --- a/packages/react-native-codegen/src/parsers/typescript/modules/utils.js +++ /dev/null @@ -1,45 +0,0 @@ -/** - * 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. - * - * @flow strict - * @format - */ - -'use strict'; - -import type { - NativeModuleTypeAnnotation, - Nullable, -} from '../../../CodegenSchema.js'; - -function unwrapNullable<+T: NativeModuleTypeAnnotation>( - x: Nullable, -): [T, boolean] { - if (x.type === 'NullableTypeAnnotation') { - return [x.typeAnnotation, true]; - } - - return [x, false]; -} - -function wrapNullable<+T: NativeModuleTypeAnnotation>( - nullable: boolean, - typeAnnotation: T, -): Nullable { - if (!nullable) { - return typeAnnotation; - } - - return { - type: 'NullableTypeAnnotation', - typeAnnotation, - }; -} - -module.exports = { - unwrapNullable, - wrapNullable, -};