Skip to content

Commit 7a2e346

Browse files
tarunrajputfacebook-github-bot
authored andcommitted
Extract contents of the case 'BooleanTypeAnnotation' into a single emitBoolean function (#34907)
Summary: Part of #34872 This PR extracts the content of the case 'BooleanTypeAnnotation' ([Flow](https://github.com./facebook/react-native/blob/b444f0e44e0d8670139acea5f14c2de32c5e2ddc/packages/react-native-codegen/src/parsers/flow/modules/index.js#L365-L367), [TypeScript](https://github.com./facebook/react-native/blob/00b795642a6562fb52d6df12e367b84674994623/packages/react-native-codegen/src/parsers/typescript/modules/index.js#L400-L402)) into a single emitBoolean function in the parsers-primitives.js file. Use the new function in the parsers. ## 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 'BooleanTypeAnnotation' into a single emitBoolean function Pull Request resolved: #34907 Test Plan: Run ```yarn jest react-native-codegen``` <img width="803" alt="image" src="https://user-images.githubusercontent.com/34857453/194751101-3b1a619e-332a-43a9-bfa3-35f5869d7c5f.png"> Reviewed By: cipolleschi Differential Revision: D40212518 Pulled By: cipolleschi fbshipit-source-id: 9c1462c1e51a1836f963866dc4101f081898df00
1 parent 7227bde commit 7a2e346

File tree

4 files changed

+69
-6
lines changed

4 files changed

+69
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
* @oncall react_native
10+
*/
11+
12+
'use-strict';
13+
14+
const {emitBoolean} = require('../parsers-primitives.js');
15+
16+
describe('emitBoolean', () => {
17+
describe('when nullable is true', () => {
18+
it('returns nullable type annotation', () => {
19+
const result = emitBoolean(true);
20+
const expected = {
21+
type: 'NullableTypeAnnotation',
22+
typeAnnotation: {
23+
type: 'BooleanTypeAnnotation',
24+
},
25+
};
26+
27+
expect(result).toEqual(expected);
28+
});
29+
});
30+
describe('when nullable is false', () => {
31+
it('returns non nullable type annotation', () => {
32+
const result = emitBoolean(false);
33+
const expected = {
34+
type: 'BooleanTypeAnnotation',
35+
};
36+
37+
expect(result).toEqual(expected);
38+
});
39+
});
40+
});

packages/react-native-codegen/src/parsers/flow/modules/index.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const {
3333
isModuleRegistryCall,
3434
} = require('../utils.js');
3535
const {unwrapNullable, wrapNullable} = require('../../parsers-commons');
36+
const {emitBoolean} = require('../../parsers-primitives');
3637
const {
3738
IncorrectlyParameterizedFlowGenericParserError,
3839
MisnamedModuleFlowInterfaceParserError,
@@ -362,9 +363,7 @@ function translateTypeAnnotation(
362363
});
363364
}
364365
case 'BooleanTypeAnnotation': {
365-
return wrapNullable(nullable, {
366-
type: 'BooleanTypeAnnotation',
367-
});
366+
return emitBoolean(nullable);
368367
}
369368
case 'NumberTypeAnnotation': {
370369
return wrapNullable(nullable, {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @flow strict
9+
*/
10+
11+
'use strict';
12+
13+
import type {BooleanTypeAnnotation, Nullable} from '../CodegenSchema';
14+
15+
const {wrapNullable} = require('./parsers-commons');
16+
17+
function emitBoolean(nullable: boolean): Nullable<BooleanTypeAnnotation> {
18+
return wrapNullable(nullable, {
19+
type: 'BooleanTypeAnnotation',
20+
});
21+
}
22+
23+
module.exports = {
24+
emitBoolean,
25+
};

packages/react-native-codegen/src/parsers/typescript/modules/index.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const {
3333
isModuleRegistryCall,
3434
} = require('../utils.js');
3535
const {unwrapNullable, wrapNullable} = require('../../parsers-commons');
36+
const {emitBoolean} = require('../../parsers-primitives');
3637
const {
3738
IncorrectlyParameterizedTypeScriptGenericParserError,
3839
MisnamedModuleTypeScriptInterfaceParserError,
@@ -397,9 +398,7 @@ function translateTypeAnnotation(
397398
});
398399
}
399400
case 'TSBooleanKeyword': {
400-
return wrapNullable(nullable, {
401-
type: 'BooleanTypeAnnotation',
402-
});
401+
return emitBoolean(nullable);
403402
}
404403
case 'TSNumberKeyword': {
405404
return wrapNullable(nullable, {

0 commit comments

Comments
 (0)