extract emitPromise from parsers modules to shared parsers-primitives (#34935)

Summary:
This PR is a task from https://github.com/facebook/react-native/issues/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 https://github.com/facebook/react-native/pull/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: https://github.com/facebook/react-native/pull/34935

Test Plan: I tested using jest and flow commands.

Reviewed By: cipolleschi

Differential Revision: D40257033

Pulled By: cipolleschi

fbshipit-source-id: 0246f43c6b688629e2de1259e7f535c2cf6dd0a4
This commit is contained in:
Antoine Doubovetzky
2022-10-11 05:22:59 -07:00
committed by Facebook GitHub Bot
parent 29e5655dee
commit 966f3cdea3
4 changed files with 101 additions and 11 deletions
@@ -18,6 +18,7 @@ const {
emitInt32,
emitRootTag,
typeAliasResolution,
emitPromise,
} = require('../parsers-primitives.js');
describe('emitBoolean', () => {
@@ -244,3 +245,73 @@ describe('typeAliasResolution', () => {
});
});
});
describe('emitPromise', () => {
const moduleName = 'testModuleName';
const language = 'Flow';
describe("when typeAnnotation doesn't have exactly one typeParameter", () => {
const typeAnnotation = {
typeParameters: {
params: [1, 2],
type: 'TypeParameterInstantiation',
},
id: {
name: 'typeAnnotationName',
},
};
it('throws an IncorrectlyParameterizedGenericParserError error', () => {
const nullable = false;
expect(() =>
emitPromise(moduleName, typeAnnotation, language, nullable),
).toThrow();
});
});
describe("when typeAnnotation doesn't has exactly one typeParameter", () => {
const typeAnnotation = {
typeParameters: {
params: [1],
type: 'TypeParameterInstantiation',
},
id: {
name: 'typeAnnotationName',
},
};
describe('when nullable is true', () => {
const nullable = true;
it('returns nullable type annotation', () => {
const result = emitPromise(
moduleName,
typeAnnotation,
language,
nullable,
);
const expected = {
type: 'NullableTypeAnnotation',
typeAnnotation: {
type: 'PromiseTypeAnnotation',
},
};
expect(result).toEqual(expected);
});
});
describe('when nullable is false', () => {
const nullable = false;
it('returns non nullable type annotation', () => {
const result = emitPromise(
moduleName,
typeAnnotation,
language,
nullable,
);
const expected = {
type: 'PromiseTypeAnnotation',
};
expect(result).toEqual(expected);
});
});
});
});
@@ -44,6 +44,7 @@ const {
emitInt32,
emitRootTag,
typeAliasResolution,
emitPromise,
} = require('../../parsers-primitives');
const {
MisnamedModuleInterfaceParserError,
@@ -95,15 +96,12 @@ function translateTypeAnnotation(
return emitRootTag(nullable);
}
case 'Promise': {
assertGenericTypeAnnotationHasExactlyOneTypeParameter(
return emitPromise(
hasteModuleName,
typeAnnotation,
language,
nullable,
);
return wrapNullable(nullable, {
type: 'PromiseTypeAnnotation',
});
}
case 'Array':
case '$ReadOnlyArray': {
@@ -21,10 +21,15 @@ import type {
Int32TypeAnnotation,
ReservedTypeAnnotation,
ObjectTypeAnnotation,
NativeModulePromiseTypeAnnotation,
} from '../CodegenSchema';
import type {ParserType} from './errors';
import type {TypeAliasResolutionStatus} from './utils';
const {wrapNullable} = require('./parsers-commons');
const {
wrapNullable,
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
} = require('./parsers-commons');
function emitBoolean(nullable: boolean): Nullable<BooleanTypeAnnotation> {
return wrapNullable(nullable, {
@@ -113,6 +118,23 @@ function typeAliasResolution(
});
}
function emitPromise(
hasteModuleName: string,
typeAnnotation: $FlowFixMe,
language: ParserType,
nullable: boolean,
): Nullable<NativeModulePromiseTypeAnnotation> {
assertGenericTypeAnnotationHasExactlyOneTypeParameter(
hasteModuleName,
typeAnnotation,
language,
);
return wrapNullable(nullable, {
type: 'PromiseTypeAnnotation',
});
}
module.exports = {
emitBoolean,
emitDouble,
@@ -120,4 +142,5 @@ module.exports = {
emitNumber,
emitRootTag,
typeAliasResolution,
emitPromise,
};
@@ -44,6 +44,7 @@ const {
emitInt32,
emitRootTag,
typeAliasResolution,
emitPromise,
} = require('../../parsers-primitives');
const {
MisnamedModuleInterfaceParserError,
@@ -207,15 +208,12 @@ function translateTypeAnnotation(
return emitRootTag(nullable);
}
case 'Promise': {
assertGenericTypeAnnotationHasExactlyOneTypeParameter(
return emitPromise(
hasteModuleName,
typeAnnotation,
language,
nullable,
);
return wrapNullable(nullable, {
type: 'PromiseTypeAnnotation',
});
}
case 'Array':
case 'ReadonlyArray': {