Skip to content

Commit 4a5bc8a

Browse files
harshsiriahmohitcharkha
authored andcommitted
Extracted IncorrectModuleRegistryCallTypeParameterParserError to throwIfIncorrectModuleRegistryCallTypeParameterParserError (facebook#34941)
Summary: This PR is part of facebook#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: facebook#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 c82a0ff commit 4a5bc8a

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
@@ -17,13 +17,15 @@ const {
1717
throwIfModuleInterfaceIsMisnamed,
1818
throwIfUnusedModuleInterfaceParserError,
1919
throwIfWrongNumberOfCallExpressionArgs,
20+
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
2021
} = require('../error-utils');
2122
const {
2223
ModuleInterfaceNotFoundParserError,
2324
MoreThanOneModuleRegistryCallsParserError,
2425
MisnamedModuleInterfaceParserError,
2526
UnusedModuleInterfaceParserError,
2627
IncorrectModuleRegistryCallArityParserError,
28+
IncorrectModuleRegistryCallTypeParameterParserError,
2729
} = require('../errors');
2830

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

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

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

2324
function throwIfModuleInterfaceIsMisnamed(
@@ -98,10 +99,49 @@ function throwIfWrongNumberOfCallExpressionArgs(
9899
}
99100
}
100101

102+
function throwIfIncorrectModuleRegistryCallTypeParameterParserError(
103+
nativeModuleName: string,
104+
typeArguments: $FlowFixMe,
105+
methodName: string,
106+
moduleName: string,
107+
language: ParserType,
108+
) {
109+
function throwError() {
110+
throw new IncorrectModuleRegistryCallTypeParameterParserError(
111+
nativeModuleName,
112+
typeArguments,
113+
methodName,
114+
moduleName,
115+
language,
116+
);
117+
}
118+
119+
if (language === 'Flow') {
120+
if (
121+
typeArguments.type !== 'TypeParameterInstantiation' ||
122+
typeArguments.params.length !== 1 ||
123+
typeArguments.params[0].type !== 'GenericTypeAnnotation' ||
124+
typeArguments.params[0].id.name !== 'Spec'
125+
) {
126+
throwError();
127+
}
128+
} else if (language === 'TypeScript') {
129+
if (
130+
typeArguments.type !== 'TSTypeParameterInstantiation' ||
131+
typeArguments.params.length !== 1 ||
132+
typeArguments.params[0].type !== 'TSTypeReference' ||
133+
typeArguments.params[0].typeName.name !== 'Spec'
134+
) {
135+
throwError();
136+
}
137+
}
138+
}
139+
101140
module.exports = {
102141
throwIfModuleInterfaceIsMisnamed,
103142
throwIfModuleInterfaceNotFound,
104143
throwIfMoreThanOneModuleRegistryCalls,
105144
throwIfUnusedModuleInterfaceParserError,
106145
throwIfWrongNumberOfCallExpressionArgs,
146+
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
107147
};

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

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ const {
6262
UnsupportedObjectPropertyTypeAnnotationParserError,
6363
UnsupportedObjectPropertyValueTypeAnnotationParserError,
6464
UntypedModuleRegistryCallParserError,
65-
IncorrectModuleRegistryCallTypeParameterParserError,
6665
IncorrectModuleRegistryCallArgumentTypeParserError,
6766
} = require('../../errors.js');
6867

@@ -72,6 +71,7 @@ const {
7271
throwIfMoreThanOneModuleRegistryCalls,
7372
throwIfUnusedModuleInterfaceParserError,
7473
throwIfWrongNumberOfCallExpressionArgs,
74+
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
7575
} = require('../../error-utils');
7676

7777
const language = 'Flow';
@@ -661,20 +661,13 @@ function buildModuleSchema(
661661
);
662662
}
663663

664-
if (
665-
typeArguments.type !== 'TypeParameterInstantiation' ||
666-
typeArguments.params.length !== 1 ||
667-
typeArguments.params[0].type !== 'GenericTypeAnnotation' ||
668-
typeArguments.params[0].id.name !== 'Spec'
669-
) {
670-
throw new IncorrectModuleRegistryCallTypeParameterParserError(
671-
hasteModuleName,
672-
typeArguments,
673-
methodName,
674-
$moduleName,
675-
language,
676-
);
677-
}
664+
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
665+
hasteModuleName,
666+
typeArguments,
667+
methodName,
668+
$moduleName,
669+
language,
670+
);
678671

679672
return $moduleName;
680673
});

0 commit comments

Comments
 (0)