Summary:
> Create a function emitStringProp(name: string, optional: boolean) in parser-primitives.js. Factor out the code from [Flow](d8ced6f895/packages/react-native-codegen/src/parsers/flow/components/events.js (L45-L51)) and [TypeScript](d8ced6f895/packages/react-native-codegen/src/parsers/typescript/components/events.js (L57-L61)) into that function. Use that function in the original call site.

bypass-github-export-checks

## Changelog:

[INTERNAL][ADDED] - emitStringProp in parser-primitves

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

Test Plan: `yarn jest packages/react-native-codegen`

Reviewed By: cortinico

Differential Revision: D46144200

Pulled By: cipolleschi

fbshipit-source-id: 076b530905ba7c28cfb2151e29e589026010c3c3
This commit is contained in:
Luiz Ozorio 2023-05-25 02:59:23 -07:00 коммит произвёл Facebook GitHub Bot
Родитель 66f4a9168b
Коммит 706239814e
4 изменённых файлов: 51 добавлений и 16 удалений

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

@ -34,6 +34,7 @@ const {
typeAliasResolution,
typeEnumResolution,
Visitor,
emitStringProp,
} = require('../parsers-primitives.js');
const {MockedParser} = require('../parserMock');
const {emitUnion} = require('../parsers-primitives');
@ -149,6 +150,38 @@ describe('emitRootTag', () => {
});
});
describe('emitStringProp', () => {
describe('when optional is true', () => {
it('returns optional StringTypeAnnotation', () => {
const result = emitStringProp('myProp', true);
const expected = {
name: 'myProp',
optional: true,
typeAnnotation: {
type: 'StringTypeAnnotation',
},
};
expect(result).toEqual(expected);
});
});
describe('when nullable is false', () => {
it('returns required StringTypeAnnotatio', () => {
const result = emitStringProp('myProp', false);
const expected = {
name: 'myProp',
optional: false,
typeAnnotation: {
type: 'StringTypeAnnotation',
},
};
expect(result).toEqual(expected);
});
});
});
describe('emitStringish', () => {
describe('when nullable is true', () => {
it('returns nullable type annotation', () => {

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

@ -23,7 +23,7 @@ const {
throwIfArgumentPropsAreNull,
} = require('../../error-utils');
const {getEventArgument} = require('../../parsers-commons');
const {emitBoolProp} = require('../../parsers-primitives');
const {emitBoolProp, emitStringProp} = require('../../parsers-primitives');
function getPropertyType(
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
@ -39,13 +39,7 @@ function getPropertyType(
case 'BooleanTypeAnnotation':
return emitBoolProp(name, optional);
case 'StringTypeAnnotation':
return {
name,
optional,
typeAnnotation: {
type: 'StringTypeAnnotation',
},
};
return emitStringProp(name, optional);
case 'Int32':
return {
name,

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

@ -150,6 +150,19 @@ function emitString(nullable: boolean): Nullable<StringTypeAnnotation> {
});
}
function emitStringProp(
name: string,
optional: boolean,
): NamedShape<StringTypeAnnotation> {
return {
name,
optional,
typeAnnotation: {
type: 'StringTypeAnnotation',
},
};
}
function typeAliasResolution(
typeResolution: TypeResolutionStatus,
objectTypeAnnotation: ObjectTypeAnnotation<
@ -609,6 +622,7 @@ module.exports = {
emitVoid,
emitString,
emitStringish,
emitStringProp,
emitMixed,
emitUnion,
emitPartial,

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

@ -25,7 +25,7 @@ const {
throwIfArgumentPropsAreNull,
} = require('../../error-utils');
const {getEventArgument} = require('../../parsers-commons');
const {emitBoolProp} = require('../../parsers-primitives');
const {emitBoolProp, emitStringProp} = require('../../parsers-primitives');
function getPropertyType(
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
@ -49,13 +49,7 @@ function getPropertyType(
case 'TSBooleanKeyword':
return emitBoolProp(name, optional);
case 'TSStringKeyword':
return {
name,
optional,
typeAnnotation: {
type: 'StringTypeAnnotation',
},
};
return emitStringProp(name, optional);
case 'Int32':
return {
name,