chore: Extract codegen case 'Stringish' into a single emitStringish function (#34936)

Summary:
This PR extracts the content of the codegen case `'Stringish'` into a single `emitStringish` function inside the `parsers-primitives.js` file and uses it in both Flow and TypeScript parsers as requested on https://github.com/facebook/react-native/issues/34872. This also adds unit tests to the new `emitStringish` function.

## Changelog

[Internal] [Changed] - Extract the content of the case 'Stringish' into a single emitStringish function

Pull Request resolved: https://github.com/facebook/react-native/pull/34936

Test Plan:
Run `yarn jest react-native-codegen` and ensure CI is green

![image](https://user-images.githubusercontent.com/11707729/194987664-b588b82b-a9e0-49a9-a3cc-a03cb0a230e6.png)

Reviewed By: cipolleschi

Differential Revision: D40255921

Pulled By: rshest

fbshipit-source-id: 9c08f81f12c93995bb6ba032fabcd6451b8dc7c1
This commit is contained in:
Gabriel Donadel Dall'Agnol 2022-10-12 03:06:04 -07:00 коммит произвёл Facebook GitHub Bot
Родитель b3219fe345
Коммит 305f7c3352
4 изменённых файлов: 40 добавлений и 6 удалений

Просмотреть файл

@ -20,6 +20,7 @@ const {
emitPromise,
emitRootTag,
emitVoid,
emitStringish,
typeAliasResolution,
} = require('../parsers-primitives.js');
@ -127,6 +128,33 @@ describe('emitRootTag', () => {
});
});
describe('emitStringish', () => {
describe('when nullable is true', () => {
it('returns nullable type annotation', () => {
const result = emitStringish(true);
const expected = {
type: 'NullableTypeAnnotation',
typeAnnotation: {
type: 'StringTypeAnnotation',
},
};
expect(result).toEqual(expected);
});
});
describe('when nullable is false', () => {
it('returns non nullable type annotation', () => {
const result = emitStringish(false);
const expected = {
type: 'StringTypeAnnotation',
};
expect(result).toEqual(expected);
});
});
});
describe('emitDouble', () => {
describe('when nullable is true', () => {
it('returns nullable type annotation', () => {

Просмотреть файл

@ -46,6 +46,7 @@ const {
emitPromise,
emitRootTag,
emitVoid,
emitStringish,
typeAliasResolution,
} = require('../../parsers-primitives');
const {
@ -203,9 +204,7 @@ function translateTypeAnnotation(
return wrapNullable(nullable || isParamNullable, paramType);
}
case 'Stringish': {
return wrapNullable(nullable, {
type: 'StringTypeAnnotation',
});
return emitStringish(nullable);
}
case 'Int32': {
return emitInt32(nullable);

Просмотреть файл

@ -24,6 +24,7 @@ import type {
ObjectTypeAnnotation,
NativeModulePromiseTypeAnnotation,
VoidTypeAnnotation,
StringTypeAnnotation,
} from '../CodegenSchema';
import type {ParserType} from './errors';
import type {TypeAliasResolutionStatus} from './utils';
@ -72,6 +73,12 @@ function emitVoid(nullable: boolean): Nullable<VoidTypeAnnotation> {
});
}
function emitStringish(nullable: boolean): Nullable<StringTypeAnnotation> {
return wrapNullable(nullable, {
type: 'StringTypeAnnotation',
});
}
function typeAliasResolution(
typeAliasResolutionStatus: TypeAliasResolutionStatus,
objectTypeAnnotation: ObjectTypeAnnotation<
@ -160,5 +167,6 @@ module.exports = {
emitPromise,
emitRootTag,
emitVoid,
emitStringish,
typeAliasResolution,
};

Просмотреть файл

@ -46,6 +46,7 @@ const {
emitPromise,
emitRootTag,
emitVoid,
emitStringish,
typeAliasResolution,
} = require('../../parsers-primitives');
const {
@ -236,9 +237,7 @@ function translateTypeAnnotation(
);
}
case 'Stringish': {
return wrapNullable(nullable, {
type: 'StringTypeAnnotation',
});
return emitStringish(nullable);
}
case 'Int32': {
return emitInt32(nullable);