Skip to content

Commit

Permalink
chore: Extract codegen case 'Float' into a single emitFloat function (#…
Browse files Browse the repository at this point in the history
…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 #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: #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
  • Loading branch information
gabrieldonadel authored and facebook-github-bot committed Oct 29, 2022
1 parent cec9a34 commit 87d6580
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
const {
emitBoolean,
emitDouble,
emitFloat,
emitNumber,
emitInt32,
emitObject,
Expand Down Expand Up @@ -424,4 +425,30 @@ describe('emitObject', () => {
expect(result).toEqual(expected);
});
});

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

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

expect(result).toEqual(expected);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const {
const {
emitBoolean,
emitDouble,
emitFloat,
emitFunction,
emitNumber,
emitInt32,
Expand Down Expand Up @@ -242,9 +243,7 @@ function translateTypeAnnotation(
return emitDouble(nullable);
}
case 'Float': {
return wrapNullable(nullable, {
type: 'FloatTypeAnnotation',
});
return emitFloat(nullable);
}
case 'UnsafeObject':
case 'Object': {
Expand Down
10 changes: 10 additions & 0 deletions packages/react-native-codegen/src/parsers/parsers-primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
NativeModulePromiseTypeAnnotation,
StringTypeAnnotation,
VoidTypeAnnotation,
NativeModuleFloatTypeAnnotation,
} from '../CodegenSchema';
import type {ParserType} from './errors';
import type {TypeAliasResolutionStatus} from './utils';
Expand Down Expand Up @@ -171,9 +172,18 @@ function emitObject(
});
}
function emitFloat(
nullable: boolean,
): Nullable<NativeModuleFloatTypeAnnotation> {
return wrapNullable(nullable, {
type: 'FloatTypeAnnotation',
});
}

module.exports = {
emitBoolean,
emitDouble,
emitFloat,
emitFunction,
emitInt32,
emitNumber,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const {
const {
emitBoolean,
emitDouble,
emitFloat,
emitFunction,
emitNumber,
emitInt32,
Expand Down Expand Up @@ -255,9 +256,7 @@ function translateTypeAnnotation(
return emitDouble(nullable);
}
case 'Float': {
return wrapNullable(nullable, {
type: 'FloatTypeAnnotation',
});
return emitFloat(nullable);
}
case 'UnsafeObject':
case 'Object': {
Expand Down

0 comments on commit 87d6580

Please sign in to comment.