Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Extract codegen ModuleInterfaceNotFoundParserError in a separa…
…te 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 #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: #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
- Loading branch information
1 parent
8422557
commit c9338c4
Showing
4 changed files
with
85 additions
and
16 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters