Skip to content

Commit aba6be6

Browse files
dhruvtailor7facebook-github-bot
authored andcommitted
Extract UnsupportedObjectPropertyValueTypeAnnotationParserError to a throwing function (#34917)
Summary: This PR is a part of #34872. Extracted the UnsupportedObjectPropertyValueTypeAnnotationParserError in its own throwing function and reuse that function passing a proper type. ## Changelog [Internal] [Changed] - Extract the UnsupportedObjectPropertyValueTypeAnnotationParserError in its own throwing function and reuse that function passing a proper type. Pull Request resolved: #34917 Test Plan: Output of yarn jest react-native-codegen. <img width="451" alt="Screenshot 2022-10-10 at 12 55 39 PM" src="https://user-images.githubusercontent.com/32268377/194816863-5220dbaa-3b63-42bf-8e62-9d7b915f7cbd.png"> Reviewed By: cortinico Differential Revision: D40424885 Pulled By: cipolleschi fbshipit-source-id: 08d4d13ee3959391261fe13c190a4bb893970757
1 parent aeab383 commit aba6be6

File tree

3 files changed

+59
-64
lines changed

3 files changed

+59
-64
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const {
1919
UnusedModuleInterfaceParserError,
2020
IncorrectModuleRegistryCallArityParserError,
2121
IncorrectModuleRegistryCallTypeParameterParserError,
22+
UnsupportedObjectPropertyValueTypeAnnotationParserError,
2223
UntypedModuleRegistryCallParserError,
2324
UnsupportedModulePropertyParserError,
2425
} = require('./errors.js');
@@ -188,10 +189,36 @@ function throwIfModuleTypeIsUnsupported(
188189
}
189190
}
190191

192+
const UnsupportedObjectPropertyTypeToInvalidPropertyValueTypeMap = {
193+
FunctionTypeAnnotation: 'FunctionTypeAnnotation',
194+
VoidTypeAnnotation: 'void',
195+
PromiseTypeAnnotation: 'Promise',
196+
};
197+
198+
function throwIfPropertyValueTypeIsUnsupported(
199+
moduleName: string,
200+
propertyValue: $FlowFixMe,
201+
propertyKey: string,
202+
type: string,
203+
language: ParserType,
204+
) {
205+
const invalidPropertyValueType =
206+
UnsupportedObjectPropertyTypeToInvalidPropertyValueTypeMap[type];
207+
208+
throw new UnsupportedObjectPropertyValueTypeAnnotationParserError(
209+
moduleName,
210+
propertyValue,
211+
propertyKey,
212+
invalidPropertyValueType,
213+
language,
214+
);
215+
}
216+
191217
module.exports = {
192218
throwIfModuleInterfaceIsMisnamed,
193219
throwIfModuleInterfaceNotFound,
194220
throwIfMoreThanOneModuleRegistryCalls,
221+
throwIfPropertyValueTypeIsUnsupported,
195222
throwIfUnusedModuleInterfaceParserError,
196223
throwIfWrongNumberOfCallExpressionArgs,
197224
throwIfIncorrectModuleRegistryCallTypeParameterParserError,

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

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ const {
6363
UnsupportedEnumDeclarationParserError,
6464
UnsupportedUnionTypeAnnotationParserError,
6565
UnsupportedObjectPropertyTypeAnnotationParserError,
66-
UnsupportedObjectPropertyValueTypeAnnotationParserError,
6766
IncorrectModuleRegistryCallArgumentTypeParserError,
6867
} = require('../../errors.js');
6968
const {verifyPlatforms} = require('../../utils');
7069

7170
const {
7271
throwIfModuleInterfaceNotFound,
7372
throwIfModuleInterfaceIsMisnamed,
73+
throwIfPropertyValueTypeIsUnsupported,
7474
throwIfUnusedModuleInterfaceParserError,
7575
throwIfWrongNumberOfCallExpressionArgs,
7676
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
@@ -309,44 +309,28 @@ function translateTypeAnnotation(
309309
),
310310
);
311311

312-
if (propertyTypeAnnotation.type === 'FunctionTypeAnnotation') {
313-
throw new UnsupportedObjectPropertyValueTypeAnnotationParserError(
312+
if (
313+
propertyTypeAnnotation.type === 'FunctionTypeAnnotation' ||
314+
propertyTypeAnnotation.type === 'PromiseTypeAnnotation' ||
315+
propertyTypeAnnotation.type === 'VoidTypeAnnotation'
316+
) {
317+
throwIfPropertyValueTypeIsUnsupported(
314318
hasteModuleName,
315319
property.value,
316320
property.key,
317321
propertyTypeAnnotation.type,
318322
language,
319323
);
324+
} else {
325+
return {
326+
name: key.name,
327+
optional,
328+
typeAnnotation: wrapNullable(
329+
isPropertyNullable,
330+
propertyTypeAnnotation,
331+
),
332+
};
320333
}
321-
322-
if (propertyTypeAnnotation.type === 'VoidTypeAnnotation') {
323-
throw new UnsupportedObjectPropertyValueTypeAnnotationParserError(
324-
hasteModuleName,
325-
property.value,
326-
property.key,
327-
'void',
328-
language,
329-
);
330-
}
331-
332-
if (propertyTypeAnnotation.type === 'PromiseTypeAnnotation') {
333-
throw new UnsupportedObjectPropertyValueTypeAnnotationParserError(
334-
hasteModuleName,
335-
property.value,
336-
property.key,
337-
'Promise',
338-
language,
339-
);
340-
}
341-
342-
return {
343-
name: key.name,
344-
optional,
345-
typeAnnotation: wrapNullable(
346-
isPropertyNullable,
347-
propertyTypeAnnotation,
348-
),
349-
};
350334
});
351335
},
352336
)

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

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ const {
6363
UnsupportedEnumDeclarationParserError,
6464
UnsupportedUnionTypeAnnotationParserError,
6565
UnsupportedObjectPropertyTypeAnnotationParserError,
66-
UnsupportedObjectPropertyValueTypeAnnotationParserError,
6766
IncorrectModuleRegistryCallArgumentTypeParserError,
6867
} = require('../../errors.js');
6968
const {verifyPlatforms} = require('../../utils');
7069

7170
const {
7271
throwIfUntypedModule,
72+
throwIfPropertyValueTypeIsUnsupported,
7373
throwIfModuleTypeIsUnsupported,
7474
throwIfUnusedModuleInterfaceParserError,
7575
throwIfModuleInterfaceNotFound,
@@ -324,44 +324,28 @@ function translateTypeAnnotation(
324324
),
325325
);
326326

327-
if (propertyTypeAnnotation.type === 'FunctionTypeAnnotation') {
328-
throw new UnsupportedObjectPropertyValueTypeAnnotationParserError(
327+
if (
328+
propertyTypeAnnotation.type === 'FunctionTypeAnnotation' ||
329+
propertyTypeAnnotation.type === 'PromiseTypeAnnotation' ||
330+
propertyTypeAnnotation.type === 'VoidTypeAnnotation'
331+
) {
332+
throwIfPropertyValueTypeIsUnsupported(
329333
hasteModuleName,
330334
property.typeAnnotation.typeAnnotation,
331335
property.key,
332336
propertyTypeAnnotation.type,
333337
language,
334338
);
339+
} else {
340+
return {
341+
name: key.name,
342+
optional,
343+
typeAnnotation: wrapNullable(
344+
isPropertyNullable,
345+
propertyTypeAnnotation,
346+
),
347+
};
335348
}
336-
337-
if (propertyTypeAnnotation.type === 'VoidTypeAnnotation') {
338-
throw new UnsupportedObjectPropertyValueTypeAnnotationParserError(
339-
hasteModuleName,
340-
property.typeAnnotation.typeAnnotation,
341-
property.key,
342-
'void',
343-
language,
344-
);
345-
}
346-
347-
if (propertyTypeAnnotation.type === 'PromiseTypeAnnotation') {
348-
throw new UnsupportedObjectPropertyValueTypeAnnotationParserError(
349-
hasteModuleName,
350-
property.typeAnnotation.typeAnnotation,
351-
property.key,
352-
'Promise',
353-
language,
354-
);
355-
}
356-
357-
return {
358-
name: key.name,
359-
optional,
360-
typeAnnotation: wrapNullable(
361-
isPropertyNullable,
362-
propertyTypeAnnotation,
363-
),
364-
};
365349
});
366350
},
367351
)

0 commit comments

Comments
 (0)