Skip to content

Commit

Permalink
Create emitStringProp function (#37527)
Browse files Browse the repository at this point in the history
Summary:
> Create a function emitStringProp(name: string, optional: boolean) in parser-primitives.js. Factor out the code from [Flow](https://github.com/facebook/react-native/blob/d8ced6f8953cd896471983714e722caf50783960/packages/react-native-codegen/src/parsers/flow/components/events.js#L45-L51) and [TypeScript](https://github.com/facebook/react-native/blob/d8ced6f8953cd896471983714e722caf50783960/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: #37527

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

Reviewed By: cortinico

Differential Revision: D46144200

Pulled By: cipolleschi

fbshipit-source-id: 076b530905ba7c28cfb2151e29e589026010c3c3
  • Loading branch information
cloudpresser authored and facebook-github-bot committed May 25, 2023
1 parent 66f4a91 commit 7062398
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const {
typeAliasResolution,
typeEnumResolution,
Visitor,
emitStringProp,
} = require('../parsers-primitives.js');
const {MockedParser} = require('../parserMock');
const {emitUnion} = require('../parsers-primitives');
Expand Down Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions packages/react-native-codegen/src/parsers/parsers-primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down Expand Up @@ -609,6 +622,7 @@ module.exports = {
emitVoid,
emitString,
emitStringish,
emitStringProp,
emitMixed,
emitUnion,
emitPartial,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit 7062398

Please sign in to comment.