Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const {
typeAliasResolution,
typeEnumResolution,
Visitor,
emitStringProp,
} = require('../parsers-primitives.js');
const {MockedParser} = require('../parserMock');
const {emitUnion} = require('../parsers-primitives');
Expand Down Expand Up @@ -148,6 +149,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 @@ -22,6 +22,7 @@ const {
throwIfArgumentPropsAreNull,
} = require('../../error-utils');
const {getEventArgument} = require('../../parsers-commons');
const {emitStringProp} = require('../../parsers-primitives');

function getPropertyType(
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
Expand All @@ -43,13 +44,7 @@ function getPropertyType(
},
};
case 'StringTypeAnnotation':
return {
name,
optional,
typeAnnotation: {
type: 'StringTypeAnnotation',
},
};
return emitStringProp(name, optional);
case 'Int32':
return {
name,
Expand Down
15 changes: 15 additions & 0 deletions packages/react-native-codegen/src/parsers/parsers-primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import type {
VoidTypeAnnotation,
NativeModuleObjectTypeAnnotation,
NativeModuleEnumDeclaration,
NamedShape,
} from '../CodegenSchema';
import type {Parser} from './parser';
import type {
Expand Down Expand Up @@ -148,6 +149,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 @@ -593,6 +607,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,6 +25,7 @@ const {
throwIfArgumentPropsAreNull,
} = require('../../error-utils');
const {getEventArgument} = require('../../parsers-commons');
const {emitStringProp} = require('../../parsers-primitives');
function getPropertyType(
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
* LTI update could not be added via codemod */
Expand Down Expand Up @@ -53,13 +54,7 @@ function getPropertyType(
},
};
case 'TSStringKeyword':
return {
name,
optional,
typeAnnotation: {
type: 'StringTypeAnnotation',
},
};
return emitStringProp(name, optional);
case 'Int32':
return {
name,
Expand Down