Skip to content

Commit 7062398

Browse files
cloudpresserfacebook-github-bot
authored andcommitted
Create emitStringProp function (#37527)
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
1 parent 66f4a91 commit 7062398

File tree

4 files changed

+51
-16
lines changed

4 files changed

+51
-16
lines changed

packages/react-native-codegen/src/parsers/__tests__/parsers-primitives-test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const {
3434
typeAliasResolution,
3535
typeEnumResolution,
3636
Visitor,
37+
emitStringProp,
3738
} = require('../parsers-primitives.js');
3839
const {MockedParser} = require('../parserMock');
3940
const {emitUnion} = require('../parsers-primitives');
@@ -149,6 +150,38 @@ describe('emitRootTag', () => {
149150
});
150151
});
151152

153+
describe('emitStringProp', () => {
154+
describe('when optional is true', () => {
155+
it('returns optional StringTypeAnnotation', () => {
156+
const result = emitStringProp('myProp', true);
157+
const expected = {
158+
name: 'myProp',
159+
optional: true,
160+
typeAnnotation: {
161+
type: 'StringTypeAnnotation',
162+
},
163+
};
164+
165+
expect(result).toEqual(expected);
166+
});
167+
});
168+
169+
describe('when nullable is false', () => {
170+
it('returns required StringTypeAnnotatio', () => {
171+
const result = emitStringProp('myProp', false);
172+
const expected = {
173+
name: 'myProp',
174+
optional: false,
175+
typeAnnotation: {
176+
type: 'StringTypeAnnotation',
177+
},
178+
};
179+
180+
expect(result).toEqual(expected);
181+
});
182+
});
183+
});
184+
152185
describe('emitStringish', () => {
153186
describe('when nullable is true', () => {
154187
it('returns nullable type annotation', () => {

packages/react-native-codegen/src/parsers/flow/components/events.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const {
2323
throwIfArgumentPropsAreNull,
2424
} = require('../../error-utils');
2525
const {getEventArgument} = require('../../parsers-commons');
26-
const {emitBoolProp} = require('../../parsers-primitives');
26+
const {emitBoolProp, emitStringProp} = require('../../parsers-primitives');
2727

2828
function getPropertyType(
2929
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
@@ -39,13 +39,7 @@ function getPropertyType(
3939
case 'BooleanTypeAnnotation':
4040
return emitBoolProp(name, optional);
4141
case 'StringTypeAnnotation':
42-
return {
43-
name,
44-
optional,
45-
typeAnnotation: {
46-
type: 'StringTypeAnnotation',
47-
},
48-
};
42+
return emitStringProp(name, optional);
4943
case 'Int32':
5044
return {
5145
name,

packages/react-native-codegen/src/parsers/parsers-primitives.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,19 @@ function emitString(nullable: boolean): Nullable<StringTypeAnnotation> {
150150
});
151151
}
152152

153+
function emitStringProp(
154+
name: string,
155+
optional: boolean,
156+
): NamedShape<StringTypeAnnotation> {
157+
return {
158+
name,
159+
optional,
160+
typeAnnotation: {
161+
type: 'StringTypeAnnotation',
162+
},
163+
};
164+
}
165+
153166
function typeAliasResolution(
154167
typeResolution: TypeResolutionStatus,
155168
objectTypeAnnotation: ObjectTypeAnnotation<
@@ -609,6 +622,7 @@ module.exports = {
609622
emitVoid,
610623
emitString,
611624
emitStringish,
625+
emitStringProp,
612626
emitMixed,
613627
emitUnion,
614628
emitPartial,

packages/react-native-codegen/src/parsers/typescript/components/events.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const {
2525
throwIfArgumentPropsAreNull,
2626
} = require('../../error-utils');
2727
const {getEventArgument} = require('../../parsers-commons');
28-
const {emitBoolProp} = require('../../parsers-primitives');
28+
const {emitBoolProp, emitStringProp} = require('../../parsers-primitives');
2929

3030
function getPropertyType(
3131
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
@@ -49,13 +49,7 @@ function getPropertyType(
4949
case 'TSBooleanKeyword':
5050
return emitBoolProp(name, optional);
5151
case 'TSStringKeyword':
52-
return {
53-
name,
54-
optional,
55-
typeAnnotation: {
56-
type: 'StringTypeAnnotation',
57-
},
58-
};
52+
return emitStringProp(name, optional);
5953
case 'Int32':
6054
return {
6155
name,

0 commit comments

Comments
 (0)