Skip to content

Commit ea55e3b

Browse files
gabrieldonadelfacebook-github-bot
authored andcommitted
chore: Unify codegen Flow and TS default case from translateTypeAnnotation (facebook#35086)
Summary: This PR unifies the Flow and TS `default:` case from codegen `translateTypeAnnotation` function into a single function called `translateDefault` inside `parser-commons.js` as requested on facebook#34872. ## Changelog [Internal] [Changed] - Unify codegen Flow and TS default case from translateTypeAnnotation Pull Request resolved: facebook#35086 Test Plan: Run `yarn jest react-native-codegen` and ensure CI is green ![image](https://user-images.githubusercontent.com/11707729/197931439-a0f0f7cd-eee7-4908-a7f1-856b40954178.png) Reviewed By: cortinico Differential Revision: D40801612 Pulled By: cipolleschi fbshipit-source-id: 612768d6fabe091ac428e7d8416c6da059fe1332
1 parent ab7b4d4 commit ea55e3b

File tree

9 files changed

+109
-60
lines changed

9 files changed

+109
-60
lines changed

packages/react-native-codegen/src/parsers/flow/components/schema.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
* @format
8-
* @flow strict-local
8+
* @flow strict
99
*/
1010

1111
'use strict';

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

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const {
3939
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
4040
emitMixedTypeAnnotation,
4141
emitUnionTypeAnnotation,
42+
translateDefault,
4243
} = require('../../parsers-commons');
4344
const {
4445
emitBoolean,
@@ -59,9 +60,7 @@ const {
5960
const {
6061
UnnamedFunctionParamParserError,
6162
UnsupportedArrayElementTypeAnnotationParserError,
62-
UnsupportedGenericParserError,
6363
UnsupportedTypeAnnotationParserError,
64-
UnsupportedEnumDeclarationParserError,
6564
UnsupportedObjectPropertyTypeAnnotationParserError,
6665
IncorrectModuleRegistryCallArgumentTypeParserError,
6766
} = require('../../errors.js');
@@ -250,34 +249,11 @@ function translateTypeAnnotation(
250249
return emitObject(nullable);
251250
}
252251
default: {
253-
const maybeEumDeclaration = types[typeAnnotation.id.name];
254-
if (
255-
maybeEumDeclaration &&
256-
maybeEumDeclaration.type === 'EnumDeclaration'
257-
) {
258-
const memberType = maybeEumDeclaration.body.type
259-
.replace('EnumNumberBody', 'NumberTypeAnnotation')
260-
.replace('EnumStringBody', 'StringTypeAnnotation');
261-
if (
262-
memberType === 'NumberTypeAnnotation' ||
263-
memberType === 'StringTypeAnnotation'
264-
) {
265-
return wrapNullable(nullable, {
266-
type: 'EnumDeclaration',
267-
memberType: memberType,
268-
});
269-
} else {
270-
throw new UnsupportedEnumDeclarationParserError(
271-
hasteModuleName,
272-
typeAnnotation,
273-
memberType,
274-
language,
275-
);
276-
}
277-
}
278-
throw new UnsupportedGenericParserError(
252+
return translateDefault(
279253
hasteModuleName,
280254
typeAnnotation,
255+
types,
256+
nullable,
281257
parser,
282258
);
283259
}

packages/react-native-codegen/src/parsers/flow/parser.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,24 @@
1010

1111
'use strict';
1212

13+
import type {ParserType} from '../errors';
1314
import type {Parser} from '../parser';
1415

1516
class FlowParser implements Parser {
17+
getMaybeEnumMemberType(maybeEnumDeclaration: $FlowFixMe): string {
18+
return maybeEnumDeclaration.body.type
19+
.replace('EnumNumberBody', 'NumberTypeAnnotation')
20+
.replace('EnumStringBody', 'StringTypeAnnotation');
21+
}
22+
23+
isEnumDeclaration(maybeEnumDeclaration: $FlowFixMe): boolean {
24+
return maybeEnumDeclaration.type === 'EnumDeclaration';
25+
}
26+
27+
language(): ParserType {
28+
return 'Flow';
29+
}
30+
1631
nameForGenericTypeAnnotation(typeAnnotation: $FlowFixMe): string {
1732
return typeAnnotation.id.name;
1833
}

packages/react-native-codegen/src/parsers/parser.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,29 @@
1010

1111
'use strict';
1212

13+
import type {ParserType} from './errors';
14+
1315
/**
1416
* This is the main interface for Parsers of various languages.
1517
* It exposes all the methods that contain language-specific logic.
1618
*/
1719
export interface Parser {
20+
/**
21+
* Given a type declaration, it possibly returns the name of the Enum type.
22+
* @parameter maybeEnumDeclaration: an object possibly containing an Enum declaration.
23+
* @returns: the name of the Enum type.
24+
*/
25+
getMaybeEnumMemberType(maybeEnumDeclaration: $FlowFixMe): string;
26+
/**
27+
* Given a type declaration, it returns a boolean specifying if is an Enum declaration.
28+
* @parameter maybeEnumDeclaration: an object possibly containing an Enum declaration.
29+
* @returns: a boolean specifying if is an Enum declaration.
30+
*/
31+
isEnumDeclaration(maybeEnumDeclaration: $FlowFixMe): boolean;
32+
/**
33+
* @returns: the Parser language.
34+
*/
35+
language(): ParserType;
1836
/**
1937
* Given a type annotation for a generic type, it returns the type name.
2038
* @parameter typeAnnotation: the annotation for a type in the AST.

packages/react-native-codegen/src/parsers/parsers-commons.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ const {
2828
UnsupportedObjectPropertyTypeAnnotationParserError,
2929
} = require('./errors');
3030
const invariant = require('invariant');
31+
import type {TypeDeclarationMap} from './utils';
32+
const {
33+
UnsupportedEnumDeclarationParserError,
34+
UnsupportedGenericParserError,
35+
} = require('./errors');
36+
import type {Parser} from './parser';
37+
import type {NativeModuleEnumDeclaration} from '../CodegenSchema';
3138

3239
function wrapModuleSchema(
3340
nativeModuleSchema: NativeModuleSchema,
@@ -157,6 +164,44 @@ function emitUnionTypeAnnotation(
157164
});
158165
}
159166

167+
function translateDefault(
168+
hasteModuleName: string,
169+
typeAnnotation: $FlowFixMe,
170+
types: TypeDeclarationMap,
171+
nullable: boolean,
172+
parser: Parser,
173+
): Nullable<NativeModuleEnumDeclaration> {
174+
const maybeEnumDeclaration =
175+
types[parser.nameForGenericTypeAnnotation(typeAnnotation)];
176+
177+
if (maybeEnumDeclaration && parser.isEnumDeclaration(maybeEnumDeclaration)) {
178+
const memberType = parser.getMaybeEnumMemberType(maybeEnumDeclaration);
179+
180+
if (
181+
memberType === 'NumberTypeAnnotation' ||
182+
memberType === 'StringTypeAnnotation'
183+
) {
184+
return wrapNullable(nullable, {
185+
type: 'EnumDeclaration',
186+
memberType: memberType,
187+
});
188+
} else {
189+
throw new UnsupportedEnumDeclarationParserError(
190+
hasteModuleName,
191+
typeAnnotation,
192+
memberType,
193+
parser.language(),
194+
);
195+
}
196+
}
197+
198+
throw new UnsupportedGenericParserError(
199+
hasteModuleName,
200+
typeAnnotation,
201+
parser,
202+
);
203+
}
204+
160205
function getKeyName(
161206
propertyOrIndex: $FlowFixMe,
162207
hasteModuleName: string,
@@ -190,4 +235,5 @@ module.exports = {
190235
emitMixedTypeAnnotation,
191236
emitUnionTypeAnnotation,
192237
getKeyName,
238+
translateDefault,
193239
};

packages/react-native-codegen/src/parsers/typescript/components/schema.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
* @format
8-
* @flow strict-local
8+
* @flow strict
99
*/
1010

1111
'use strict';

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

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const {
4242
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
4343
emitMixedTypeAnnotation,
4444
emitUnionTypeAnnotation,
45+
translateDefault,
4546
} = require('../../parsers-commons');
4647
const {
4748
emitBoolean,
@@ -63,7 +64,6 @@ const {
6364
UnsupportedArrayElementTypeAnnotationParserError,
6465
UnsupportedGenericParserError,
6566
UnsupportedTypeAnnotationParserError,
66-
UnsupportedEnumDeclarationParserError,
6767
UnsupportedObjectPropertyTypeAnnotationParserError,
6868
IncorrectModuleRegistryCallArgumentTypeParserError,
6969
} = require('../../errors.js');
@@ -264,36 +264,11 @@ function translateTypeAnnotation(
264264
return emitObject(nullable);
265265
}
266266
default: {
267-
const maybeEumDeclaration = types[typeAnnotation.typeName.name];
268-
if (
269-
maybeEumDeclaration &&
270-
maybeEumDeclaration.type === 'TSEnumDeclaration'
271-
) {
272-
const memberType = maybeEumDeclaration.members[0].initializer
273-
? maybeEumDeclaration.members[0].initializer.type
274-
.replace('NumericLiteral', 'NumberTypeAnnotation')
275-
.replace('StringLiteral', 'StringTypeAnnotation')
276-
: 'StringTypeAnnotation';
277-
if (
278-
memberType === 'NumberTypeAnnotation' ||
279-
memberType === 'StringTypeAnnotation'
280-
) {
281-
return wrapNullable(nullable, {
282-
type: 'EnumDeclaration',
283-
memberType: memberType,
284-
});
285-
} else {
286-
throw new UnsupportedEnumDeclarationParserError(
287-
hasteModuleName,
288-
typeAnnotation,
289-
memberType,
290-
language,
291-
);
292-
}
293-
}
294-
throw new UnsupportedGenericParserError(
267+
return translateDefault(
295268
hasteModuleName,
296269
typeAnnotation,
270+
types,
271+
nullable,
297272
parser,
298273
);
299274
}

packages/react-native-codegen/src/parsers/typescript/parser.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,28 @@
1010

1111
'use strict';
1212

13+
import type {ParserType} from '../errors';
1314
import type {Parser} from '../parser';
1415

1516
class TypeScriptParser implements Parser {
17+
getMaybeEnumMemberType(maybeEnumDeclaration: $FlowFixMe): string {
18+
if (maybeEnumDeclaration.members[0].initializer) {
19+
return maybeEnumDeclaration.members[0].initializer.type
20+
.replace('NumericLiteral', 'NumberTypeAnnotation')
21+
.replace('StringLiteral', 'StringTypeAnnotation');
22+
}
23+
24+
return 'StringTypeAnnotation';
25+
}
26+
27+
isEnumDeclaration(maybeEnumDeclaration: $FlowFixMe): boolean {
28+
return maybeEnumDeclaration.type === 'TSEnumDeclaration';
29+
}
30+
31+
language(): ParserType {
32+
return 'TypeScript';
33+
}
34+
1635
nameForGenericTypeAnnotation(typeAnnotation: $FlowFixMe): string {
1736
return typeAnnotation.typeName.name;
1837
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow strict-local
7+
* @flow strict
88
* @format
99
*/
1010

0 commit comments

Comments
 (0)