Skip to content

Commit eda90e5

Browse files
ken0nekfacebook-github-bot
authored andcommitted
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 #34872. This also adds unit tests to the new `emitString` function. ref: #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: #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
1 parent 790f40c commit eda90e5

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const {
2020
emitPromise,
2121
emitRootTag,
2222
emitVoid,
23+
emitString,
2324
emitStringish,
2425
typeAliasResolution,
2526
} = require('../parsers-primitives.js');
@@ -155,6 +156,33 @@ describe('emitStringish', () => {
155156
});
156157
});
157158

159+
describe('emitString', () => {
160+
describe('when nullable is true', () => {
161+
it('returns nullable type annotation', () => {
162+
const result = emitString(true);
163+
const expected = {
164+
type: 'NullableTypeAnnotation',
165+
typeAnnotation: {
166+
type: 'StringTypeAnnotation',
167+
},
168+
};
169+
170+
expect(result).toEqual(expected);
171+
});
172+
});
173+
174+
describe('when nullable is false', () => {
175+
it('returns non nullable type annotation', () => {
176+
const result = emitString(false);
177+
const expected = {
178+
type: 'StringTypeAnnotation',
179+
};
180+
181+
expect(result).toEqual(expected);
182+
});
183+
});
184+
});
185+
158186
describe('emitDouble', () => {
159187
describe('when nullable is true', () => {
160188
it('returns nullable type annotation', () => {

packages/react-native-codegen/src/parsers/flow/modules/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const {
4949
emitPromise,
5050
emitRootTag,
5151
emitVoid,
52+
emitString,
5253
emitStringish,
5354
typeAliasResolution,
5455
} = require('../../parsers-primitives');
@@ -354,9 +355,7 @@ function translateTypeAnnotation(
354355
return emitVoid(nullable);
355356
}
356357
case 'StringTypeAnnotation': {
357-
return wrapNullable(nullable, {
358-
type: 'StringTypeAnnotation',
359-
});
358+
return emitString(nullable);
360359
}
361360
case 'FunctionTypeAnnotation': {
362361
const translateFunctionTypeAnnotationValue: NativeModuleFunctionTypeAnnotation =

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ function emitFunction(
8686
return wrapNullable(nullable, translateFunctionTypeAnnotationValue);
8787
}
8888

89+
function emitString(nullable: boolean): Nullable<StringTypeAnnotation> {
90+
return wrapNullable(nullable, {
91+
type: 'StringTypeAnnotation',
92+
});
93+
}
94+
8995
function typeAliasResolution(
9096
typeAliasResolutionStatus: TypeAliasResolutionStatus,
9197
objectTypeAnnotation: ObjectTypeAnnotation<
@@ -174,7 +180,8 @@ module.exports = {
174180
emitObject,
175181
emitPromise,
176182
emitRootTag,
177-
emitStringish,
178183
emitVoid,
184+
emitString,
185+
emitStringish,
179186
typeAliasResolution,
180187
};

packages/react-native-codegen/src/parsers/typescript/modules/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const {
4949
emitPromise,
5050
emitRootTag,
5151
emitVoid,
52+
emitString,
5253
emitStringish,
5354
typeAliasResolution,
5455
} = require('../../parsers-primitives');
@@ -369,9 +370,7 @@ function translateTypeAnnotation(
369370
return emitVoid(nullable);
370371
}
371372
case 'TSStringKeyword': {
372-
return wrapNullable(nullable, {
373-
type: 'StringTypeAnnotation',
374-
});
373+
return emitString(nullable);
375374
}
376375
case 'TSFunctionType': {
377376
const translateFunctionTypeAnnotationValue: NativeModuleFunctionTypeAnnotation =

0 commit comments

Comments
 (0)