Skip to content

Commit 8a847a3

Browse files
Antoine Doubovetzkyfacebook-github-bot
authored andcommitted
Replace ternary in assertGenericTypeAnnotationHasExactlyOneTypeParameter with typeParameterInstantiation attribute in parser (facebook#35157)
Summary: Part of facebook#34872: > Create a new function typeParameterInstantiation in the [parsers.js file](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/parser.js) and add documentation to it. Implement it properly in the [FlowParser.js](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/flow/parser.js#L15) and in the [TypeScriptParser.js](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/typescript/parser.js#L15). Update the signature of [assertGenericTypeAnnotationHasExactlyOneTypeParameter](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/parsers-commons.js#L67) function to take the Parser instead of the language and use the new function in place of the [ternary operator ?:](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/parsers-commons.js#L83). There are 3 things I'm not sure about: 1. The issue suggests to create a new function. For this case I believe an attribute is simpler. Is there a reason to prefer a function ? 2. To update the tests I had to create a mocked parser. I created a new file `parserMock` (I took example on [AnimatedMock](https://github.com/facebook/react-native/blob/main/Libraries/Animated/AnimatedMock.js)). Does it seem ok ? 3. I'm not sure what to add in the documentation of `typeParameterInstantiation` ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [Internal] [Changed] - Replace ternary in assertGenericTypeAnnotationHasExactlyOneTypeParameter with typeParameterInstantiation attribute in parser Pull Request resolved: facebook#35157 Test Plan: I tested using Jest and Flow commands. Reviewed By: rshest Differential Revision: D40889856 Pulled By: cipolleschi fbshipit-source-id: 8d9a8e087852f98dcc3fc0ecf1d4a7153f482ce7
1 parent 2245603 commit 8a847a3

File tree

10 files changed

+74
-98
lines changed

10 files changed

+74
-98
lines changed

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

Lines changed: 10 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ const {
2020
} = require('../parsers-commons.js');
2121
const {UnsupportedUnionTypeAnnotationParserError} = require('../errors');
2222
import type {UnionTypeAnnotationMemberType} from '../../CodegenSchema';
23+
import {MockedParser} from '../parserMock';
24+
25+
const parser = new MockedParser();
2326

2427
describe('wrapNullable', () => {
2528
describe('when nullable is true', () => {
@@ -101,7 +104,7 @@ describe('assertGenericTypeAnnotationHasExactlyOneTypeParameter', () => {
101104
assertGenericTypeAnnotationHasExactlyOneTypeParameter(
102105
moduleName,
103106
typeAnnotation,
104-
'Flow',
107+
parser,
105108
),
106109
).not.toThrow();
107110
});
@@ -117,14 +120,14 @@ describe('assertGenericTypeAnnotationHasExactlyOneTypeParameter', () => {
117120
assertGenericTypeAnnotationHasExactlyOneTypeParameter(
118121
moduleName,
119122
typeAnnotation,
120-
'Flow',
123+
parser,
121124
),
122125
).toThrowErrorMatchingInlineSnapshot(
123126
`"Module testModuleName: Generic 'typeAnnotationName' must have type parameters."`,
124127
);
125128
});
126129

127-
it('throws an error if typeAnnotation.typeParameters.type is not TypeParameterInstantiation when language is Flow', () => {
130+
it('throws an error if typeAnnotation.typeParameters.type is not equal to parser.typeParameterInstantiation', () => {
128131
const flowTypeAnnotation = {
129132
typeParameters: {
130133
type: 'wrongType',
@@ -138,36 +141,14 @@ describe('assertGenericTypeAnnotationHasExactlyOneTypeParameter', () => {
138141
assertGenericTypeAnnotationHasExactlyOneTypeParameter(
139142
moduleName,
140143
flowTypeAnnotation,
141-
'Flow',
144+
parser,
142145
),
143146
).toThrowErrorMatchingInlineSnapshot(
144147
`"assertGenericTypeAnnotationHasExactlyOneTypeParameter: Type parameters must be an AST node of type 'TypeParameterInstantiation'"`,
145148
);
146149
});
147150

148-
it('throws an error if typeAnnotation.typeParameters.type is not TSTypeParameterInstantiation when language is TypeScript', () => {
149-
const typeScriptTypeAnnotation = {
150-
typeParameters: {
151-
type: 'wrongType',
152-
params: [1],
153-
},
154-
typeName: {
155-
name: 'typeAnnotationName',
156-
},
157-
};
158-
expect(() =>
159-
assertGenericTypeAnnotationHasExactlyOneTypeParameter(
160-
moduleName,
161-
typeScriptTypeAnnotation,
162-
'TypeScript',
163-
),
164-
).toThrowErrorMatchingInlineSnapshot(
165-
`"assertGenericTypeAnnotationHasExactlyOneTypeParameter: Type parameters must be an AST node of type 'TSTypeParameterInstantiation'"`,
166-
);
167-
});
168-
169-
it("throws an IncorrectlyParameterizedGenericParserError if typeParameters don't have 1 exactly parameter for Flow", () => {
170-
const language: ParserType = 'Flow';
151+
it("throws an IncorrectlyParameterizedGenericParserError if typeParameters don't have 1 exactly parameter", () => {
171152
const typeAnnotationWithTwoParams = {
172153
typeParameters: {
173154
params: [1, 2],
@@ -181,7 +162,7 @@ describe('assertGenericTypeAnnotationHasExactlyOneTypeParameter', () => {
181162
assertGenericTypeAnnotationHasExactlyOneTypeParameter(
182163
moduleName,
183164
typeAnnotationWithTwoParams,
184-
language,
165+
parser,
185166
),
186167
).toThrowErrorMatchingInlineSnapshot(
187168
`"Module testModuleName: Generic 'typeAnnotationName' must have exactly one type parameter."`,
@@ -200,48 +181,7 @@ describe('assertGenericTypeAnnotationHasExactlyOneTypeParameter', () => {
200181
assertGenericTypeAnnotationHasExactlyOneTypeParameter(
201182
moduleName,
202183
typeAnnotationWithNoParams,
203-
language,
204-
),
205-
).toThrowErrorMatchingInlineSnapshot(
206-
`"Module testModuleName: Generic 'typeAnnotationName' must have exactly one type parameter."`,
207-
);
208-
});
209-
210-
it("throws an IncorrectlyParameterizedGenericParserError if typeParameters don't have 1 exactly parameter for TS", () => {
211-
const language: ParserType = 'TypeScript';
212-
const typeAnnotationWithTwoParams = {
213-
typeParameters: {
214-
params: [1, 2],
215-
type: 'TSTypeParameterInstantiation',
216-
},
217-
typeName: {
218-
name: 'typeAnnotationName',
219-
},
220-
};
221-
expect(() =>
222-
assertGenericTypeAnnotationHasExactlyOneTypeParameter(
223-
moduleName,
224-
typeAnnotationWithTwoParams,
225-
language,
226-
),
227-
).toThrowErrorMatchingInlineSnapshot(
228-
`"Module testModuleName: Generic 'typeAnnotationName' must have exactly one type parameter."`,
229-
);
230-
231-
const typeAnnotationWithNoParams = {
232-
typeParameters: {
233-
params: [],
234-
type: 'TSTypeParameterInstantiation',
235-
},
236-
typeName: {
237-
name: 'typeAnnotationName',
238-
},
239-
};
240-
expect(() =>
241-
assertGenericTypeAnnotationHasExactlyOneTypeParameter(
242-
moduleName,
243-
typeAnnotationWithNoParams,
244-
language,
184+
parser,
245185
),
246186
).toThrowErrorMatchingInlineSnapshot(
247187
`"Module testModuleName: Generic 'typeAnnotationName' must have exactly one type parameter."`,

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ const {
2626
emitMixedTypeAnnotation,
2727
typeAliasResolution,
2828
} = require('../parsers-primitives.js');
29+
import {MockedParser} from '../parserMock';
30+
31+
const parser = new MockedParser();
2932

3033
describe('emitBoolean', () => {
3134
describe('when nullable is true', () => {
@@ -334,7 +337,7 @@ describe('typeAliasResolution', () => {
334337

335338
describe('emitPromise', () => {
336339
const moduleName = 'testModuleName';
337-
const language = 'Flow';
340+
338341
describe("when typeAnnotation doesn't have exactly one typeParameter", () => {
339342
const typeAnnotation = {
340343
typeParameters: {
@@ -348,7 +351,7 @@ describe('emitPromise', () => {
348351
it('throws an IncorrectlyParameterizedGenericParserError error', () => {
349352
const nullable = false;
350353
expect(() =>
351-
emitPromise(moduleName, typeAnnotation, language, nullable),
354+
emitPromise(moduleName, typeAnnotation, parser, nullable),
352355
).toThrow();
353356
});
354357
});
@@ -370,7 +373,7 @@ describe('emitPromise', () => {
370373
const result = emitPromise(
371374
moduleName,
372375
typeAnnotation,
373-
language,
376+
parser,
374377
nullable,
375378
);
376379
const expected = {
@@ -389,7 +392,7 @@ describe('emitPromise', () => {
389392
const result = emitPromise(
390393
moduleName,
391394
typeAnnotation,
392-
language,
395+
parser,
393396
nullable,
394397
);
395398
const expected = {

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,19 +184,14 @@ function translateTypeAnnotation(
184184
return emitRootTag(nullable);
185185
}
186186
case 'Promise': {
187-
return emitPromise(
188-
hasteModuleName,
189-
typeAnnotation,
190-
language,
191-
nullable,
192-
);
187+
return emitPromise(hasteModuleName, typeAnnotation, parser, nullable);
193188
}
194189
case 'Array':
195190
case '$ReadOnlyArray': {
196191
assertGenericTypeAnnotationHasExactlyOneTypeParameter(
197192
hasteModuleName,
198193
typeAnnotation,
199-
language,
194+
parser,
200195
);
201196

202197
return translateArrayTypeAnnotation(
@@ -213,7 +208,7 @@ function translateTypeAnnotation(
213208
assertGenericTypeAnnotationHasExactlyOneTypeParameter(
214209
hasteModuleName,
215210
typeAnnotation,
216-
language,
211+
parser,
217212
);
218213

219214
const [paramType, isParamNullable] = unwrapNullable(

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import type {ParserType} from '../errors';
1414
import type {Parser} from '../parser';
1515

1616
class FlowParser implements Parser {
17+
typeParameterInstantiation: string = 'TypeParameterInstantiation';
18+
1719
getMaybeEnumMemberType(maybeEnumDeclaration: $FlowFixMe): string {
1820
return maybeEnumDeclaration.body.type
1921
.replace('EnumNumberBody', 'NumberTypeAnnotation')

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ import type {ParserType} from './errors';
1717
* It exposes all the methods that contain language-specific logic.
1818
*/
1919
export interface Parser {
20+
/**
21+
* This is the TypeParameterInstantiation value
22+
*/
23+
typeParameterInstantiation: string;
24+
2025
/**
2126
* Given a type declaration, it possibly returns the name of the Enum type.
2227
* @parameter maybeEnumDeclaration: an object possibly containing an Enum declaration.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict
8+
* @format
9+
*/
10+
11+
'use strict';
12+
13+
import type {Parser} from './parser';
14+
import type {ParserType} from './errors';
15+
16+
export class MockedParser implements Parser {
17+
typeParameterInstantiation: string = 'TypeParameterInstantiation';
18+
19+
getMaybeEnumMemberType(maybeEnumDeclaration: $FlowFixMe): string {
20+
return maybeEnumDeclaration.body.type
21+
.replace('EnumNumberBody', 'NumberTypeAnnotation')
22+
.replace('EnumStringBody', 'StringTypeAnnotation');
23+
}
24+
25+
isEnumDeclaration(maybeEnumDeclaration: $FlowFixMe): boolean {
26+
return maybeEnumDeclaration.type === 'EnumDeclaration';
27+
}
28+
29+
language(): ParserType {
30+
return 'Flow';
31+
}
32+
33+
nameForGenericTypeAnnotation(typeAnnotation: $FlowFixMe): string {
34+
return typeAnnotation.id.name;
35+
}
36+
}

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,17 @@ function assertGenericTypeAnnotationHasExactlyOneTypeParameter(
7777
* TODO(T108222691): Use flow-types for @babel/parser
7878
*/
7979
typeAnnotation: $FlowFixMe,
80-
language: ParserType,
80+
parser: Parser,
8181
) {
8282
if (typeAnnotation.typeParameters == null) {
8383
throw new MissingTypeParameterGenericParserError(
8484
moduleName,
8585
typeAnnotation,
86-
language,
86+
parser.language(),
8787
);
8888
}
8989

90-
const typeAnnotationType =
91-
language === 'TypeScript'
92-
? 'TSTypeParameterInstantiation'
93-
: 'TypeParameterInstantiation';
90+
const typeAnnotationType = parser.typeParameterInstantiation;
9491

9592
invariant(
9693
typeAnnotation.typeParameters.type === typeAnnotationType,
@@ -101,7 +98,7 @@ function assertGenericTypeAnnotationHasExactlyOneTypeParameter(
10198
throw new MoreThanOneTypeParameterGenericParserError(
10299
moduleName,
103100
typeAnnotation,
104-
language,
101+
parser.language(),
105102
);
106103
}
107104
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import type {
3232
NamedShape,
3333
} from '../CodegenSchema';
3434
import type {ParserType} from './errors';
35+
import type {Parser} from './parser';
3536
import type {
3637
ParserErrorCapturer,
3738
TypeAliasResolutionStatus,
@@ -174,13 +175,13 @@ function typeAliasResolution(
174175
function emitPromise(
175176
hasteModuleName: string,
176177
typeAnnotation: $FlowFixMe,
177-
language: ParserType,
178+
parser: Parser,
178179
nullable: boolean,
179180
): Nullable<NativeModulePromiseTypeAnnotation> {
180181
assertGenericTypeAnnotationHasExactlyOneTypeParameter(
181182
hasteModuleName,
182183
typeAnnotation,
183-
language,
184+
parser,
184185
);
185186

186187
return wrapNullable(nullable, {

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,19 +213,14 @@ function translateTypeAnnotation(
213213
return emitRootTag(nullable);
214214
}
215215
case 'Promise': {
216-
return emitPromise(
217-
hasteModuleName,
218-
typeAnnotation,
219-
language,
220-
nullable,
221-
);
216+
return emitPromise(hasteModuleName, typeAnnotation, parser, nullable);
222217
}
223218
case 'Array':
224219
case 'ReadonlyArray': {
225220
assertGenericTypeAnnotationHasExactlyOneTypeParameter(
226221
hasteModuleName,
227222
typeAnnotation,
228-
language,
223+
parser,
229224
);
230225

231226
return translateArrayTypeAnnotation(

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import type {ParserType} from '../errors';
1414
import type {Parser} from '../parser';
1515

1616
class TypeScriptParser implements Parser {
17+
typeParameterInstantiation: string = 'TSTypeParameterInstantiation';
18+
1719
getMaybeEnumMemberType(maybeEnumDeclaration: $FlowFixMe): string {
1820
if (maybeEnumDeclaration.members[0].initializer) {
1921
return maybeEnumDeclaration.members[0].initializer.type

0 commit comments

Comments
 (0)