From 7b9e446bb2d527c6fc099893c81de8a187a6b013 Mon Sep 17 00:00:00 2001 From: Antoine Doubovetzky Date: Thu, 30 Mar 2023 13:53:46 +0200 Subject: [PATCH 1/3] refactor(codegen): extract throwIfConfigNotfound error --- .../src/parsers/__tests__/error-utils-test.js | 23 +++++++++++++++++++ .../src/parsers/error-utils.js | 7 ++++++ .../src/parsers/flow/components/index.js | 6 ++--- .../parsers/typescript/components/index.js | 6 ++--- 4 files changed, 36 insertions(+), 6 deletions(-) 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 index 887fb852d634f2..4f4ebde97e6bcb 100644 --- a/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js +++ b/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js @@ -11,6 +11,8 @@ 'use strict'; +import {throwIfConfigNotfound} from '../error-utils'; + const { throwIfModuleInterfaceNotFound, throwIfMoreThanOneModuleRegistryCalls, @@ -849,3 +851,24 @@ describe('throwIfMoreThanOneCodegenNativecommands', () => { }).not.toThrow(); }); }); + +describe('throwIfConfigNotfound', () => { + it('throws an error if config is not found', () => { + const configs: Array<{[string]: string}> = []; + expect(() => { + throwIfConfigNotfound(configs); + }).toThrowError('Could not find component config for native component'); + }); + + it('does not throw an error if config contains some elements', () => { + const configs: Array<{[string]: string}> = [ + { + propsTypeName: 'testPropsTypeName', + componentName: 'testComponentName', + }, + ]; + expect(() => { + throwIfConfigNotfound(configs); + }).not.toThrow(); + }); +}); diff --git a/packages/react-native-codegen/src/parsers/error-utils.js b/packages/react-native-codegen/src/parsers/error-utils.js index d6d286ea3f64c9..158ed078017072 100644 --- a/packages/react-native-codegen/src/parsers/error-utils.js +++ b/packages/react-native-codegen/src/parsers/error-utils.js @@ -298,6 +298,12 @@ function throwIfMoreThanOneCodegenNativecommands( } } +function throwIfConfigNotfound(foundConfigs: Array<{[string]: string}>) { + if (foundConfigs.length === 0) { + throw new Error('Could not find component config for native component'); + } +} + module.exports = { throwIfModuleInterfaceIsMisnamed, throwIfUnsupportedFunctionReturnTypeAnnotationParserError, @@ -316,4 +322,5 @@ module.exports = { throwIfPartialNotAnnotatingTypeParameter, throwIfPartialWithMoreParameter, throwIfMoreThanOneCodegenNativecommands, + throwIfConfigNotfound, }; diff --git a/packages/react-native-codegen/src/parsers/flow/components/index.js b/packages/react-native-codegen/src/parsers/flow/components/index.js index 30c613e8962e14..8b0fddde060809 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/index.js +++ b/packages/react-native-codegen/src/parsers/flow/components/index.js @@ -12,6 +12,8 @@ import type {Parser} from '../../parser'; import type {ComponentSchemaBuilderConfig} from '../../schema.js'; +import {throwIfConfigNotfound} from '../../error-utils'; + const {getCommands} = require('./commands'); const {getEvents} = require('./events'); const {getExtendsProps, removeKnownExtends} = require('./extends'); @@ -39,9 +41,7 @@ function findComponentConfig(ast: $FlowFixMe, parser: Parser) { findNativeComponentType(statement, foundConfigs, parser); }); - if (foundConfigs.length === 0) { - throw new Error('Could not find component config for native component'); - } + throwIfConfigNotfound(foundConfigs); if (foundConfigs.length > 1) { throw new Error('Only one component is supported per file'); } diff --git a/packages/react-native-codegen/src/parsers/typescript/components/index.js b/packages/react-native-codegen/src/parsers/typescript/components/index.js index 20c24bc0a79d7a..aa081555eb841d 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/index.js @@ -13,6 +13,8 @@ import type {ExtendsPropsShape} from '../../../CodegenSchema.js'; import type {Parser} from '../../parser'; import type {ComponentSchemaBuilderConfig} from '../../schema.js'; +import {throwIfConfigNotfound} from '../../error-utils'; + const {getCommands} = require('./commands'); const {getEvents} = require('./events'); const {categorizeProps} = require('./extends'); @@ -40,9 +42,7 @@ function findComponentConfig(ast: $FlowFixMe, parser: Parser) { findNativeComponentType(statement, foundConfigs, parser), ); - if (foundConfigs.length === 0) { - throw new Error('Could not find component config for native component'); - } + throwIfConfigNotfound(foundConfigs); if (foundConfigs.length > 1) { throw new Error('Only one component is supported per file'); } From 5060422526bdc697a38ce9a66dd23f19186ec1ff Mon Sep 17 00:00:00 2001 From: Antoine Doubovetzky Date: Thu, 30 Mar 2023 13:56:34 +0200 Subject: [PATCH 2/3] refactor(codegen): extract throwIfMoreThanOneConfig error --- .../src/parsers/__tests__/error-utils-test.js | 32 ++++++++++++++++++- .../src/parsers/error-utils.js | 7 ++++ .../src/parsers/flow/components/index.js | 9 +++--- .../parsers/typescript/components/index.js | 9 +++--- 4 files changed, 48 insertions(+), 9 deletions(-) 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 index 4f4ebde97e6bcb..7b7a0bd9ef9ad2 100644 --- a/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js +++ b/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js @@ -11,7 +11,7 @@ 'use strict'; -import {throwIfConfigNotfound} from '../error-utils'; +import {throwIfConfigNotfound, throwIfMoreThanOneConfig} from '../error-utils'; const { throwIfModuleInterfaceNotFound, @@ -872,3 +872,33 @@ describe('throwIfConfigNotfound', () => { }).not.toThrow(); }); }); + +describe('throwIfMoreThanOneConfig', () => { + it('throws an error if config is not found', () => { + const configs: Array<{[string]: string}> = [ + { + propsTypeName: 'testPropsTypeName1', + componentName: 'testComponentName1', + }, + { + propsTypeName: 'testPropsTypeName2', + componentName: 'testComponentName2', + }, + ]; + expect(() => { + throwIfMoreThanOneConfig(configs); + }).toThrowError('Only one component is supported per file'); + }); + + it('does not throw an error if config contains some elements', () => { + const configs: Array<{[string]: string}> = [ + { + propsTypeName: 'testPropsTypeName', + componentName: 'testComponentName', + }, + ]; + expect(() => { + throwIfMoreThanOneConfig(configs); + }).not.toThrow(); + }); +}); diff --git a/packages/react-native-codegen/src/parsers/error-utils.js b/packages/react-native-codegen/src/parsers/error-utils.js index 158ed078017072..87adc6a7af1bee 100644 --- a/packages/react-native-codegen/src/parsers/error-utils.js +++ b/packages/react-native-codegen/src/parsers/error-utils.js @@ -304,6 +304,12 @@ function throwIfConfigNotfound(foundConfigs: Array<{[string]: string}>) { } } +function throwIfMoreThanOneConfig(foundConfigs: Array<{[string]: string}>) { + if (foundConfigs.length > 1) { + throw new Error('Only one component is supported per file'); + } +} + module.exports = { throwIfModuleInterfaceIsMisnamed, throwIfUnsupportedFunctionReturnTypeAnnotationParserError, @@ -323,4 +329,5 @@ module.exports = { throwIfPartialWithMoreParameter, throwIfMoreThanOneCodegenNativecommands, throwIfConfigNotfound, + throwIfMoreThanOneConfig, }; diff --git a/packages/react-native-codegen/src/parsers/flow/components/index.js b/packages/react-native-codegen/src/parsers/flow/components/index.js index 8b0fddde060809..bb990ad0aa1dd2 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/index.js +++ b/packages/react-native-codegen/src/parsers/flow/components/index.js @@ -12,7 +12,10 @@ import type {Parser} from '../../parser'; import type {ComponentSchemaBuilderConfig} from '../../schema.js'; -import {throwIfConfigNotfound} from '../../error-utils'; +import { + throwIfConfigNotfound, + throwIfMoreThanOneConfig, +} from '../../error-utils'; const {getCommands} = require('./commands'); const {getEvents} = require('./events'); @@ -42,9 +45,7 @@ function findComponentConfig(ast: $FlowFixMe, parser: Parser) { }); throwIfConfigNotfound(foundConfigs); - if (foundConfigs.length > 1) { - throw new Error('Only one component is supported per file'); - } + throwIfMoreThanOneConfig(foundConfigs); const foundConfig = foundConfigs[0]; diff --git a/packages/react-native-codegen/src/parsers/typescript/components/index.js b/packages/react-native-codegen/src/parsers/typescript/components/index.js index aa081555eb841d..ca51be5139853f 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/index.js @@ -13,7 +13,10 @@ import type {ExtendsPropsShape} from '../../../CodegenSchema.js'; import type {Parser} from '../../parser'; import type {ComponentSchemaBuilderConfig} from '../../schema.js'; -import {throwIfConfigNotfound} from '../../error-utils'; +import { + throwIfConfigNotfound, + throwIfMoreThanOneConfig, +} from '../../error-utils'; const {getCommands} = require('./commands'); const {getEvents} = require('./events'); @@ -43,9 +46,7 @@ function findComponentConfig(ast: $FlowFixMe, parser: Parser) { ); throwIfConfigNotfound(foundConfigs); - if (foundConfigs.length > 1) { - throw new Error('Only one component is supported per file'); - } + throwIfMoreThanOneConfig(foundConfigs); const foundConfig = foundConfigs[0]; From 33568f424c7e5197c3d325c44d1dbac857511c28 Mon Sep 17 00:00:00 2001 From: Antoine Doubovetzky Date: Thu, 30 Mar 2023 17:33:08 +0200 Subject: [PATCH 3/3] Replace import with require --- .../src/parsers/__tests__/error-utils-test.js | 5 ++++- .../src/parsers/flow/components/index.js | 9 ++++----- .../src/parsers/typescript/components/index.js | 9 ++++----- 3 files changed, 12 insertions(+), 11 deletions(-) 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 index 7b7a0bd9ef9ad2..4806672ad7c8ae 100644 --- a/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js +++ b/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js @@ -11,7 +11,10 @@ 'use strict'; -import {throwIfConfigNotfound, throwIfMoreThanOneConfig} from '../error-utils'; +const { + throwIfConfigNotfound, + throwIfMoreThanOneConfig, +} = require('../error-utils'); const { throwIfModuleInterfaceNotFound, diff --git a/packages/react-native-codegen/src/parsers/flow/components/index.js b/packages/react-native-codegen/src/parsers/flow/components/index.js index bb990ad0aa1dd2..a216417f9cbc95 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/index.js +++ b/packages/react-native-codegen/src/parsers/flow/components/index.js @@ -12,11 +12,6 @@ import type {Parser} from '../../parser'; import type {ComponentSchemaBuilderConfig} from '../../schema.js'; -import { - throwIfConfigNotfound, - throwIfMoreThanOneConfig, -} from '../../error-utils'; - const {getCommands} = require('./commands'); const {getEvents} = require('./events'); const {getExtendsProps, removeKnownExtends} = require('./extends'); @@ -31,6 +26,10 @@ const { getOptions, getCommandTypeNameAndOptionsExpression, } = require('../../parsers-commons'); +const { + throwIfConfigNotfound, + throwIfMoreThanOneConfig, +} = require('../../error-utils'); // $FlowFixMe[signature-verification-failure] there's no flowtype for AST function findComponentConfig(ast: $FlowFixMe, parser: Parser) { diff --git a/packages/react-native-codegen/src/parsers/typescript/components/index.js b/packages/react-native-codegen/src/parsers/typescript/components/index.js index ca51be5139853f..4ebca03d663550 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/index.js @@ -13,11 +13,6 @@ import type {ExtendsPropsShape} from '../../../CodegenSchema.js'; import type {Parser} from '../../parser'; import type {ComponentSchemaBuilderConfig} from '../../schema.js'; -import { - throwIfConfigNotfound, - throwIfMoreThanOneConfig, -} from '../../error-utils'; - const {getCommands} = require('./commands'); const {getEvents} = require('./events'); const {categorizeProps} = require('./extends'); @@ -32,6 +27,10 @@ const { getOptions, getCommandTypeNameAndOptionsExpression, } = require('../../parsers-commons'); +const { + throwIfConfigNotfound, + throwIfMoreThanOneConfig, +} = require('../../error-utils'); // $FlowFixMe[signature-verification-failure] TODO(T108222691): Use flow-types for @babel/parser function findComponentConfig(ast: $FlowFixMe, parser: Parser) {