Skip to content

Commit b87d371

Browse files
tarunrajputfacebook-github-bot
authored andcommitted
extract emitMixedTypeAnnotation to parsers-commons (#34954)
Summary: Create a function emitMixedTypeAnnotation into the parsers-commons.js file to put together this code from [Flow](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/flow/modules/index.js#L400-L404) and [TypeScript](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/typescript/modules/index.js#L435-L440). Part of #34872 ## 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 emitMixedTypeAnnotation into the parsers-commons Pull Request resolved: #34954 Test Plan: run ```yarn jest react-native-codegen``` <img width="781" alt="image" src="https://user-images.githubusercontent.com/34857453/195291603-f138c64c-1b49-41af-a133-ee366d02706a.png"> Reviewed By: NickGerleman Differential Revision: D40297103 Pulled By: cipolleschi fbshipit-source-id: 3ee3569fbfa850ac50df18b74ecc3eefbeb267c9
1 parent c403cd4 commit b87d371

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
import {IncorrectlyParameterizedGenericParserError} from '../errors';
1515
import {assertGenericTypeAnnotationHasExactlyOneTypeParameter} from '../parsers-commons';
1616

17-
const {wrapNullable, unwrapNullable} = require('../parsers-commons.js');
17+
const {
18+
wrapNullable,
19+
unwrapNullable,
20+
emitMixedTypeAnnotation,
21+
} = require('../parsers-commons.js');
1822

1923
describe('wrapNullable', () => {
2024
describe('when nullable is true', () => {
@@ -172,3 +176,29 @@ describe('assertGenericTypeAnnotationHasExactlyOneTypeParameter', () => {
172176
).toThrow(IncorrectlyParameterizedGenericParserError);
173177
});
174178
});
179+
180+
describe('emitMixedTypeAnnotation', () => {
181+
describe('when nullable is true', () => {
182+
it('returns nullable type annotation', () => {
183+
const result = emitMixedTypeAnnotation(true);
184+
const expected = {
185+
type: 'NullableTypeAnnotation',
186+
typeAnnotation: {
187+
type: 'MixedTypeAnnotation',
188+
},
189+
};
190+
191+
expect(result).toEqual(expected);
192+
});
193+
});
194+
describe('when nullable is false', () => {
195+
it('returns non nullable type annotation', () => {
196+
const result = emitMixedTypeAnnotation(false);
197+
const expected = {
198+
type: 'MixedTypeAnnotation',
199+
};
200+
201+
expect(result).toEqual(expected);
202+
});
203+
});
204+
});

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const {
3737
unwrapNullable,
3838
wrapNullable,
3939
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
40+
emitMixedTypeAnnotation,
4041
} = require('../../parsers-commons');
4142
const {
4243
emitBoolean,
@@ -414,9 +415,7 @@ function translateTypeAnnotation(
414415
}
415416
case 'MixedTypeAnnotation': {
416417
if (cxxOnly) {
417-
return wrapNullable(nullable, {
418-
type: 'MixedTypeAnnotation',
419-
});
418+
return emitMixedTypeAnnotation(nullable);
420419
}
421420
// Fallthrough
422421
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
NativeModuleSchema,
1616
NativeModuleTypeAnnotation,
1717
Nullable,
18+
NativeModuleMixedTypeAnnotation,
1819
} from '../CodegenSchema.js';
1920
const {IncorrectlyParameterizedGenericParserError} = require('./errors');
2021
import type {ParserType} from './errors';
@@ -91,9 +92,18 @@ function assertGenericTypeAnnotationHasExactlyOneTypeParameter(
9192
}
9293
}
9394

95+
function emitMixedTypeAnnotation(
96+
nullable: boolean,
97+
): Nullable<NativeModuleMixedTypeAnnotation> {
98+
return wrapNullable(nullable, {
99+
type: 'MixedTypeAnnotation',
100+
});
101+
}
102+
94103
module.exports = {
95104
wrapModuleSchema,
96105
unwrapNullable,
97106
wrapNullable,
98107
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
108+
emitMixedTypeAnnotation,
99109
};

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const {
3737
unwrapNullable,
3838
wrapNullable,
3939
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
40+
emitMixedTypeAnnotation,
4041
} = require('../../parsers-commons');
4142
const {
4243
emitBoolean,
@@ -430,9 +431,7 @@ function translateTypeAnnotation(
430431
}
431432
case 'TSUnknownKeyword': {
432433
if (cxxOnly) {
433-
return wrapNullable(nullable, {
434-
type: 'MixedTypeAnnotation',
435-
});
434+
return emitMixedTypeAnnotation(nullable);
436435
}
437436
// Fallthrough
438437
}

0 commit comments

Comments
 (0)