Extract contents of the case 'NumberTypeAnnotation' into a single emitNumber function (#34908)

Summary:
Part of https://github.com/facebook/react-native/issues/34872

This PR:
- extracts the content of the case 'NumberTypeAnnotation' ([Flow](https://github.com/facebook/react-native/blob/b444f0e44e0d8670139acea5f14c2de32c5e2ddc/packages/react-native-codegen/src/parsers/flow/modules/index.js#L370-L372), [TypeScript](https://github.com/facebook/react-native/blob/00b795642a6562fb52d6df12e367b84674994623/packages/react-native-codegen/src/parsers/typescript/modules/index.js#L405-L407)) into a single emitNumber function in the parsers-primitives.js file, and uses the new function in the parsers
- unit tests emitNumber function

## Changelog

[Internal] [Changed] - Extract contents of the case 'NumberTypeAnnotation' into a single emitNumber function

Pull Request resolved: https://github.com/facebook/react-native/pull/34908

Test Plan:
` yarn jest react-native-codegen`

<img width="935" alt="image" src="https://user-images.githubusercontent.com/19575877/194764122-44975a97-3acd-4f27-babe-ddcd58c3ec61.png">

Reviewed By: cipolleschi

Differential Revision: D40214542

Pulled By: cipolleschi

fbshipit-source-id: a7746d3f1dd5f127dc520c6156383b18e00281ec
This commit is contained in:
Youssef Eddibili
2022-10-09 17:34:10 -07:00
committed by Facebook GitHub Bot
parent 74fda10702
commit 54fc62cda7
4 changed files with 53 additions and 9 deletions
@@ -11,7 +11,11 @@
'use-strict';
const {emitBoolean, emitInt32} = require('../parsers-primitives.js');
const {
emitBoolean,
emitNumber,
emitInt32,
} = require('../parsers-primitives.js');
describe('emitBoolean', () => {
describe('when nullable is true', () => {
@@ -64,3 +68,29 @@ describe('emitInt32', () => {
});
});
});
describe('emitNumber', () => {
describe('when nullable is true', () => {
it('returns nullable type annotation', () => {
const result = emitNumber(true);
const expected = {
type: 'NullableTypeAnnotation',
typeAnnotation: {
type: 'NumberTypeAnnotation',
},
};
expect(result).toEqual(expected);
});
});
describe('when nullable is false', () => {
it('returns non nullable type annotation', () => {
const result = emitNumber(false);
const expected = {
type: 'NumberTypeAnnotation',
};
expect(result).toEqual(expected);
});
});
});
@@ -33,7 +33,11 @@ const {
isModuleRegistryCall,
} = require('../utils.js');
const {unwrapNullable, wrapNullable} = require('../../parsers-commons');
const {emitBoolean, emitInt32} = require('../../parsers-primitives');
const {
emitBoolean,
emitNumber,
emitInt32,
} = require('../../parsers-primitives');
const {
IncorrectlyParameterizedFlowGenericParserError,
MisnamedModuleFlowInterfaceParserError,
@@ -364,9 +368,7 @@ function translateTypeAnnotation(
return emitBoolean(nullable);
}
case 'NumberTypeAnnotation': {
return wrapNullable(nullable, {
type: 'NumberTypeAnnotation',
});
return emitNumber(nullable);
}
case 'VoidTypeAnnotation': {
return wrapNullable(nullable, {
@@ -13,6 +13,7 @@
import type {
BooleanTypeAnnotation,
Int32TypeAnnotation,
NativeModuleNumberTypeAnnotation,
Nullable,
} from '../CodegenSchema';
@@ -30,7 +31,16 @@ function emitInt32(nullable: boolean): Nullable<Int32TypeAnnotation> {
});
}
function emitNumber(
nullable: boolean,
): Nullable<NativeModuleNumberTypeAnnotation> {
return wrapNullable(nullable, {
type: 'NumberTypeAnnotation',
});
}
module.exports = {
emitBoolean,
emitInt32,
emitNumber,
};
@@ -33,7 +33,11 @@ const {
isModuleRegistryCall,
} = require('../utils.js');
const {unwrapNullable, wrapNullable} = require('../../parsers-commons');
const {emitBoolean, emitInt32} = require('../../parsers-primitives');
const {
emitBoolean,
emitNumber,
emitInt32,
} = require('../../parsers-primitives');
const {
IncorrectlyParameterizedTypeScriptGenericParserError,
MisnamedModuleTypeScriptInterfaceParserError,
@@ -399,9 +403,7 @@ function translateTypeAnnotation(
return emitBoolean(nullable);
}
case 'TSNumberKeyword': {
return wrapNullable(nullable, {
type: 'NumberTypeAnnotation',
});
return emitNumber(nullable);
}
case 'TSVoidKeyword': {
return wrapNullable(nullable, {