Skip to content

Commit bb519ec

Browse files
harshsiriahfacebook-github-bot
authored andcommitted
Extracted IncorrectModuleRegistryCallTypeParameterParserError to throwIfIncorrectModuleRegistryCallTypeParameterParserError (#34941)
Summary: This PR is part of #34872. This PR extracts `IncorrectModuleRegistryCallTypeParameterParserError` exception to a separate function inside an `error-utils.js` file ## Changelog [Internal] [Changed] - Extract `IncorrectModuleRegistryCallTypeParameterParserError` to a seperate function inside `error-utils.js` Pull Request resolved: #34941 Test Plan: ```sh yarn jest react-native-codegen ``` Added unit case in `error-utils-test.js` file <img width="940" alt="Screenshot 2022-10-11 at 4 42 03 PM" src="https://user-images.githubusercontent.com/86605635/195076564-3b023c17-661c-4330-805c-0216c4391d59.png"> Reviewed By: dmytrorykun Differential Revision: D40296642 Pulled By: cipolleschi fbshipit-source-id: 7c7bba6a4f68e9b8fa4729a7651f22cce6d7ca6e
1 parent 76c5b6f commit bb519ec

File tree

4 files changed

+309
-30
lines changed

4 files changed

+309
-30
lines changed

packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ const {
1616
throwIfMoreThanOneModuleRegistryCalls,
1717
throwIfUnusedModuleInterfaceParserError,
1818
throwIfWrongNumberOfCallExpressionArgs,
19+
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
1920
} = require('../error-utils');
2021
const {
2122
ModuleInterfaceNotFoundParserError,
2223
MoreThanOneModuleRegistryCallsParserError,
2324
UnusedModuleInterfaceParserError,
2425
IncorrectModuleRegistryCallArityParserError,
26+
IncorrectModuleRegistryCallTypeParameterParserError,
2527
} = require('../errors');
2628

2729
describe('throwIfModuleInterfaceNotFound', () => {
@@ -147,3 +149,254 @@ describe('throwErrorIfWrongNumberOfCallExpressionArgs', () => {
147149
}).not.toThrow(IncorrectModuleRegistryCallArityParserError);
148150
});
149151
});
152+
153+
describe('throwIfIncorrectModuleRegistryCallTypeParameterParserError', () => {
154+
const nativeModuleName = 'moduleName';
155+
const methodName = 'methodName';
156+
const moduleName = 'moduleName';
157+
it('throw error if flowTypeArguments type is incorrect', () => {
158+
const flowTypeArguments = {
159+
type: '',
160+
params: [
161+
{
162+
type: 'GenericTypeAnnotation',
163+
id: {
164+
name: 'Spec',
165+
},
166+
},
167+
],
168+
};
169+
170+
const parserType = 'Flow';
171+
172+
expect(() => {
173+
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
174+
nativeModuleName,
175+
flowTypeArguments,
176+
methodName,
177+
moduleName,
178+
parserType,
179+
);
180+
}).toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
181+
});
182+
183+
it('throw error if flowTypeArguments params length is not 1', () => {
184+
const flowTypeArguments = {
185+
type: 'TypeParameterInstantiation',
186+
params: [],
187+
};
188+
189+
const parserType = 'Flow';
190+
191+
expect(() => {
192+
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
193+
nativeModuleName,
194+
flowTypeArguments,
195+
methodName,
196+
moduleName,
197+
parserType,
198+
);
199+
}).toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
200+
});
201+
202+
it('throw error if flowTypeArguments params type is not GenericTypeAnnotation', () => {
203+
const flowTypeArguments = {
204+
type: 'TypeParameterInstantiation',
205+
params: [
206+
{
207+
type: '',
208+
id: {
209+
name: 'Spec',
210+
},
211+
},
212+
],
213+
};
214+
215+
const parserType = 'Flow';
216+
217+
expect(() => {
218+
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
219+
nativeModuleName,
220+
flowTypeArguments,
221+
methodName,
222+
moduleName,
223+
parserType,
224+
);
225+
}).toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
226+
});
227+
228+
it('throw error if flowTypeArguments params id name is not Spec', () => {
229+
const flowTypeArguments = {
230+
type: 'TypeParameterInstantiation',
231+
params: [
232+
{
233+
type: 'GenericTypeAnnotation',
234+
id: {
235+
name: '',
236+
},
237+
},
238+
],
239+
};
240+
241+
const parserType = 'Flow';
242+
243+
expect(() => {
244+
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
245+
nativeModuleName,
246+
flowTypeArguments,
247+
methodName,
248+
moduleName,
249+
parserType,
250+
);
251+
}).toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
252+
});
253+
254+
it('do not throw error if flowTypeArguments are correct', () => {
255+
const flowTypeArguments = {
256+
type: 'TypeParameterInstantiation',
257+
params: [
258+
{
259+
type: 'GenericTypeAnnotation',
260+
id: {
261+
name: 'Spec',
262+
},
263+
},
264+
],
265+
};
266+
267+
const parserType = 'Flow';
268+
269+
expect(() => {
270+
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
271+
nativeModuleName,
272+
flowTypeArguments,
273+
methodName,
274+
moduleName,
275+
parserType,
276+
);
277+
}).not.toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
278+
});
279+
280+
it('throw error if typeScriptTypeArguments type not correct', () => {
281+
const typeScriptTypeArguments = {
282+
type: '',
283+
params: [
284+
{
285+
type: 'TSTypeReference',
286+
typeName: {
287+
name: 'Spec',
288+
},
289+
},
290+
],
291+
};
292+
293+
const parserType = 'TypeScript';
294+
295+
expect(() => {
296+
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
297+
nativeModuleName,
298+
typeScriptTypeArguments,
299+
methodName,
300+
moduleName,
301+
parserType,
302+
);
303+
}).toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
304+
});
305+
306+
it('throw error if typeScriptTypeArguments params length is not equal to 1', () => {
307+
const typeScriptTypeArguments = {
308+
type: 'TSTypeParameterInstantiation',
309+
params: [],
310+
};
311+
312+
const parserType = 'TypeScript';
313+
314+
expect(() => {
315+
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
316+
nativeModuleName,
317+
typeScriptTypeArguments,
318+
methodName,
319+
moduleName,
320+
parserType,
321+
);
322+
}).toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
323+
});
324+
325+
it('throw error if typeScriptTypeArguments params type is not TSTypeReference', () => {
326+
const typeScriptTypeArguments = {
327+
type: 'TSTypeParameterInstantiation',
328+
params: [
329+
{
330+
type: '',
331+
typeName: {
332+
name: 'Spec',
333+
},
334+
},
335+
],
336+
};
337+
338+
const parserType = 'TypeScript';
339+
340+
expect(() => {
341+
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
342+
nativeModuleName,
343+
typeScriptTypeArguments,
344+
methodName,
345+
moduleName,
346+
parserType,
347+
);
348+
}).toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
349+
});
350+
351+
it('throw error if typeScriptTypeArguments params typeName name is not Spec', () => {
352+
const typeScriptTypeArguments = {
353+
type: 'TSTypeParameterInstantiation',
354+
params: [
355+
{
356+
type: 'TSTypeReference',
357+
typeName: {
358+
name: '',
359+
},
360+
},
361+
],
362+
};
363+
364+
const parserType = 'TypeScript';
365+
366+
expect(() => {
367+
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
368+
nativeModuleName,
369+
typeScriptTypeArguments,
370+
methodName,
371+
moduleName,
372+
parserType,
373+
);
374+
}).toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
375+
});
376+
377+
it('do not throw error if typeScriptTypeArguments are correct', () => {
378+
const typeScriptTypeArguments = {
379+
type: 'TSTypeParameterInstantiation',
380+
params: [
381+
{
382+
type: 'TSTypeReference',
383+
typeName: {
384+
name: 'Spec',
385+
},
386+
},
387+
],
388+
};
389+
390+
const parserType = 'TypeScript';
391+
392+
expect(() => {
393+
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
394+
nativeModuleName,
395+
typeScriptTypeArguments,
396+
methodName,
397+
moduleName,
398+
parserType,
399+
);
400+
}).not.toThrow(IncorrectModuleRegistryCallTypeParameterParserError);
401+
});
402+
});

packages/react-native-codegen/src/parsers/error-utils.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const {
1717
MoreThanOneModuleRegistryCallsParserError,
1818
UnusedModuleInterfaceParserError,
1919
IncorrectModuleRegistryCallArityParserError,
20+
IncorrectModuleRegistryCallTypeParameterParserError,
2021
} = require('./errors.js');
2122

2223
function throwIfModuleInterfaceNotFound(
@@ -83,9 +84,48 @@ function throwIfWrongNumberOfCallExpressionArgs(
8384
}
8485
}
8586

87+
function throwIfIncorrectModuleRegistryCallTypeParameterParserError(
88+
nativeModuleName: string,
89+
typeArguments: $FlowFixMe,
90+
methodName: string,
91+
moduleName: string,
92+
language: ParserType,
93+
) {
94+
function throwError() {
95+
throw new IncorrectModuleRegistryCallTypeParameterParserError(
96+
nativeModuleName,
97+
typeArguments,
98+
methodName,
99+
moduleName,
100+
language,
101+
);
102+
}
103+
104+
if (language === 'Flow') {
105+
if (
106+
typeArguments.type !== 'TypeParameterInstantiation' ||
107+
typeArguments.params.length !== 1 ||
108+
typeArguments.params[0].type !== 'GenericTypeAnnotation' ||
109+
typeArguments.params[0].id.name !== 'Spec'
110+
) {
111+
throwError();
112+
}
113+
} else if (language === 'TypeScript') {
114+
if (
115+
typeArguments.type !== 'TSTypeParameterInstantiation' ||
116+
typeArguments.params.length !== 1 ||
117+
typeArguments.params[0].type !== 'TSTypeReference' ||
118+
typeArguments.params[0].typeName.name !== 'Spec'
119+
) {
120+
throwError();
121+
}
122+
}
123+
}
124+
86125
module.exports = {
87126
throwIfModuleInterfaceNotFound,
88127
throwIfMoreThanOneModuleRegistryCalls,
89128
throwIfUnusedModuleInterfaceParserError,
90129
throwIfWrongNumberOfCallExpressionArgs,
130+
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
91131
};

packages/react-native-codegen/src/parsers/flow/modules/index.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ const {
6464
UnsupportedObjectPropertyTypeAnnotationParserError,
6565
UnsupportedObjectPropertyValueTypeAnnotationParserError,
6666
UntypedModuleRegistryCallParserError,
67-
IncorrectModuleRegistryCallTypeParameterParserError,
6867
IncorrectModuleRegistryCallArgumentTypeParserError,
6968
} = require('../../errors.js');
7069

7170
const {
7271
throwIfModuleInterfaceNotFound,
7372
throwIfUnusedModuleInterfaceParserError,
7473
throwIfWrongNumberOfCallExpressionArgs,
74+
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
7575
} = require('../../error-utils');
7676

7777
const language = 'Flow';
@@ -667,20 +667,13 @@ function buildModuleSchema(
667667
);
668668
}
669669

670-
if (
671-
typeArguments.type !== 'TypeParameterInstantiation' ||
672-
typeArguments.params.length !== 1 ||
673-
typeArguments.params[0].type !== 'GenericTypeAnnotation' ||
674-
typeArguments.params[0].id.name !== 'Spec'
675-
) {
676-
throw new IncorrectModuleRegistryCallTypeParameterParserError(
677-
hasteModuleName,
678-
typeArguments,
679-
methodName,
680-
$moduleName,
681-
language,
682-
);
683-
}
670+
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
671+
hasteModuleName,
672+
typeArguments,
673+
methodName,
674+
$moduleName,
675+
language,
676+
);
684677

685678
return $moduleName;
686679
});

0 commit comments

Comments
 (0)