Extract the content of the case 'StringTypeAnnotation' into a single … (#34981)

Summary:
This PR extracts the content of the codegen case 'String' into a single `emitString` 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 `emitString` function.

ref: https://github.com/facebook/react-native/pull/34936

## 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 'StringTypeAnnotation' into a single emitString function

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

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

Reviewed By: cortinico

Differential Revision: D40376836

Pulled By: cipolleschi

fbshipit-source-id: feb1b07ec7fc2c333f5054f8cd8d18457d985257
This commit is contained in:
Ken Tominaga
2022-10-19 01:38:28 -07:00
committed by Facebook GitHub Bot
parent 790f40cfeb
commit eda90e5181
4 changed files with 40 additions and 7 deletions
@@ -20,6 +20,7 @@ const {
emitPromise,
emitRootTag,
emitVoid,
emitString,
emitStringish,
typeAliasResolution,
} = require('../parsers-primitives.js');
@@ -155,6 +156,33 @@ describe('emitStringish', () => {
});
});
describe('emitString', () => {
describe('when nullable is true', () => {
it('returns nullable type annotation', () => {
const result = emitString(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 = emitString(false);
const expected = {
type: 'StringTypeAnnotation',
};
expect(result).toEqual(expected);
});
});
});
describe('emitDouble', () => {
describe('when nullable is true', () => {
it('returns nullable type annotation', () => {
@@ -49,6 +49,7 @@ const {
emitPromise,
emitRootTag,
emitVoid,
emitString,
emitStringish,
typeAliasResolution,
} = require('../../parsers-primitives');
@@ -354,9 +355,7 @@ function translateTypeAnnotation(
return emitVoid(nullable);
}
case 'StringTypeAnnotation': {
return wrapNullable(nullable, {
type: 'StringTypeAnnotation',
});
return emitString(nullable);
}
case 'FunctionTypeAnnotation': {
const translateFunctionTypeAnnotationValue: NativeModuleFunctionTypeAnnotation =
@@ -86,6 +86,12 @@ function emitFunction(
return wrapNullable(nullable, translateFunctionTypeAnnotationValue);
}
function emitString(nullable: boolean): Nullable<StringTypeAnnotation> {
return wrapNullable(nullable, {
type: 'StringTypeAnnotation',
});
}
function typeAliasResolution(
typeAliasResolutionStatus: TypeAliasResolutionStatus,
objectTypeAnnotation: ObjectTypeAnnotation<
@@ -174,7 +180,8 @@ module.exports = {
emitObject,
emitPromise,
emitRootTag,
emitStringish,
emitVoid,
emitString,
emitStringish,
typeAliasResolution,
};
@@ -49,6 +49,7 @@ const {
emitPromise,
emitRootTag,
emitVoid,
emitString,
emitStringish,
typeAliasResolution,
} = require('../../parsers-primitives');
@@ -369,9 +370,7 @@ function translateTypeAnnotation(
return emitVoid(nullable);
}
case 'TSStringKeyword': {
return wrapNullable(nullable, {
type: 'StringTypeAnnotation',
});
return emitString(nullable);
}
case 'TSFunctionType': {
const translateFunctionTypeAnnotationValue: NativeModuleFunctionTypeAnnotation =