Skip to content

Commit

Permalink
Extract contents of the case 'NumberTypeAnnotation' into a single emi…
Browse files Browse the repository at this point in the history
…tNumber function (#34908)

Summary:
Part of #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: #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
  • Loading branch information
youedd authored and facebook-github-bot committed Oct 10, 2022
1 parent 74fda10 commit 54fc62c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
});
});
10 changes: 6 additions & 4 deletions packages/react-native-codegen/src/parsers/flow/modules/index.js
Expand Up @@ -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,
Expand Down Expand Up @@ -364,9 +368,7 @@ function translateTypeAnnotation(
return emitBoolean(nullable);
}
case 'NumberTypeAnnotation': {
return wrapNullable(nullable, {
type: 'NumberTypeAnnotation',
});
return emitNumber(nullable);
}
case 'VoidTypeAnnotation': {
return wrapNullable(nullable, {
Expand Down
10 changes: 10 additions & 0 deletions packages/react-native-codegen/src/parsers/parsers-primitives.js
Expand Up @@ -13,6 +13,7 @@
import type {
BooleanTypeAnnotation,
Int32TypeAnnotation,
NativeModuleNumberTypeAnnotation,
Nullable,
} from '../CodegenSchema';

Expand All @@ -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,
};
Expand Up @@ -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,
Expand Down Expand Up @@ -399,9 +403,7 @@ function translateTypeAnnotation(
return emitBoolean(nullable);
}
case 'TSNumberKeyword': {
return wrapNullable(nullable, {
type: 'NumberTypeAnnotation',
});
return emitNumber(nullable);
}
case 'TSVoidKeyword': {
return wrapNullable(nullable, {
Expand Down

0 comments on commit 54fc62c

Please sign in to comment.