Skip to content

Commit

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

Expand Down Expand Up @@ -1275,3 +1276,90 @@ describe('getCommandOptions', () => {
);
});
});

describe('getOptions', () => {
it('returns null if optionsExpression is falsy', () => {
expect(getOptions(null)).toBeNull();
expect(getOptions(undefined)).toBeNull();
expect(getOptions(false)).toBeNull();
expect(getOptions(0)).toBeNull();
expect(getOptions('')).toBeNull();
});

it('parses and returns options correctly if codegen options are defined correctly', () => {
const optionsExpression = {
properties: [
{
value: {
type: 'ArrayExpression',
value: 'value',
elements: [
{
value: 'value1',
},
],
},
key: {
name: 'keyName',
},
},
],
};
expect(getOptions(optionsExpression)).toEqual({
keyName: ['value1'],
});
});

it('throws an error if codegen options are not defined correctly', () => {
const optionsExpression = {
properties: null,
};
expect(() => getOptions(optionsExpression)).toThrowError(
'Failed to parse codegen options, please check that they are defined correctly',
);
});

it('throws an error if both paperComponentName and paperComponentNameDeprecated are used', () => {
const optionsExpression = {
properties: [
{
key: {name: 'paperComponentName'},
value: {value: 'RCTRefreshControl'},
},
{
key: {name: 'paperComponentNameDeprecated'},
value: {value: 'RCTSwitch'},
},
],
};
expect(() => getOptions(optionsExpression)).toThrowError(
'Failed to parse codegen options, cannot use both paperComponentName and paperComponentNameDeprecated',
);
});

it('returns options if only paperComponentName is used', () => {
const optionsExpression = {
properties: [
{
key: {name: 'paperComponentName'},
value: {value: 'RCTRefreshControl'},
},
],
};
const expectedOptions = {paperComponentName: 'RCTRefreshControl'};
expect(getOptions(optionsExpression)).toEqual(expectedOptions);
});

it('returns options if only paperComponentNameDeprecated is used', () => {
const optionsExpression = {
properties: [
{
key: {name: 'paperComponentNameDeprecated'},
value: {value: 'RCTRefreshControl'},
},
],
};
const expectedOptions = {paperComponentNameDeprecated: 'RCTRefreshControl'};
expect(getOptions(optionsExpression)).toEqual(expectedOptions);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ import type {ComponentSchemaBuilderConfig} from '../../schema.js';
const {getCommands} = require('./commands');
const {getEvents} = require('./events');
const {getExtendsProps, removeKnownExtends} = require('./extends');
const {getOptions} = require('./options');
const {getProps} = require('./props');
const {getProperties} = require('./componentsUtils.js');
const {getCommandOptions} = require('../../parsers-commons');
const {getCommandOptions, getOptions} = 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

This file was deleted.

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 @@ -22,6 +22,7 @@ import type {
NativeModulePropertyShape,
SchemaType,
NativeModuleEnumMap,
OptionsShape,
} from '../CodegenSchema.js';

import type {Parser} from './parser';
Expand Down Expand Up @@ -675,6 +676,40 @@ function getCommandOptions(
return foundOptions;
}

function getOptions(optionsExpression: OptionsAST): ?OptionsShape {
if (!optionsExpression) {
return null;
}
let foundOptions;
try {
foundOptions = optionsExpression.properties.reduce((options, prop) => {
if (prop.value.type === 'ArrayExpression') {
options[prop.key.name] = prop.value.elements.map(
element => element.value,
);
} else {
options[prop.key.name] = prop.value.value;
}
return options;
}, {});
} catch (e) {
throw new Error(
'Failed to parse codegen options, please check that they are defined correctly',
);
}

if (
foundOptions.paperComponentName &&
foundOptions.paperComponentNameDeprecated
) {
throw new Error(
'Failed to parse codegen options, cannot use both paperComponentName and paperComponentNameDeprecated',
);
}

return foundOptions;
}

module.exports = {
wrapModuleSchema,
unwrapNullable,
Expand All @@ -689,4 +724,5 @@ module.exports = {
parseModuleName,
buildModuleSchema,
getCommandOptions,
getOptions,
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ import type {ComponentSchemaBuilderConfig} from '../../schema.js';
const {getCommands} = require('./commands');
const {getEvents} = require('./events');
const {categorizeProps} = require('./extends');
const {getOptions} = require('./options');
const {getProps} = require('./props');
const {getProperties} = require('./componentsUtils.js');
const {getCommandOptions} = require('../../parsers-commons');
const {getCommandOptions, getOptions} = 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

This file was deleted.

0 comments on commit 10645c7

Please sign in to comment.