Skip to content

Commit 966f3cd

Browse files
Antoine Doubovetzkyfacebook-github-bot
Antoine Doubovetzky
authored andcommitted
extract emitPromise from parsers modules to shared parsers-primitives (#34935)
Summary: This PR is a task from #34872: > Extract the content of the case 'Promise' ([Flow](https://github.com/facebook/react-native/blob/b444f0e44e0d8670139acea5f14c2de32c5e2ddc/packages/react-native-codegen/src/parsers/flow/modules/index.js#L90-L97), [TypeScript](https://github.com/facebook/react-native/blob/00b795642a6562fb52d6df12e367b84674994623/packages/react-native-codegen/src/parsers/typescript/modules/index.js#L197-L205)) into a single emitPromise function in the parsers-primitives.js file. Use the new function in the parsers. Note that this PR should be merged after #34933 ## 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] - Extract contents of the case 'Promise' into a single emitPromise function inside parsers-primitives Pull Request resolved: #34935 Test Plan: I tested using jest and flow commands. Reviewed By: cipolleschi Differential Revision: D40257033 Pulled By: cipolleschi fbshipit-source-id: 0246f43c6b688629e2de1259e7f535c2cf6dd0a4
1 parent 29e5655 commit 966f3cd

File tree

4 files changed

+101
-11
lines changed

4 files changed

+101
-11
lines changed

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const {
1818
emitInt32,
1919
emitRootTag,
2020
typeAliasResolution,
21+
emitPromise,
2122
} = require('../parsers-primitives.js');
2223

2324
describe('emitBoolean', () => {
@@ -244,3 +245,73 @@ describe('typeAliasResolution', () => {
244245
});
245246
});
246247
});
248+
249+
describe('emitPromise', () => {
250+
const moduleName = 'testModuleName';
251+
const language = 'Flow';
252+
describe("when typeAnnotation doesn't have exactly one typeParameter", () => {
253+
const typeAnnotation = {
254+
typeParameters: {
255+
params: [1, 2],
256+
type: 'TypeParameterInstantiation',
257+
},
258+
id: {
259+
name: 'typeAnnotationName',
260+
},
261+
};
262+
it('throws an IncorrectlyParameterizedGenericParserError error', () => {
263+
const nullable = false;
264+
expect(() =>
265+
emitPromise(moduleName, typeAnnotation, language, nullable),
266+
).toThrow();
267+
});
268+
});
269+
270+
describe("when typeAnnotation doesn't has exactly one typeParameter", () => {
271+
const typeAnnotation = {
272+
typeParameters: {
273+
params: [1],
274+
type: 'TypeParameterInstantiation',
275+
},
276+
id: {
277+
name: 'typeAnnotationName',
278+
},
279+
};
280+
281+
describe('when nullable is true', () => {
282+
const nullable = true;
283+
it('returns nullable type annotation', () => {
284+
const result = emitPromise(
285+
moduleName,
286+
typeAnnotation,
287+
language,
288+
nullable,
289+
);
290+
const expected = {
291+
type: 'NullableTypeAnnotation',
292+
typeAnnotation: {
293+
type: 'PromiseTypeAnnotation',
294+
},
295+
};
296+
297+
expect(result).toEqual(expected);
298+
});
299+
});
300+
describe('when nullable is false', () => {
301+
const nullable = false;
302+
it('returns non nullable type annotation', () => {
303+
const result = emitPromise(
304+
moduleName,
305+
typeAnnotation,
306+
language,
307+
nullable,
308+
);
309+
const expected = {
310+
type: 'PromiseTypeAnnotation',
311+
};
312+
313+
expect(result).toEqual(expected);
314+
});
315+
});
316+
});
317+
});

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const {
4444
emitInt32,
4545
emitRootTag,
4646
typeAliasResolution,
47+
emitPromise,
4748
} = require('../../parsers-primitives');
4849
const {
4950
MisnamedModuleInterfaceParserError,
@@ -95,15 +96,12 @@ function translateTypeAnnotation(
9596
return emitRootTag(nullable);
9697
}
9798
case 'Promise': {
98-
assertGenericTypeAnnotationHasExactlyOneTypeParameter(
99+
return emitPromise(
99100
hasteModuleName,
100101
typeAnnotation,
101102
language,
103+
nullable,
102104
);
103-
104-
return wrapNullable(nullable, {
105-
type: 'PromiseTypeAnnotation',
106-
});
107105
}
108106
case 'Array':
109107
case '$ReadOnlyArray': {

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ import type {
2121
Int32TypeAnnotation,
2222
ReservedTypeAnnotation,
2323
ObjectTypeAnnotation,
24+
NativeModulePromiseTypeAnnotation,
2425
} from '../CodegenSchema';
26+
import type {ParserType} from './errors';
2527
import type {TypeAliasResolutionStatus} from './utils';
2628

27-
const {wrapNullable} = require('./parsers-commons');
29+
const {
30+
wrapNullable,
31+
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
32+
} = require('./parsers-commons');
2833

2934
function emitBoolean(nullable: boolean): Nullable<BooleanTypeAnnotation> {
3035
return wrapNullable(nullable, {
@@ -113,11 +118,29 @@ function typeAliasResolution(
113118
});
114119
}
115120

121+
function emitPromise(
122+
hasteModuleName: string,
123+
typeAnnotation: $FlowFixMe,
124+
language: ParserType,
125+
nullable: boolean,
126+
): Nullable<NativeModulePromiseTypeAnnotation> {
127+
assertGenericTypeAnnotationHasExactlyOneTypeParameter(
128+
hasteModuleName,
129+
typeAnnotation,
130+
language,
131+
);
132+
133+
return wrapNullable(nullable, {
134+
type: 'PromiseTypeAnnotation',
135+
});
136+
}
137+
116138
module.exports = {
117139
emitBoolean,
118140
emitDouble,
119141
emitInt32,
120142
emitNumber,
121143
emitRootTag,
122144
typeAliasResolution,
145+
emitPromise,
123146
};

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const {
4444
emitInt32,
4545
emitRootTag,
4646
typeAliasResolution,
47+
emitPromise,
4748
} = require('../../parsers-primitives');
4849
const {
4950
MisnamedModuleInterfaceParserError,
@@ -207,15 +208,12 @@ function translateTypeAnnotation(
207208
return emitRootTag(nullable);
208209
}
209210
case 'Promise': {
210-
assertGenericTypeAnnotationHasExactlyOneTypeParameter(
211+
return emitPromise(
211212
hasteModuleName,
212213
typeAnnotation,
213214
language,
215+
nullable,
214216
);
215-
216-
return wrapNullable(nullable, {
217-
type: 'PromiseTypeAnnotation',
218-
});
219217
}
220218
case 'Array':
221219
case 'ReadonlyArray': {

0 commit comments

Comments
 (0)