Skip to content

Commit

Permalink
Extract the content of the case 'Int32' (Flow, TypeScript) into a sin…
Browse files Browse the repository at this point in the history
…gle emitInt32 function in the parsers-primitives.js file. (#34906)

Summary:
Part of #34872

Refactors emitInt32 in codegen parser module into a common parsers-primitive.js

## 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 the content of the case 'Int32' (Flow, TypeScript) into a single emitInt32 function

Pull Request resolved: #34906

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

<img width="690" alt="Screen Shot 2022-10-09 at 15 45 21" src="https://user-images.githubusercontent.com/6936373/194742142-801a89c8-f803-4e8d-9b05-cf6bda97ea19.png">

Reviewed By: cipolleschi

Differential Revision: D40212544

Pulled By: cipolleschi

fbshipit-source-id: 845e3758ab654edb972a8d8dc3cfa73464e2d49d
  • Loading branch information
Naturalclar authored and facebook-github-bot committed Oct 9, 2022
1 parent 7a2e346 commit db8c11d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

'use-strict';

const {emitBoolean} = require('../parsers-primitives.js');
const {emitBoolean, emitInt32} = require('../parsers-primitives.js');

describe('emitBoolean', () => {
describe('when nullable is true', () => {
Expand All @@ -38,3 +38,29 @@ describe('emitBoolean', () => {
});
});
});

describe('emitInt32', () => {
describe('when nullable is true', () => {
it('returns nullable type annotation', () => {
const result = emitInt32(true);
const expected = {
type: 'NullableTypeAnnotation',
typeAnnotation: {
type: 'Int32TypeAnnotation',
},
};

expect(result).toEqual(expected);
});
});
describe('when nullable is false', () => {
it('returns non nullable type annotation', () => {
const result = emitInt32(false);
const expected = {
type: 'Int32TypeAnnotation',
};

expect(result).toEqual(expected);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const {
isModuleRegistryCall,
} = require('../utils.js');
const {unwrapNullable, wrapNullable} = require('../../parsers-commons');
const {emitBoolean} = require('../../parsers-primitives');
const {emitBoolean, emitInt32} = require('../../parsers-primitives');
const {
IncorrectlyParameterizedFlowGenericParserError,
MisnamedModuleFlowInterfaceParserError,
Expand Down Expand Up @@ -195,9 +195,7 @@ function translateTypeAnnotation(
});
}
case 'Int32': {
return wrapNullable(nullable, {
type: 'Int32TypeAnnotation',
});
return emitInt32(nullable);
}
case 'Double': {
return wrapNullable(nullable, {
Expand Down
13 changes: 12 additions & 1 deletion packages/react-native-codegen/src/parsers/parsers-primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

'use strict';

import type {BooleanTypeAnnotation, Nullable} from '../CodegenSchema';
import type {
BooleanTypeAnnotation,
Int32TypeAnnotation,
Nullable,
} from '../CodegenSchema';

const {wrapNullable} = require('./parsers-commons');

Expand All @@ -20,6 +24,13 @@ function emitBoolean(nullable: boolean): Nullable<BooleanTypeAnnotation> {
});
}

function emitInt32(nullable: boolean): Nullable<Int32TypeAnnotation> {
return wrapNullable(nullable, {
type: 'Int32TypeAnnotation',
});
}

module.exports = {
emitBoolean,
emitInt32,
};
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const {
isModuleRegistryCall,
} = require('../utils.js');
const {unwrapNullable, wrapNullable} = require('../../parsers-commons');
const {emitBoolean} = require('../../parsers-primitives');
const {emitBoolean, emitInt32} = require('../../parsers-primitives');
const {
IncorrectlyParameterizedTypeScriptGenericParserError,
MisnamedModuleTypeScriptInterfaceParserError,
Expand Down Expand Up @@ -228,9 +228,7 @@ function translateTypeAnnotation(
});
}
case 'Int32': {
return wrapNullable(nullable, {
type: 'Int32TypeAnnotation',
});
return emitInt32(nullable);
}
case 'Double': {
return wrapNullable(nullable, {
Expand Down

0 comments on commit db8c11d

Please sign in to comment.