mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Move emitUnionTypeAnnotation into parsers-primitives (#35502)
Summary: This PR aims to move the emitUnionTypeAnnotation function into parsers-primitives. It was extracted to parsers-commons in a task of https://github.com/facebook/react-native/issues/34872. But, all other emit* functions are in this file, so I think it was a mistake. ## 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] - Move emitUnionTypeAnnotation function into parsers-primitives Pull Request resolved: https://github.com/facebook/react-native/pull/35502 Test Plan: yarn flow: <img width="506" alt="image" src="https://user-images.githubusercontent.com/40902940/204494702-4a190d29-38bb-4da3-beb1-236f685ce671.png"> yarn lint: <img width="261" alt="image" src="https://user-images.githubusercontent.com/40902940/204494798-3dfb0a8a-8535-455c-9483-ec17a7d8a710.png"> yarn test: <img width="381" alt="image" src="https://user-images.githubusercontent.com/40902940/204494766-27fdaccc-f90d-4b77-b53b-2ff0d720c74e.png"> Reviewed By: rshest Differential Revision: D41577409 Pulled By: cortinico fbshipit-source-id: c37b85e8a53c2b1c2bb0e00bc772dd615d1269dd
This commit is contained in:
committed by
Facebook GitHub Bot
parent
daefeb1c0b
commit
ee8701e13e
@@ -17,22 +17,17 @@ import {
|
||||
parseObjectProperty,
|
||||
wrapNullable,
|
||||
unwrapNullable,
|
||||
emitUnionTypeAnnotation,
|
||||
} from '../parsers-commons';
|
||||
import type {ParserType} from '../errors';
|
||||
import type {UnionTypeAnnotationMemberType} from '../../CodegenSchema';
|
||||
|
||||
const {
|
||||
UnsupportedUnionTypeAnnotationParserError,
|
||||
UnsupportedObjectPropertyTypeAnnotationParserError,
|
||||
} = require('../errors');
|
||||
|
||||
import {MockedParser} from '../parserMock';
|
||||
import {TypeScriptParser} from '../typescript/parser';
|
||||
import {FlowParser} from '../flow/parser';
|
||||
|
||||
const parser = new MockedParser();
|
||||
const flowParser = new FlowParser();
|
||||
const typeScriptParser = new TypeScriptParser();
|
||||
|
||||
const flowTranslateTypeAnnotation = require('../flow/modules/index');
|
||||
@@ -396,430 +391,3 @@ describe('parseObjectProperty', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('emitUnionTypeAnnotation', () => {
|
||||
const hasteModuleName = 'SampleTurboModule';
|
||||
|
||||
describe('when language is flow', () => {
|
||||
describe('when members type is numeric', () => {
|
||||
const typeAnnotation = {
|
||||
type: 'UnionTypeAnnotation',
|
||||
types: [
|
||||
{type: 'NumberLiteralTypeAnnotation'},
|
||||
{type: 'NumberLiteralTypeAnnotation'},
|
||||
],
|
||||
};
|
||||
describe('when nullable is true', () => {
|
||||
it('returns nullable type annotation', () => {
|
||||
const result = emitUnionTypeAnnotation(
|
||||
true,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
flowParser,
|
||||
);
|
||||
|
||||
const expected = {
|
||||
type: 'NullableTypeAnnotation',
|
||||
typeAnnotation: {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: 'NumberTypeAnnotation',
|
||||
},
|
||||
};
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when nullable is false', () => {
|
||||
it('returns non nullable type annotation', () => {
|
||||
const result = emitUnionTypeAnnotation(
|
||||
false,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
flowParser,
|
||||
);
|
||||
|
||||
const expected = {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: 'NumberTypeAnnotation',
|
||||
};
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when members type is string', () => {
|
||||
const typeAnnotation = {
|
||||
type: 'UnionTypeAnnotation',
|
||||
types: [
|
||||
{type: 'StringLiteralTypeAnnotation'},
|
||||
{type: 'StringLiteralTypeAnnotation'},
|
||||
],
|
||||
};
|
||||
describe('when nullable is true', () => {
|
||||
it('returns nullable type annotation', () => {
|
||||
const result = emitUnionTypeAnnotation(
|
||||
true,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
flowParser,
|
||||
);
|
||||
|
||||
const expected = {
|
||||
type: 'NullableTypeAnnotation',
|
||||
typeAnnotation: {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: 'StringTypeAnnotation',
|
||||
},
|
||||
};
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when nullable is false', () => {
|
||||
it('returns non nullable type annotation', () => {
|
||||
const result = emitUnionTypeAnnotation(
|
||||
false,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
flowParser,
|
||||
);
|
||||
|
||||
const expected = {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: 'StringTypeAnnotation',
|
||||
};
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when members type is object', () => {
|
||||
const typeAnnotation = {
|
||||
type: 'UnionTypeAnnotation',
|
||||
types: [{type: 'ObjectTypeAnnotation'}, {type: 'ObjectTypeAnnotation'}],
|
||||
};
|
||||
describe('when nullable is true', () => {
|
||||
it('returns nullable type annotation', () => {
|
||||
const result = emitUnionTypeAnnotation(
|
||||
true,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
flowParser,
|
||||
);
|
||||
|
||||
const expected = {
|
||||
type: 'NullableTypeAnnotation',
|
||||
typeAnnotation: {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: 'ObjectTypeAnnotation',
|
||||
},
|
||||
};
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when nullable is false', () => {
|
||||
it('returns non nullable type annotation', () => {
|
||||
const result = emitUnionTypeAnnotation(
|
||||
false,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
flowParser,
|
||||
);
|
||||
|
||||
const expected = {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: 'ObjectTypeAnnotation',
|
||||
};
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when members type is mixed', () => {
|
||||
const typeAnnotation = {
|
||||
type: 'UnionTypeAnnotation',
|
||||
types: [
|
||||
{type: 'NumberLiteralTypeAnnotation'},
|
||||
{type: 'StringLiteralTypeAnnotation'},
|
||||
{type: 'ObjectTypeAnnotation'},
|
||||
],
|
||||
};
|
||||
const unionTypes: UnionTypeAnnotationMemberType[] = [
|
||||
'NumberTypeAnnotation',
|
||||
'StringTypeAnnotation',
|
||||
'ObjectTypeAnnotation',
|
||||
];
|
||||
describe('when nullable is true', () => {
|
||||
it('throws an excpetion', () => {
|
||||
const expected = new UnsupportedUnionTypeAnnotationParserError(
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
unionTypes,
|
||||
flowParser.language(),
|
||||
);
|
||||
|
||||
expect(() => {
|
||||
emitUnionTypeAnnotation(
|
||||
true,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
flowParser,
|
||||
);
|
||||
}).toThrow(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when nullable is false', () => {
|
||||
it('throws an excpetion', () => {
|
||||
const expected = new UnsupportedUnionTypeAnnotationParserError(
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
unionTypes,
|
||||
flowParser.language(),
|
||||
);
|
||||
|
||||
expect(() => {
|
||||
emitUnionTypeAnnotation(
|
||||
false,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
flowParser,
|
||||
);
|
||||
}).toThrow(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when language is typescript', () => {
|
||||
describe('when members type is numeric', () => {
|
||||
const typeAnnotation = {
|
||||
type: 'TSUnionType',
|
||||
types: [
|
||||
{
|
||||
type: 'TSLiteralType',
|
||||
literal: {type: 'NumericLiteral'},
|
||||
},
|
||||
{
|
||||
type: 'TSLiteralType',
|
||||
literal: {type: 'NumericLiteral'},
|
||||
},
|
||||
],
|
||||
};
|
||||
describe('when nullable is true', () => {
|
||||
it('returns nullable type annotation', () => {
|
||||
const result = emitUnionTypeAnnotation(
|
||||
true,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
typeScriptParser,
|
||||
);
|
||||
|
||||
const expected = {
|
||||
type: 'NullableTypeAnnotation',
|
||||
typeAnnotation: {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: 'NumberTypeAnnotation',
|
||||
},
|
||||
};
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when nullable is false', () => {
|
||||
it('returns non nullable type annotation', () => {
|
||||
const result = emitUnionTypeAnnotation(
|
||||
false,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
typeScriptParser,
|
||||
);
|
||||
|
||||
const expected = {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: 'NumberTypeAnnotation',
|
||||
};
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when members type is string', () => {
|
||||
const typeAnnotation = {
|
||||
type: 'TSUnionType',
|
||||
types: [
|
||||
{
|
||||
type: 'TSLiteralType',
|
||||
literal: {type: 'StringLiteral'},
|
||||
},
|
||||
{
|
||||
type: 'TSLiteralType',
|
||||
literal: {type: 'StringLiteral'},
|
||||
},
|
||||
],
|
||||
};
|
||||
describe('when nullable is true', () => {
|
||||
it('returns nullable type annotation', () => {
|
||||
const result = emitUnionTypeAnnotation(
|
||||
true,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
typeScriptParser,
|
||||
);
|
||||
|
||||
const expected = {
|
||||
type: 'NullableTypeAnnotation',
|
||||
typeAnnotation: {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: 'StringTypeAnnotation',
|
||||
},
|
||||
};
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when nullable is false', () => {
|
||||
it('returns non nullable type annotation', () => {
|
||||
const result = emitUnionTypeAnnotation(
|
||||
false,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
typeScriptParser,
|
||||
);
|
||||
|
||||
const expected = {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: 'StringTypeAnnotation',
|
||||
};
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when members type is object', () => {
|
||||
const typeAnnotation = {
|
||||
type: 'TSUnionType',
|
||||
types: [
|
||||
{
|
||||
type: 'TSLiteralType',
|
||||
},
|
||||
{
|
||||
type: 'TSLiteralType',
|
||||
},
|
||||
],
|
||||
};
|
||||
describe('when nullable is true', () => {
|
||||
it('returns nullable type annotation', () => {
|
||||
const result = emitUnionTypeAnnotation(
|
||||
true,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
typeScriptParser,
|
||||
);
|
||||
|
||||
const expected = {
|
||||
type: 'NullableTypeAnnotation',
|
||||
typeAnnotation: {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: 'ObjectTypeAnnotation',
|
||||
},
|
||||
};
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when nullable is false', () => {
|
||||
it('returns non nullable type annotation', () => {
|
||||
const result = emitUnionTypeAnnotation(
|
||||
false,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
typeScriptParser,
|
||||
);
|
||||
|
||||
const expected = {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: 'ObjectTypeAnnotation',
|
||||
};
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when members type is mixed', () => {
|
||||
const typeAnnotation = {
|
||||
type: 'TSUnionType',
|
||||
types: [
|
||||
{
|
||||
type: 'TSLiteralType',
|
||||
literal: {type: 'NumericLiteral'},
|
||||
},
|
||||
{
|
||||
type: 'TSLiteralType',
|
||||
literal: {type: 'StringLiteral'},
|
||||
},
|
||||
{
|
||||
type: 'TSLiteralType',
|
||||
},
|
||||
],
|
||||
};
|
||||
const unionTypes = [
|
||||
'NumberTypeAnnotation',
|
||||
'StringTypeAnnotation',
|
||||
'ObjectTypeAnnotation',
|
||||
];
|
||||
describe('when nullable is true', () => {
|
||||
it('throws an excpetion', () => {
|
||||
const expected = new UnsupportedUnionTypeAnnotationParserError(
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
unionTypes,
|
||||
typeScriptParser.language(),
|
||||
);
|
||||
|
||||
expect(() => {
|
||||
emitUnionTypeAnnotation(
|
||||
true,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
typeScriptParser,
|
||||
);
|
||||
}).toThrow(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when nullable is false', () => {
|
||||
it('throws an excpetion', () => {
|
||||
const expected = new UnsupportedUnionTypeAnnotationParserError(
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
unionTypes,
|
||||
typeScriptParser.language(),
|
||||
);
|
||||
|
||||
expect(() => {
|
||||
emitUnionTypeAnnotation(
|
||||
false,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
typeScriptParser,
|
||||
);
|
||||
}).toThrow(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+420
-5
@@ -11,6 +11,8 @@
|
||||
|
||||
'use-strict';
|
||||
|
||||
import type {UnionTypeAnnotationMemberType} from '../../CodegenSchema';
|
||||
|
||||
const {
|
||||
emitBoolean,
|
||||
emitDouble,
|
||||
@@ -23,12 +25,18 @@ const {
|
||||
emitVoid,
|
||||
emitString,
|
||||
emitStringish,
|
||||
emitMixedTypeAnnotation,
|
||||
emitMixed,
|
||||
typeAliasResolution,
|
||||
} = require('../parsers-primitives.js');
|
||||
import {MockedParser} from '../parserMock';
|
||||
const {MockedParser} = require('../parserMock');
|
||||
const {emitUnion} = require('../parsers-primitives');
|
||||
const {UnsupportedUnionTypeAnnotationParserError} = require('../errors');
|
||||
const {FlowParser} = require('../flow/parser');
|
||||
const {TypeScriptParser} = require('../typescript/parser');
|
||||
|
||||
const parser = new MockedParser();
|
||||
const flowParser = new FlowParser();
|
||||
const typeScriptParser = new TypeScriptParser();
|
||||
|
||||
describe('emitBoolean', () => {
|
||||
describe('when nullable is true', () => {
|
||||
@@ -472,10 +480,10 @@ describe('emitObject', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('emitMixedTypeAnnotation', () => {
|
||||
describe('emitMixed', () => {
|
||||
describe('when nullable is true', () => {
|
||||
it('returns nullable type annotation', () => {
|
||||
const result = emitMixedTypeAnnotation(true);
|
||||
const result = emitMixed(true);
|
||||
const expected = {
|
||||
type: 'NullableTypeAnnotation',
|
||||
typeAnnotation: {
|
||||
@@ -488,7 +496,7 @@ describe('emitMixedTypeAnnotation', () => {
|
||||
});
|
||||
describe('when nullable is false', () => {
|
||||
it('returns non nullable type annotation', () => {
|
||||
const result = emitMixedTypeAnnotation(false);
|
||||
const result = emitMixed(false);
|
||||
const expected = {
|
||||
type: 'MixedTypeAnnotation',
|
||||
};
|
||||
@@ -497,3 +505,410 @@ describe('emitMixedTypeAnnotation', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('emitUnion', () => {
|
||||
const hasteModuleName = 'SampleTurboModule';
|
||||
|
||||
describe('when language is flow', () => {
|
||||
describe('when members type is numeric', () => {
|
||||
const typeAnnotation = {
|
||||
type: 'UnionTypeAnnotation',
|
||||
types: [
|
||||
{type: 'NumberLiteralTypeAnnotation'},
|
||||
{type: 'NumberLiteralTypeAnnotation'},
|
||||
],
|
||||
};
|
||||
describe('when nullable is true', () => {
|
||||
it('returns nullable type annotation', () => {
|
||||
const result = emitUnion(
|
||||
true,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
flowParser,
|
||||
);
|
||||
|
||||
const expected = {
|
||||
type: 'NullableTypeAnnotation',
|
||||
typeAnnotation: {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: 'NumberTypeAnnotation',
|
||||
},
|
||||
};
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when nullable is false', () => {
|
||||
it('returns non nullable type annotation', () => {
|
||||
const result = emitUnion(
|
||||
false,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
flowParser,
|
||||
);
|
||||
|
||||
const expected = {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: 'NumberTypeAnnotation',
|
||||
};
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when members type is string', () => {
|
||||
const typeAnnotation = {
|
||||
type: 'UnionTypeAnnotation',
|
||||
types: [
|
||||
{type: 'StringLiteralTypeAnnotation'},
|
||||
{type: 'StringLiteralTypeAnnotation'},
|
||||
],
|
||||
};
|
||||
describe('when nullable is true', () => {
|
||||
it('returns nullable type annotation', () => {
|
||||
const result = emitUnion(
|
||||
true,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
flowParser,
|
||||
);
|
||||
|
||||
const expected = {
|
||||
type: 'NullableTypeAnnotation',
|
||||
typeAnnotation: {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: 'StringTypeAnnotation',
|
||||
},
|
||||
};
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when nullable is false', () => {
|
||||
it('returns non nullable type annotation', () => {
|
||||
const result = emitUnion(
|
||||
false,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
flowParser,
|
||||
);
|
||||
|
||||
const expected = {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: 'StringTypeAnnotation',
|
||||
};
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when members type is object', () => {
|
||||
const typeAnnotation = {
|
||||
type: 'UnionTypeAnnotation',
|
||||
types: [{type: 'ObjectTypeAnnotation'}, {type: 'ObjectTypeAnnotation'}],
|
||||
};
|
||||
describe('when nullable is true', () => {
|
||||
it('returns nullable type annotation', () => {
|
||||
const result = emitUnion(
|
||||
true,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
flowParser,
|
||||
);
|
||||
|
||||
const expected = {
|
||||
type: 'NullableTypeAnnotation',
|
||||
typeAnnotation: {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: 'ObjectTypeAnnotation',
|
||||
},
|
||||
};
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when nullable is false', () => {
|
||||
it('returns non nullable type annotation', () => {
|
||||
const result = emitUnion(
|
||||
false,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
flowParser,
|
||||
);
|
||||
|
||||
const expected = {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: 'ObjectTypeAnnotation',
|
||||
};
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when members type is mixed', () => {
|
||||
const typeAnnotation = {
|
||||
type: 'UnionTypeAnnotation',
|
||||
types: [
|
||||
{type: 'NumberLiteralTypeAnnotation'},
|
||||
{type: 'StringLiteralTypeAnnotation'},
|
||||
{type: 'ObjectTypeAnnotation'},
|
||||
],
|
||||
};
|
||||
const unionTypes: UnionTypeAnnotationMemberType[] = [
|
||||
'NumberTypeAnnotation',
|
||||
'StringTypeAnnotation',
|
||||
'ObjectTypeAnnotation',
|
||||
];
|
||||
describe('when nullable is true', () => {
|
||||
it('throws an excpetion', () => {
|
||||
const expected = new UnsupportedUnionTypeAnnotationParserError(
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
unionTypes,
|
||||
flowParser.language(),
|
||||
);
|
||||
|
||||
expect(() => {
|
||||
emitUnion(true, hasteModuleName, typeAnnotation, flowParser);
|
||||
}).toThrow(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when nullable is false', () => {
|
||||
it('throws an excpetion', () => {
|
||||
const expected = new UnsupportedUnionTypeAnnotationParserError(
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
unionTypes,
|
||||
flowParser.language(),
|
||||
);
|
||||
|
||||
expect(() => {
|
||||
emitUnion(false, hasteModuleName, typeAnnotation, flowParser);
|
||||
}).toThrow(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when language is typescript', () => {
|
||||
describe('when members type is numeric', () => {
|
||||
const typeAnnotation = {
|
||||
type: 'TSUnionType',
|
||||
types: [
|
||||
{
|
||||
type: 'TSLiteralType',
|
||||
literal: {type: 'NumericLiteral'},
|
||||
},
|
||||
{
|
||||
type: 'TSLiteralType',
|
||||
literal: {type: 'NumericLiteral'},
|
||||
},
|
||||
],
|
||||
};
|
||||
describe('when nullable is true', () => {
|
||||
it('returns nullable type annotation', () => {
|
||||
const result = emitUnion(
|
||||
true,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
typeScriptParser,
|
||||
);
|
||||
|
||||
const expected = {
|
||||
type: 'NullableTypeAnnotation',
|
||||
typeAnnotation: {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: 'NumberTypeAnnotation',
|
||||
},
|
||||
};
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when nullable is false', () => {
|
||||
it('returns non nullable type annotation', () => {
|
||||
const result = emitUnion(
|
||||
false,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
typeScriptParser,
|
||||
);
|
||||
|
||||
const expected = {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: 'NumberTypeAnnotation',
|
||||
};
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when members type is string', () => {
|
||||
const typeAnnotation = {
|
||||
type: 'TSUnionType',
|
||||
types: [
|
||||
{
|
||||
type: 'TSLiteralType',
|
||||
literal: {type: 'StringLiteral'},
|
||||
},
|
||||
{
|
||||
type: 'TSLiteralType',
|
||||
literal: {type: 'StringLiteral'},
|
||||
},
|
||||
],
|
||||
};
|
||||
describe('when nullable is true', () => {
|
||||
it('returns nullable type annotation', () => {
|
||||
const result = emitUnion(
|
||||
true,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
typeScriptParser,
|
||||
);
|
||||
|
||||
const expected = {
|
||||
type: 'NullableTypeAnnotation',
|
||||
typeAnnotation: {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: 'StringTypeAnnotation',
|
||||
},
|
||||
};
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when nullable is false', () => {
|
||||
it('returns non nullable type annotation', () => {
|
||||
const result = emitUnion(
|
||||
false,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
typeScriptParser,
|
||||
);
|
||||
|
||||
const expected = {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: 'StringTypeAnnotation',
|
||||
};
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when members type is object', () => {
|
||||
const typeAnnotation = {
|
||||
type: 'TSUnionType',
|
||||
types: [
|
||||
{
|
||||
type: 'TSLiteralType',
|
||||
},
|
||||
{
|
||||
type: 'TSLiteralType',
|
||||
},
|
||||
],
|
||||
};
|
||||
describe('when nullable is true', () => {
|
||||
it('returns nullable type annotation', () => {
|
||||
const result = emitUnion(
|
||||
true,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
typeScriptParser,
|
||||
);
|
||||
|
||||
const expected = {
|
||||
type: 'NullableTypeAnnotation',
|
||||
typeAnnotation: {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: 'ObjectTypeAnnotation',
|
||||
},
|
||||
};
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when nullable is false', () => {
|
||||
it('returns non nullable type annotation', () => {
|
||||
const result = emitUnion(
|
||||
false,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
typeScriptParser,
|
||||
);
|
||||
|
||||
const expected = {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: 'ObjectTypeAnnotation',
|
||||
};
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when members type is mixed', () => {
|
||||
const typeAnnotation = {
|
||||
type: 'TSUnionType',
|
||||
types: [
|
||||
{
|
||||
type: 'TSLiteralType',
|
||||
literal: {type: 'NumericLiteral'},
|
||||
},
|
||||
{
|
||||
type: 'TSLiteralType',
|
||||
literal: {type: 'StringLiteral'},
|
||||
},
|
||||
{
|
||||
type: 'TSLiteralType',
|
||||
},
|
||||
],
|
||||
};
|
||||
const unionTypes = [
|
||||
'NumberTypeAnnotation',
|
||||
'StringTypeAnnotation',
|
||||
'ObjectTypeAnnotation',
|
||||
];
|
||||
describe('when nullable is true', () => {
|
||||
it('throws an excpetion', () => {
|
||||
const expected = new UnsupportedUnionTypeAnnotationParserError(
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
unionTypes,
|
||||
typeScriptParser.language(),
|
||||
);
|
||||
|
||||
expect(() => {
|
||||
emitUnion(true, hasteModuleName, typeAnnotation, typeScriptParser);
|
||||
}).toThrow(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when nullable is false', () => {
|
||||
it('throws an excpetion', () => {
|
||||
const expected = new UnsupportedUnionTypeAnnotationParserError(
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
unionTypes,
|
||||
typeScriptParser.language(),
|
||||
);
|
||||
|
||||
expect(() => {
|
||||
emitUnion(false, hasteModuleName, typeAnnotation, typeScriptParser);
|
||||
}).toThrow(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -28,7 +28,6 @@ const {
|
||||
wrapNullable,
|
||||
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
|
||||
parseObjectProperty,
|
||||
emitUnionTypeAnnotation,
|
||||
translateDefault,
|
||||
buildPropertySchema,
|
||||
} = require('../../parsers-commons');
|
||||
@@ -45,7 +44,8 @@ const {
|
||||
emitVoid,
|
||||
emitString,
|
||||
emitStringish,
|
||||
emitMixedTypeAnnotation,
|
||||
emitMixed,
|
||||
emitUnion,
|
||||
typeAliasResolution,
|
||||
translateArrayTypeAnnotation,
|
||||
} = require('../../parsers-primitives');
|
||||
@@ -232,12 +232,7 @@ function translateTypeAnnotation(
|
||||
);
|
||||
}
|
||||
case 'UnionTypeAnnotation': {
|
||||
return emitUnionTypeAnnotation(
|
||||
nullable,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
parser,
|
||||
);
|
||||
return emitUnion(nullable, hasteModuleName, typeAnnotation, parser);
|
||||
}
|
||||
case 'StringLiteralTypeAnnotation': {
|
||||
// 'a' is a special case for 'a' | 'b' but the type name is different
|
||||
@@ -248,7 +243,7 @@ function translateTypeAnnotation(
|
||||
}
|
||||
case 'MixedTypeAnnotation': {
|
||||
if (cxxOnly) {
|
||||
return emitMixedTypeAnnotation(nullable);
|
||||
return emitMixed(nullable);
|
||||
}
|
||||
// Fallthrough
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ import type {
|
||||
NativeModuleFunctionTypeAnnotation,
|
||||
NativeModuleParamTypeAnnotation,
|
||||
NativeModulePropertyShape,
|
||||
NativeModuleUnionTypeAnnotation,
|
||||
SchemaType,
|
||||
} from '../CodegenSchema.js';
|
||||
|
||||
@@ -42,7 +41,6 @@ const {
|
||||
UnsupportedEnumDeclarationParserError,
|
||||
UnsupportedGenericParserError,
|
||||
UnsupportedObjectPropertyTypeAnnotationParserError,
|
||||
UnsupportedUnionTypeAnnotationParserError,
|
||||
UnnamedFunctionParamParserError,
|
||||
} = require('./errors');
|
||||
|
||||
@@ -207,32 +205,6 @@ function parseObjectProperty(
|
||||
};
|
||||
}
|
||||
|
||||
function emitUnionTypeAnnotation(
|
||||
nullable: boolean,
|
||||
hasteModuleName: string,
|
||||
typeAnnotation: $FlowFixMe,
|
||||
parser: Parser,
|
||||
): Nullable<NativeModuleUnionTypeAnnotation> {
|
||||
const unionTypes = parser.remapUnionTypeAnnotationMemberNames(
|
||||
typeAnnotation.types,
|
||||
);
|
||||
|
||||
// Only support unionTypes of the same kind
|
||||
if (unionTypes.length > 1) {
|
||||
throw new UnsupportedUnionTypeAnnotationParserError(
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
unionTypes,
|
||||
parser.language(),
|
||||
);
|
||||
}
|
||||
|
||||
return wrapNullable(nullable, {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: unionTypes[0],
|
||||
});
|
||||
}
|
||||
|
||||
function translateDefault(
|
||||
hasteModuleName: string,
|
||||
typeAnnotation: $FlowFixMe,
|
||||
@@ -469,7 +441,6 @@ module.exports = {
|
||||
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
|
||||
isObjectProperty,
|
||||
parseObjectProperty,
|
||||
emitUnionTypeAnnotation,
|
||||
translateDefault,
|
||||
translateFunctionTypeAnnotation,
|
||||
buildPropertySchema,
|
||||
|
||||
@@ -25,6 +25,7 @@ import type {
|
||||
NativeModuleNumberTypeAnnotation,
|
||||
NativeModulePromiseTypeAnnotation,
|
||||
NativeModuleTypeAliasTypeAnnotation,
|
||||
NativeModuleUnionTypeAnnotation,
|
||||
ObjectTypeAnnotation,
|
||||
ReservedTypeAnnotation,
|
||||
StringTypeAnnotation,
|
||||
@@ -38,6 +39,7 @@ import type {
|
||||
TypeDeclarationMap,
|
||||
} from './utils';
|
||||
|
||||
const {UnsupportedUnionTypeAnnotationParserError} = require('./errors');
|
||||
const {
|
||||
throwIfArrayElementTypeAnnotationIsUnsupported,
|
||||
} = require('./error-utils');
|
||||
@@ -119,7 +121,7 @@ function emitFunction(
|
||||
return wrapNullable(nullable, translateFunctionTypeAnnotationValue);
|
||||
}
|
||||
|
||||
function emitMixedTypeAnnotation(
|
||||
function emitMixed(
|
||||
nullable: boolean,
|
||||
): Nullable<NativeModuleMixedTypeAnnotation> {
|
||||
return wrapNullable(nullable, {
|
||||
@@ -249,6 +251,32 @@ function emitFloat(
|
||||
});
|
||||
}
|
||||
|
||||
function emitUnion(
|
||||
nullable: boolean,
|
||||
hasteModuleName: string,
|
||||
typeAnnotation: $FlowFixMe,
|
||||
parser: Parser,
|
||||
): Nullable<NativeModuleUnionTypeAnnotation> {
|
||||
const unionTypes = parser.remapUnionTypeAnnotationMemberNames(
|
||||
typeAnnotation.types,
|
||||
);
|
||||
|
||||
// Only support unionTypes of the same kind
|
||||
if (unionTypes.length > 1) {
|
||||
throw new UnsupportedUnionTypeAnnotationParserError(
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
unionTypes,
|
||||
parser.language(),
|
||||
);
|
||||
}
|
||||
|
||||
return wrapNullable(nullable, {
|
||||
type: 'UnionTypeAnnotation',
|
||||
memberType: unionTypes[0],
|
||||
});
|
||||
}
|
||||
|
||||
function translateArrayTypeAnnotation(
|
||||
hasteModuleName: string,
|
||||
types: TypeDeclarationMap,
|
||||
@@ -318,7 +346,8 @@ module.exports = {
|
||||
emitVoid,
|
||||
emitString,
|
||||
emitStringish,
|
||||
emitMixedTypeAnnotation,
|
||||
emitMixed,
|
||||
emitUnion,
|
||||
typeAliasResolution,
|
||||
translateArrayTypeAnnotation,
|
||||
};
|
||||
|
||||
@@ -28,7 +28,6 @@ const {resolveTypeAnnotation, getTypes} = require('../utils.js');
|
||||
const {
|
||||
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
|
||||
parseObjectProperty,
|
||||
emitUnionTypeAnnotation,
|
||||
translateDefault,
|
||||
buildPropertySchema,
|
||||
} = require('../../parsers-commons');
|
||||
@@ -46,7 +45,8 @@ const {
|
||||
emitVoid,
|
||||
emitString,
|
||||
emitStringish,
|
||||
emitMixedTypeAnnotation,
|
||||
emitMixed,
|
||||
emitUnion,
|
||||
typeAliasResolution,
|
||||
translateArrayTypeAnnotation,
|
||||
} = require('../../parsers-primitives');
|
||||
@@ -248,16 +248,11 @@ function translateTypeAnnotation(
|
||||
);
|
||||
}
|
||||
case 'TSUnionType': {
|
||||
return emitUnionTypeAnnotation(
|
||||
nullable,
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
parser,
|
||||
);
|
||||
return emitUnion(nullable, hasteModuleName, typeAnnotation, parser);
|
||||
}
|
||||
case 'TSUnknownKeyword': {
|
||||
if (cxxOnly) {
|
||||
return emitMixedTypeAnnotation(nullable);
|
||||
return emitMixed(nullable);
|
||||
}
|
||||
// Fallthrough
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user