Skip to content

Commit 87d6580

Browse files
gabrieldonadelfacebook-github-bot
authored andcommitted
chore: Extract codegen case 'Float' into a single emitFloat function (facebook#35124)
Summary: ## Summary This PR extracts the content of the codegen case `'Float'` into a single `emitFloat` function inside the `parsers-primitives.js` file and uses it in both Flow and TypeScript parsers as requested on facebook#34872. This also adds unit tests to the new `emitFloat` function. ## Changelog [Internal] [Changed] - Extract the content of the case 'Float' into a single emitFloat function Pull Request resolved: facebook#35124 Test Plan: Run `yarn jest react-native-codegen` and ensure CI is green ![image](https://user-images.githubusercontent.com/11707729/198704932-202e2cd7-5b04-4009-b47e-b4999fee6c98.png) Reviewed By: rshest Differential Revision: D40828746 Pulled By: cipolleschi fbshipit-source-id: 9c7cecf7268f16aaef29065c1983ad9a4dd18dbe
1 parent cec9a34 commit 87d6580

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

packages/react-native-codegen/src/parsers/__tests__/parsers-primitives-test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
const {
1515
emitBoolean,
1616
emitDouble,
17+
emitFloat,
1718
emitNumber,
1819
emitInt32,
1920
emitObject,
@@ -424,4 +425,30 @@ describe('emitObject', () => {
424425
expect(result).toEqual(expected);
425426
});
426427
});
428+
429+
describe('emitFloat', () => {
430+
describe('when nullable is true', () => {
431+
it('returns nullable type annotation', () => {
432+
const result = emitFloat(true);
433+
const expected = {
434+
type: 'NullableTypeAnnotation',
435+
typeAnnotation: {
436+
type: 'FloatTypeAnnotation',
437+
},
438+
};
439+
440+
expect(result).toEqual(expected);
441+
});
442+
});
443+
describe('when nullable is false', () => {
444+
it('returns non nullable type annotation', () => {
445+
const result = emitFloat(false);
446+
const expected = {
447+
type: 'FloatTypeAnnotation',
448+
};
449+
450+
expect(result).toEqual(expected);
451+
});
452+
});
453+
});
427454
});

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const {
4343
const {
4444
emitBoolean,
4545
emitDouble,
46+
emitFloat,
4647
emitFunction,
4748
emitNumber,
4849
emitInt32,
@@ -242,9 +243,7 @@ function translateTypeAnnotation(
242243
return emitDouble(nullable);
243244
}
244245
case 'Float': {
245-
return wrapNullable(nullable, {
246-
type: 'FloatTypeAnnotation',
247-
});
246+
return emitFloat(nullable);
248247
}
249248
case 'UnsafeObject':
250249
case 'Object': {

packages/react-native-codegen/src/parsers/parsers-primitives.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import type {
2626
NativeModulePromiseTypeAnnotation,
2727
StringTypeAnnotation,
2828
VoidTypeAnnotation,
29+
NativeModuleFloatTypeAnnotation,
2930
} from '../CodegenSchema';
3031
import type {ParserType} from './errors';
3132
import type {TypeAliasResolutionStatus} from './utils';
@@ -171,9 +172,18 @@ function emitObject(
171172
});
172173
}
173174

175+
function emitFloat(
176+
nullable: boolean,
177+
): Nullable<NativeModuleFloatTypeAnnotation> {
178+
return wrapNullable(nullable, {
179+
type: 'FloatTypeAnnotation',
180+
});
181+
}
182+
174183
module.exports = {
175184
emitBoolean,
176185
emitDouble,
186+
emitFloat,
177187
emitFunction,
178188
emitInt32,
179189
emitNumber,

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const {
4646
const {
4747
emitBoolean,
4848
emitDouble,
49+
emitFloat,
4950
emitFunction,
5051
emitNumber,
5152
emitInt32,
@@ -255,9 +256,7 @@ function translateTypeAnnotation(
255256
return emitDouble(nullable);
256257
}
257258
case 'Float': {
258-
return wrapNullable(nullable, {
259-
type: 'FloatTypeAnnotation',
260-
});
259+
return emitFloat(nullable);
261260
}
262261
case 'UnsafeObject':
263262
case 'Object': {

0 commit comments

Comments
 (0)