Skip to content

Commit

Permalink
extract emitPromise from parsers modules to shared parsers-primitives (
Browse files Browse the repository at this point in the history
…#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
  • Loading branch information
AntoineDoubovetzky authored and facebook-github-bot committed Oct 11, 2022
1 parent 29e5655 commit 966f3cd
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 11 deletions.
Expand Up @@ -18,6 +18,7 @@ const {
emitInt32,
emitRootTag,
typeAliasResolution,
emitPromise,
} = require('../parsers-primitives.js');

describe('emitBoolean', () => {
Expand Down Expand Up @@ -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);
});
});
});
});
Expand Up @@ -44,6 +44,7 @@ const {
emitInt32,
emitRootTag,
typeAliasResolution,
emitPromise,
} = require('../../parsers-primitives');
const {
MisnamedModuleInterfaceParserError,
Expand Down Expand Up @@ -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': {
Expand Down
25 changes: 24 additions & 1 deletion packages/react-native-codegen/src/parsers/parsers-primitives.js
Expand Up @@ -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, {
Expand Down Expand Up @@ -113,11 +118,29 @@ 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,
emitInt32,
emitNumber,
emitRootTag,
typeAliasResolution,
emitPromise,
};
Expand Up @@ -44,6 +44,7 @@ const {
emitInt32,
emitRootTag,
typeAliasResolution,
emitPromise,
} = require('../../parsers-primitives');
const {
MisnamedModuleInterfaceParserError,
Expand Down Expand Up @@ -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': {
Expand Down

0 comments on commit 966f3cd

Please sign in to comment.