Skip to content

Commit

Permalink
refactor(codegen): move getCommandOptions to parsers-commons
Browse files Browse the repository at this point in the history
  • Loading branch information
tarunrajput authored and cipolleschi committed Mar 13, 2023
1 parent 803bb16 commit c476009
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
buildSchemaFromConfigType,
buildSchema,
parseModuleName,
getCommandOptions,
} from '../parsers-commons';
import type {ParserType} from '../errors';

Expand Down Expand Up @@ -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',
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>,
}>;

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;
Expand Down Expand Up @@ -82,6 +50,5 @@ function getOptions(optionsExpression: OptionsAST): ?OptionsShape {
}

module.exports = {
getCommandOptions,
getOptions,
};
36 changes: 36 additions & 0 deletions packages/react-native-codegen/src/parsers/parsers-commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ const {

const invariant = require('invariant');

export type CommandOptions = $ReadOnly<{
supportedCommands: $ReadOnlyArray<string>,
}>;

// $FlowFixMe[unclear-type] TODO(T108222691): Use flow-types for @babel/parser
type OptionsAST = Object;

function wrapModuleSchema(
nativeModuleSchema: NativeModuleSchema,
hasteModuleName: string,
Expand Down Expand Up @@ -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,
Expand All @@ -653,4 +688,5 @@ module.exports = {
buildSchema,
parseModuleName,
buildModuleSchema,
getCommandOptions,
};
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>,
}>;

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;
Expand Down Expand Up @@ -82,6 +50,5 @@ function getOptions(optionsExpression: OptionsAST): ?OptionsShape {
}

module.exports = {
getCommandOptions,
getOptions,
};

0 comments on commit c476009

Please sign in to comment.