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 https://github.com/facebook/react-native/issues/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: https://github.com/facebook/react-native/pull/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
This commit is contained in:
Tarun Chauhan 2022-10-14 03:09:51 -07:00 коммит произвёл Facebook GitHub Bot
Родитель c403cd4f11
Коммит b87d371a38
4 изменённых файлов: 45 добавлений и 7 удалений

Просмотреть файл

@ -14,7 +14,11 @@
import {IncorrectlyParameterizedGenericParserError} from '../errors';
import {assertGenericTypeAnnotationHasExactlyOneTypeParameter} from '../parsers-commons';
const {wrapNullable, unwrapNullable} = require('../parsers-commons.js');
const {
wrapNullable,
unwrapNullable,
emitMixedTypeAnnotation,
} = require('../parsers-commons.js');
describe('wrapNullable', () => {
describe('when nullable is true', () => {
@ -172,3 +176,29 @@ describe('assertGenericTypeAnnotationHasExactlyOneTypeParameter', () => {
).toThrow(IncorrectlyParameterizedGenericParserError);
});
});
describe('emitMixedTypeAnnotation', () => {
describe('when nullable is true', () => {
it('returns nullable type annotation', () => {
const result = emitMixedTypeAnnotation(true);
const expected = {
type: 'NullableTypeAnnotation',
typeAnnotation: {
type: 'MixedTypeAnnotation',
},
};
expect(result).toEqual(expected);
});
});
describe('when nullable is false', () => {
it('returns non nullable type annotation', () => {
const result = emitMixedTypeAnnotation(false);
const expected = {
type: 'MixedTypeAnnotation',
};
expect(result).toEqual(expected);
});
});
});

Просмотреть файл

@ -37,6 +37,7 @@ const {
unwrapNullable,
wrapNullable,
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
emitMixedTypeAnnotation,
} = require('../../parsers-commons');
const {
emitBoolean,
@ -414,9 +415,7 @@ function translateTypeAnnotation(
}
case 'MixedTypeAnnotation': {
if (cxxOnly) {
return wrapNullable(nullable, {
type: 'MixedTypeAnnotation',
});
return emitMixedTypeAnnotation(nullable);
}
// Fallthrough
}

Просмотреть файл

@ -15,6 +15,7 @@ import type {
NativeModuleSchema,
NativeModuleTypeAnnotation,
Nullable,
NativeModuleMixedTypeAnnotation,
} from '../CodegenSchema.js';
const {IncorrectlyParameterizedGenericParserError} = require('./errors');
import type {ParserType} from './errors';
@ -91,9 +92,18 @@ function assertGenericTypeAnnotationHasExactlyOneTypeParameter(
}
}
function emitMixedTypeAnnotation(
nullable: boolean,
): Nullable<NativeModuleMixedTypeAnnotation> {
return wrapNullable(nullable, {
type: 'MixedTypeAnnotation',
});
}
module.exports = {
wrapModuleSchema,
unwrapNullable,
wrapNullable,
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
emitMixedTypeAnnotation,
};

Просмотреть файл

@ -37,6 +37,7 @@ const {
unwrapNullable,
wrapNullable,
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
emitMixedTypeAnnotation,
} = require('../../parsers-commons');
const {
emitBoolean,
@ -430,9 +431,7 @@ function translateTypeAnnotation(
}
case 'TSUnknownKeyword': {
if (cxxOnly) {
return wrapNullable(nullable, {
type: 'MixedTypeAnnotation',
});
return emitMixedTypeAnnotation(nullable);
}
// Fallthrough
}