From c47600904a6c7711785c8dba2a0c8838bf9d6542 Mon Sep 17 00:00:00 2001 From: Tarun Chauhan Date: Thu, 9 Mar 2023 02:43:03 +0530 Subject: [PATCH] refactor(codegen): move getCommandOptions to parsers-commons --- .../parsers/__tests__/parsers-commons-test.js | 44 +++++++++++++++++++ .../src/parsers/flow/components/index.js | 5 ++- .../src/parsers/flow/components/options.js | 33 -------------- .../src/parsers/parsers-commons.js | 36 +++++++++++++++ .../parsers/typescript/components/index.js | 5 ++- .../parsers/typescript/components/options.js | 33 -------------- 6 files changed, 86 insertions(+), 70 deletions(-) diff --git a/packages/react-native-codegen/src/parsers/__tests__/parsers-commons-test.js b/packages/react-native-codegen/src/parsers/__tests__/parsers-commons-test.js index 7ab1c5d0787bdc..a71c2d3cc03dbb 100644 --- a/packages/react-native-codegen/src/parsers/__tests__/parsers-commons-test.js +++ b/packages/react-native-codegen/src/parsers/__tests__/parsers-commons-test.js @@ -20,6 +20,7 @@ import { buildSchemaFromConfigType, buildSchema, parseModuleName, + getCommandOptions, } from '../parsers-commons'; import type {ParserType} from '../errors'; @@ -1231,3 +1232,46 @@ describe('buildModuleSchema', () => { expect(schema).toEqual(schmeaMock); }); }); + +describe('getCommandOptions', () => { + it('returns null when commandOptionsExpression is null', () => { + const result = getCommandOptions(null); + expect(result).toBeNull(); + }); + + it('parses and returns command options correctly', () => { + const commandOptionsExpression = { + properties: [ + { + range: [], + loc: {}, + type: '', + key: { + name: 'hotspotUpdate', + loc: {}, + }, + value: { + elements: [ + { + value: 'value', + }, + ], + }, + }, + ], + }; + const result = getCommandOptions(commandOptionsExpression); + expect(result).toEqual({ + hotspotUpdate: ['value'], + }); + }); + + it('should throw an error if command options are not defined correctly', () => { + const commandOptionsExpression = { + properties: null, + }; + expect(() => getCommandOptions(commandOptionsExpression)).toThrowError( + 'Failed to parse command options, please check that they are defined correctly', + ); + }); +}); 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 e209ce4671f018..b99abff451c4b1 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/index.js +++ b/packages/react-native-codegen/src/parsers/flow/components/index.js @@ -11,15 +11,16 @@ 'use strict'; import type {Parser} from '../../parser'; import type {TypeDeclarationMap} from '../../utils'; -import type {CommandOptions} from './options'; +import type {CommandOptions} from '../../parsers-commons'; import type {ComponentSchemaBuilderConfig} from '../../schema.js'; const {getCommands} = require('./commands'); const {getEvents} = require('./events'); const {getExtendsProps, removeKnownExtends} = require('./extends'); -const {getCommandOptions, getOptions} = require('./options'); +const {getOptions} = require('./options'); const {getProps} = require('./props'); const {getProperties} = require('./componentsUtils.js'); +const {getCommandOptions} = require('../../parsers-commons'); /* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's * LTI update could not be added via codemod */ diff --git a/packages/react-native-codegen/src/parsers/flow/components/options.js b/packages/react-native-codegen/src/parsers/flow/components/options.js index a83ba528b467d8..385b928b17e9b8 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/options.js +++ b/packages/react-native-codegen/src/parsers/flow/components/options.js @@ -15,38 +15,6 @@ import type {OptionsShape} from '../../../CodegenSchema.js'; // $FlowFixMe[unclear-type] there's no flowtype for ASTs type OptionsAST = Object; -export type CommandOptions = $ReadOnly<{ - supportedCommands: $ReadOnlyArray, -}>; - -function getCommandOptions( - commandOptionsExpression: OptionsAST, -): ?CommandOptions { - if (commandOptionsExpression == null) { - return null; - } - - let foundOptions; - try { - foundOptions = commandOptionsExpression.properties.reduce( - (options, prop) => { - options[prop.key.name] = ( - (prop && prop.value && prop.value.elements) || - [] - ).map(element => element && element.value); - return options; - }, - {}, - ); - } catch (e) { - throw new Error( - 'Failed to parse command options, please check that they are defined correctly', - ); - } - - return foundOptions; -} - function getOptions(optionsExpression: OptionsAST): ?OptionsShape { if (!optionsExpression) { return null; @@ -82,6 +50,5 @@ function getOptions(optionsExpression: OptionsAST): ?OptionsShape { } module.exports = { - getCommandOptions, getOptions, }; diff --git a/packages/react-native-codegen/src/parsers/parsers-commons.js b/packages/react-native-codegen/src/parsers/parsers-commons.js index ae073d8c21136c..2f97619bd1d28d 100644 --- a/packages/react-native-codegen/src/parsers/parsers-commons.js +++ b/packages/react-native-codegen/src/parsers/parsers-commons.js @@ -61,6 +61,13 @@ const { const invariant = require('invariant'); +export type CommandOptions = $ReadOnly<{ + supportedCommands: $ReadOnlyArray, +}>; + +// $FlowFixMe[unclear-type] TODO(T108222691): Use flow-types for @babel/parser +type OptionsAST = Object; + function wrapModuleSchema( nativeModuleSchema: NativeModuleSchema, hasteModuleName: string, @@ -640,6 +647,34 @@ const buildModuleSchema = ( ); }; +function getCommandOptions( + commandOptionsExpression: OptionsAST, +): ?CommandOptions { + if (commandOptionsExpression == null) { + return null; + } + + let foundOptions; + try { + foundOptions = commandOptionsExpression.properties.reduce( + (options, prop) => { + options[prop.key.name] = ( + (prop && prop.value && prop.value.elements) || + [] + ).map(element => element && element.value); + return options; + }, + {}, + ); + } catch (e) { + throw new Error( + 'Failed to parse command options, please check that they are defined correctly', + ); + } + + return foundOptions; +} + module.exports = { wrapModuleSchema, unwrapNullable, @@ -653,4 +688,5 @@ module.exports = { buildSchema, parseModuleName, buildModuleSchema, + getCommandOptions, }; 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 2adaddeceeb60d..f6e1eb621317b9 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/index.js @@ -12,15 +12,16 @@ import type {ExtendsPropsShape} from '../../../CodegenSchema.js'; import type {Parser} from '../../parser'; import type {TypeDeclarationMap} from '../../utils'; -import type {CommandOptions} from './options'; +import type {CommandOptions} from '../../parsers-commons'; import type {ComponentSchemaBuilderConfig} from '../../schema.js'; const {getCommands} = require('./commands'); const {getEvents} = require('./events'); const {categorizeProps} = require('./extends'); -const {getCommandOptions, getOptions} = require('./options'); +const {getOptions} = require('./options'); const {getProps} = require('./props'); const {getProperties} = require('./componentsUtils.js'); +const {getCommandOptions} = require('../../parsers-commons'); /* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's * LTI update could not be added via codemod */ diff --git a/packages/react-native-codegen/src/parsers/typescript/components/options.js b/packages/react-native-codegen/src/parsers/typescript/components/options.js index 96ea8abc75b1d5..aef607d572aa90 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/options.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/options.js @@ -15,38 +15,6 @@ import type {OptionsShape} from '../../../CodegenSchema.js'; // $FlowFixMe[unclear-type] TODO(T108222691): Use flow-types for @babel/parser type OptionsAST = Object; -export type CommandOptions = $ReadOnly<{ - supportedCommands: $ReadOnlyArray, -}>; - -function getCommandOptions( - commandOptionsExpression: OptionsAST, -): ?CommandOptions { - if (commandOptionsExpression == null) { - return null; - } - - let foundOptions; - try { - foundOptions = commandOptionsExpression.properties.reduce( - (options, prop) => { - options[prop.key.name] = ( - (prop && prop.value && prop.value.elements) || - [] - ).map(element => element && element.value); - return options; - }, - {}, - ); - } catch (e) { - throw new Error( - 'Failed to parse command options, please check that they are defined correctly', - ); - } - - return foundOptions; -} - function getOptions(optionsExpression: OptionsAST): ?OptionsShape { if (!optionsExpression) { return null; @@ -82,6 +50,5 @@ function getOptions(optionsExpression: OptionsAST): ?OptionsShape { } module.exports = { - getCommandOptions, getOptions, };