Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracted IncorrectModuleRegistryCallTypeParameterParserError to throwIfIncorrectModuleRegistryCallTypeParameterParserError #34941

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
/**
* 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 {
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
} = require('../error-utils');

const {
IncorrectModuleRegistryCallTypeParameterParserError,
} = require('../errors');

describe('throwIfIncorrectModuleRegistryCallTypeParameterParserError', () => {
const nativeModuleName = 'moduleName';
const methodName = 'methodName';
const moduleName = 'moduleName';
it('throw error if flowTypeArguments type is incorrect', () => {
const flowTypeArguments = {
type: '',
params: [
{
type: 'GenericTypeAnnotation',
id: {
name: 'Spec',
},
},
],
};

const parserType = 'Flow';

expect(() => {
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
nativeModuleName,
flowTypeArguments,
methodName,
moduleName,
parserType,
);
}).toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
});

it('throw error if flowTypeArguments params length is not 1', () => {
const flowTypeArguments = {
type: 'TypeParameterInstantiation',
params: [],
};

const parserType = 'Flow';

expect(() => {
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
nativeModuleName,
flowTypeArguments,
methodName,
moduleName,
parserType,
);
}).toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
});

it('throw error if flowTypeArguments params type is not GenericTypeAnnotation', () => {
const flowTypeArguments = {
type: 'TypeParameterInstantiation',
params: [
{
type: '',
id: {
name: 'Spec',
},
},
],
};

const parserType = 'Flow';

expect(() => {
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
nativeModuleName,
flowTypeArguments,
methodName,
moduleName,
parserType,
);
}).toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
});

it('throw error if flowTypeArguments params id name is not Spec', () => {
const flowTypeArguments = {
type: 'TypeParameterInstantiation',
params: [
{
type: 'GenericTypeAnnotation',
id: {
name: '',
},
},
],
};

const parserType = 'Flow';

expect(() => {
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
nativeModuleName,
flowTypeArguments,
methodName,
moduleName,
parserType,
);
}).toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
});

it('do not throw error if flowTypeArguments are correct', () => {
const flowTypeArguments = {
type: 'TypeParameterInstantiation',
params: [
{
type: 'GenericTypeAnnotation',
id: {
name: 'Spec',
},
},
],
};

const parserType = 'Flow';

expect(() => {
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
nativeModuleName,
flowTypeArguments,
methodName,
moduleName,
parserType,
);
}).not.toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
});

it('throw error if typeScriptTypeArguments type not correct', () => {
const typeScriptTypeArguments = {
type: '',
params: [
{
type: 'TSTypeReference',
typeName: {
name: 'Spec',
},
},
],
};

const parserType = 'TypeScript';

expect(() => {
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
nativeModuleName,
typeScriptTypeArguments,
methodName,
moduleName,
parserType,
);
}).toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
});

it('throw error if typeScriptTypeArguments params length is not equal to 1', () => {
const typeScriptTypeArguments = {
type: 'TSTypeParameterInstantiation',
params: [],
};

const parserType = 'TypeScript';

expect(() => {
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
nativeModuleName,
typeScriptTypeArguments,
methodName,
moduleName,
parserType,
);
}).toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
});

it('throw error if typeScriptTypeArguments params type is not TSTypeReference', () => {
const typeScriptTypeArguments = {
type: 'TSTypeParameterInstantiation',
params: [
{
type: '',
typeName: {
name: 'Spec',
},
},
],
};

const parserType = 'TypeScript';

expect(() => {
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
nativeModuleName,
typeScriptTypeArguments,
methodName,
moduleName,
parserType,
);
}).toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
});

it('throw error if typeScriptTypeArguments params typeName name is not Spec', () => {
const typeScriptTypeArguments = {
type: 'TSTypeParameterInstantiation',
params: [
{
type: 'TSTypeReference',
typeName: {
name: '',
},
},
],
};

const parserType = 'TypeScript';

expect(() => {
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
nativeModuleName,
typeScriptTypeArguments,
methodName,
moduleName,
parserType,
);
}).toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
});

it('do not throw error if typeScriptTypeArguments are correct', () => {
const typeScriptTypeArguments = {
type: 'TSTypeParameterInstantiation',
params: [
{
type: 'TSTypeReference',
typeName: {
name: 'Spec',
},
},
],
};

const parserType = 'TypeScript';

expect(() => {
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
nativeModuleName,
typeScriptTypeArguments,
methodName,
moduleName,
parserType,
);
}).not.toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
});
});
58 changes: 58 additions & 0 deletions packages/react-native-codegen/src/parsers/error-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* 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 {
IncorrectModuleRegistryCallTypeParameterParserError,
} = require('./errors.js');

function throwIfIncorrectModuleRegistryCallTypeParameterParserError(
nativeModuleName: string,
typeArguments: $FlowFixMe,
methodName: string,
moduleName: string,
language: ParserType,
) {
function throwError() {
throw new IncorrectModuleRegistryCallTypeParameterParserError(
nativeModuleName,
typeArguments,
methodName,
moduleName,
language,
);
}

if (language === 'Flow') {
if (
typeArguments.type !== 'TypeParameterInstantiation' ||
typeArguments.params.length !== 1 ||
typeArguments.params[0].type !== 'GenericTypeAnnotation' ||
typeArguments.params[0].id.name !== 'Spec'
) {
throwError();
}
} else if (language === 'TypeScript') {
if (
typeArguments.type !== 'TSTypeParameterInstantiation' ||
typeArguments.params.length !== 1 ||
typeArguments.params[0].type !== 'TSTypeReference' ||
typeArguments.params[0].typeName.name !== 'Spec'
) {
throwError();
}
}
}

module.exports = {
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
};
26 changes: 11 additions & 15 deletions packages/react-native-codegen/src/parsers/flow/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ const {
UnusedModuleInterfaceParserError,
MoreThanOneModuleRegistryCallsParserError,
UntypedModuleRegistryCallParserError,
IncorrectModuleRegistryCallTypeParameterParserError,
IncorrectModuleRegistryCallArityParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');

const {
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
} = require('../../error-utils');

const language = 'Flow';

function nullGuard<T>(fn: () => T): ?T {
Expand Down Expand Up @@ -673,20 +676,13 @@ function buildModuleSchema(
);
}

if (
typeArguments.type !== 'TypeParameterInstantiation' ||
typeArguments.params.length !== 1 ||
typeArguments.params[0].type !== 'GenericTypeAnnotation' ||
typeArguments.params[0].id.name !== 'Spec'
) {
throw new IncorrectModuleRegistryCallTypeParameterParserError(
hasteModuleName,
typeArguments,
methodName,
$moduleName,
language,
);
}
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
hasteModuleName,
typeArguments,
methodName,
$moduleName,
language,
);

return $moduleName;
});
Expand Down
Loading