From c9338c459709b7c911370dd3b10d71a966b700ce Mon Sep 17 00:00:00 2001 From: Gabriel Donadel Dall'Agnol Date: Fri, 14 Oct 2022 03:09:51 -0700 Subject: [PATCH] chore: Extract codegen ModuleInterfaceNotFoundParserError in a separate function (#34922) Summary: This PR extracts the codegen `ModuleInterfaceNotFoundParserError` exception into a separate function inside `error-utils.js` so that it can be used by both Flow and Typescript parsers as requested on https://github.com/facebook/react-native/issues/34872. This also adds unit tests to the new `throwIfModuleInterfaceNotFound` function. ## Changelog [Internal] [Changed] - Extract codegen `ModuleInterfaceNotFoundParserError` in a separate function that can de used by Flow and TypeScript parsers Pull Request resolved: https://github.com/facebook/react-native/pull/34922 Test Plan: Run `yarn jest react-native-codegen` and ensure CI is green ![image](https://user-images.githubusercontent.com/11707729/194876017-8e98e3d2-4518-4cbe-b7f0-b77a54060b1e.png) Reviewed By: dmytrorykun Differential Revision: D40296533 Pulled By: cipolleschi fbshipit-source-id: 205469b15337f6c3847936a6ef5427b5a9e08665 --- .../src/parsers/__tests__/error-utils-test.js | 37 +++++++++++++++++++ .../src/parsers/error-utils.js | 33 +++++++++++++++++ .../src/parsers/flow/modules/index.js | 16 ++++---- .../src/parsers/typescript/modules/index.js | 15 ++++---- 4 files changed, 85 insertions(+), 16 deletions(-) create mode 100644 packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js create mode 100644 packages/react-native-codegen/src/parsers/error-utils.js diff --git a/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js b/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js new file mode 100644 index 00000000000000..86b5281435f0bb --- /dev/null +++ b/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js @@ -0,0 +1,37 @@ +/** + * 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. + * + * @format + * @flow strict-local + * @oncall react_native + */ + +'use strict'; + +const {throwIfModuleInterfaceNotFound} = require('../error-utils'); +const {ModuleInterfaceNotFoundParserError} = require('../errors'); + +describe('throwIfModuleInterfaceNotFound', () => { + it('throw error if there are zero module specs', () => { + const nativeModuleName = 'moduleName'; + const specId = {name: 'Name'}; + const parserType = 'TypeScript'; + + expect(() => { + throwIfModuleInterfaceNotFound(0, nativeModuleName, specId, parserType); + }).toThrow(ModuleInterfaceNotFoundParserError); + }); + + it("don't throw error if there is at least one module spec", () => { + const nativeModuleName = 'moduleName'; + const specId = {name: 'Spec'}; + const parserType = 'Flow'; + + expect(() => { + throwIfModuleInterfaceNotFound(1, nativeModuleName, specId, parserType); + }).not.toThrow(ModuleInterfaceNotFoundParserError); + }); +}); diff --git a/packages/react-native-codegen/src/parsers/error-utils.js b/packages/react-native-codegen/src/parsers/error-utils.js new file mode 100644 index 00000000000000..bda360da020f86 --- /dev/null +++ b/packages/react-native-codegen/src/parsers/error-utils.js @@ -0,0 +1,33 @@ +/** + * 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-local + * @format + */ + +'use strict'; + +import type {ParserType} from './errors'; +const {ModuleInterfaceNotFoundParserError} = require('./errors.js'); + +function throwIfModuleInterfaceNotFound( + numberOfModuleSpecs: number, + nativeModuleName: string, + ast: $FlowFixMe, + parserType: ParserType, +) { + if (numberOfModuleSpecs === 0) { + throw new ModuleInterfaceNotFoundParserError( + nativeModuleName, + ast, + parserType, + ); + } +} + +module.exports = { + throwIfModuleInterfaceNotFound, +}; 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 169a3aeb9f1a96..9c211785269c2f 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -50,7 +50,6 @@ const { } = require('../../parsers-primitives'); const { MisnamedModuleInterfaceParserError, - ModuleInterfaceNotFoundParserError, MoreThanOneModuleInterfaceParserError, UnnamedFunctionParamParserError, UnsupportedArrayElementTypeAnnotationParserError, @@ -71,6 +70,8 @@ const { IncorrectModuleRegistryCallArgumentTypeParserError, } = require('../../errors.js'); +const {throwIfModuleInterfaceNotFound} = require('../../error-utils'); + const language = 'Flow'; function nullGuard(fn: () => T): ?T { @@ -578,13 +579,12 @@ function buildModuleSchema( isModuleInterface, ); - if (moduleSpecs.length === 0) { - throw new ModuleInterfaceNotFoundParserError( - hasteModuleName, - ast, - language, - ); - } + throwIfModuleInterfaceNotFound( + moduleSpecs.length, + hasteModuleName, + ast, + language, + ); if (moduleSpecs.length > 1) { throw new MoreThanOneModuleInterfaceParserError( 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 522eb134b36143..ff33f6ba96fe69 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -50,7 +50,6 @@ const { } = require('../../parsers-primitives'); const { MisnamedModuleInterfaceParserError, - ModuleInterfaceNotFoundParserError, MoreThanOneModuleInterfaceParserError, UnnamedFunctionParamParserError, UnsupportedArrayElementTypeAnnotationParserError, @@ -70,6 +69,7 @@ const { IncorrectModuleRegistryCallArityParserError, IncorrectModuleRegistryCallArgumentTypeParserError, } = require('../../errors.js'); +const {throwIfModuleInterfaceNotFound} = require('../../error-utils'); const language = 'TypeScript'; @@ -612,13 +612,12 @@ function buildModuleSchema( isModuleInterface, ); - if (moduleSpecs.length === 0) { - throw new ModuleInterfaceNotFoundParserError( - hasteModuleName, - ast, - language, - ); - } + throwIfModuleInterfaceNotFound( + moduleSpecs.length, + hasteModuleName, + ast, + language, + ); if (moduleSpecs.length > 1) { throw new MoreThanOneModuleInterfaceParserError(