move remapUnionTypeAnnotationMemberNames to Parser (#35314)

Summary:
Part of https://github.com/facebook/react-native/issues/34872#issuecomment-1304236257

> [Assigned to youedd] Create a new function [remapUnionTypeAnnotationMemberNames](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/parsers-commons.js#L110) in the [parser.js file](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/parser.js) and add documentation to it. Implement it properly in the [FlowParser.js](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/flow/parser.js#L15) and in the [TypeScriptParser.js](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/typescript/parser.js#L15). Remove the function [remapUnionTypeAnnotationMemberNames](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/parsers-commons.js#L110) and update the [emitUnionTypeAnnotation](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/parsers-commons.js#L110) signature to accept a Parser parameter instead of a language one. Use the new Parser function instead of the old one [here](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/parsers-commons.js#L139).

## 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 remapUnionTypeAnnotationMemberNames to the parsers implementations

Pull Request resolved: https://github.com/facebook/react-native/pull/35314

Test Plan:
`yarn jest react-native-codegen`

![image](https://user-images.githubusercontent.com/19575877/201389910-31d48601-7023-4c94-a6d5-efccb18629cd.png)

Reviewed By: christophpurrer

Differential Revision: D41247716

Pulled By: cipolleschi

fbshipit-source-id: 6f708895392d5bdac5d4edbc67587194321ddb3d
This commit is contained in:
youedd 2022-11-14 12:28:35 -08:00 коммит произвёл Facebook GitHub Bot
Родитель 1bb67e7818
Коммит a7ae9885ed
9 изменённых файлов: 106 добавлений и 57 удалений

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

@ -29,8 +29,10 @@ const {
import {MockedParser} from '../parserMock'; import {MockedParser} from '../parserMock';
import {TypeScriptParser} from '../typescript/parser'; import {TypeScriptParser} from '../typescript/parser';
import {FlowParser} from '../flow/parser';
const parser = new MockedParser(); const parser = new MockedParser();
const flowParser = new FlowParser();
const typeScriptParser = new TypeScriptParser(); const typeScriptParser = new TypeScriptParser();
const flowTranslateTypeAnnotation = require('../flow/modules/index'); const flowTranslateTypeAnnotation = require('../flow/modules/index');
@ -399,8 +401,6 @@ describe('emitUnionTypeAnnotation', () => {
const hasteModuleName = 'SampleTurboModule'; const hasteModuleName = 'SampleTurboModule';
describe('when language is flow', () => { describe('when language is flow', () => {
const language: ParserType = 'Flow';
describe('when members type is numeric', () => { describe('when members type is numeric', () => {
const typeAnnotation = { const typeAnnotation = {
type: 'UnionTypeAnnotation', type: 'UnionTypeAnnotation',
@ -415,7 +415,7 @@ describe('emitUnionTypeAnnotation', () => {
true, true,
hasteModuleName, hasteModuleName,
typeAnnotation, typeAnnotation,
language, flowParser,
); );
const expected = { const expected = {
@ -436,7 +436,7 @@ describe('emitUnionTypeAnnotation', () => {
false, false,
hasteModuleName, hasteModuleName,
typeAnnotation, typeAnnotation,
language, flowParser,
); );
const expected = { const expected = {
@ -463,7 +463,7 @@ describe('emitUnionTypeAnnotation', () => {
true, true,
hasteModuleName, hasteModuleName,
typeAnnotation, typeAnnotation,
language, flowParser,
); );
const expected = { const expected = {
@ -484,7 +484,7 @@ describe('emitUnionTypeAnnotation', () => {
false, false,
hasteModuleName, hasteModuleName,
typeAnnotation, typeAnnotation,
language, flowParser,
); );
const expected = { const expected = {
@ -508,7 +508,7 @@ describe('emitUnionTypeAnnotation', () => {
true, true,
hasteModuleName, hasteModuleName,
typeAnnotation, typeAnnotation,
language, flowParser,
); );
const expected = { const expected = {
@ -529,7 +529,7 @@ describe('emitUnionTypeAnnotation', () => {
false, false,
hasteModuleName, hasteModuleName,
typeAnnotation, typeAnnotation,
language, flowParser,
); );
const expected = { const expected = {
@ -562,7 +562,7 @@ describe('emitUnionTypeAnnotation', () => {
hasteModuleName, hasteModuleName,
typeAnnotation, typeAnnotation,
unionTypes, unionTypes,
language, flowParser.language(),
); );
expect(() => { expect(() => {
@ -570,7 +570,7 @@ describe('emitUnionTypeAnnotation', () => {
true, true,
hasteModuleName, hasteModuleName,
typeAnnotation, typeAnnotation,
language, flowParser,
); );
}).toThrow(expected); }).toThrow(expected);
}); });
@ -582,7 +582,7 @@ describe('emitUnionTypeAnnotation', () => {
hasteModuleName, hasteModuleName,
typeAnnotation, typeAnnotation,
unionTypes, unionTypes,
language, flowParser.language(),
); );
expect(() => { expect(() => {
@ -590,7 +590,7 @@ describe('emitUnionTypeAnnotation', () => {
false, false,
hasteModuleName, hasteModuleName,
typeAnnotation, typeAnnotation,
language, flowParser,
); );
}).toThrow(expected); }).toThrow(expected);
}); });
@ -599,8 +599,6 @@ describe('emitUnionTypeAnnotation', () => {
}); });
describe('when language is typescript', () => { describe('when language is typescript', () => {
const language: ParserType = 'TypeScript';
describe('when members type is numeric', () => { describe('when members type is numeric', () => {
const typeAnnotation = { const typeAnnotation = {
type: 'TSUnionType', type: 'TSUnionType',
@ -621,7 +619,7 @@ describe('emitUnionTypeAnnotation', () => {
true, true,
hasteModuleName, hasteModuleName,
typeAnnotation, typeAnnotation,
language, typeScriptParser,
); );
const expected = { const expected = {
@ -642,7 +640,7 @@ describe('emitUnionTypeAnnotation', () => {
false, false,
hasteModuleName, hasteModuleName,
typeAnnotation, typeAnnotation,
language, typeScriptParser,
); );
const expected = { const expected = {
@ -675,7 +673,7 @@ describe('emitUnionTypeAnnotation', () => {
true, true,
hasteModuleName, hasteModuleName,
typeAnnotation, typeAnnotation,
language, typeScriptParser,
); );
const expected = { const expected = {
@ -696,7 +694,7 @@ describe('emitUnionTypeAnnotation', () => {
false, false,
hasteModuleName, hasteModuleName,
typeAnnotation, typeAnnotation,
language, typeScriptParser,
); );
const expected = { const expected = {
@ -727,7 +725,7 @@ describe('emitUnionTypeAnnotation', () => {
true, true,
hasteModuleName, hasteModuleName,
typeAnnotation, typeAnnotation,
language, typeScriptParser,
); );
const expected = { const expected = {
@ -748,7 +746,7 @@ describe('emitUnionTypeAnnotation', () => {
false, false,
hasteModuleName, hasteModuleName,
typeAnnotation, typeAnnotation,
language, typeScriptParser,
); );
const expected = { const expected = {
@ -789,7 +787,7 @@ describe('emitUnionTypeAnnotation', () => {
hasteModuleName, hasteModuleName,
typeAnnotation, typeAnnotation,
unionTypes, unionTypes,
language, typeScriptParser.language(),
); );
expect(() => { expect(() => {
@ -797,7 +795,7 @@ describe('emitUnionTypeAnnotation', () => {
true, true,
hasteModuleName, hasteModuleName,
typeAnnotation, typeAnnotation,
language, typeScriptParser,
); );
}).toThrow(expected); }).toThrow(expected);
}); });
@ -809,7 +807,7 @@ describe('emitUnionTypeAnnotation', () => {
hasteModuleName, hasteModuleName,
typeAnnotation, typeAnnotation,
unionTypes, unionTypes,
language, typeScriptParser.language(),
); );
expect(() => { expect(() => {
@ -817,7 +815,7 @@ describe('emitUnionTypeAnnotation', () => {
false, false,
hasteModuleName, hasteModuleName,
typeAnnotation, typeAnnotation,
language, typeScriptParser,
); );
}).toThrow(expected); }).toThrow(expected);
}); });

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

@ -19,7 +19,7 @@ import {TypeScriptParser} from '../typescript/parser';
import {FlowParser} from '../flow/parser'; import {FlowParser} from '../flow/parser';
const hasteModuleName = 'moduleName'; const hasteModuleName = 'moduleName';
describe('TypeScriptParser', () => { describe('FlowParser', () => {
const parser = new FlowParser(); const parser = new FlowParser();
describe('getKeyName', () => { describe('getKeyName', () => {
describe('when propertyOrIndex is ObjectTypeProperty', () => { describe('when propertyOrIndex is ObjectTypeProperty', () => {
@ -76,9 +76,26 @@ describe('TypeScriptParser', () => {
}); });
}); });
}); });
describe('remapUnionTypeAnnotationMemberNames', () => {
it('returns remaped union annotation member types without duplicates', () => {
const membersType = [
{type: 'NumberLiteralTypeAnnotation'},
{type: 'ObjectTypeAnnotation'},
{type: 'StringLiteralTypeAnnotation'},
{type: 'ObjectTypeAnnotation'},
];
expect(parser.remapUnionTypeAnnotationMemberNames(membersType)).toEqual([
'NumberTypeAnnotation',
'ObjectTypeAnnotation',
'StringTypeAnnotation',
]);
});
});
}); });
describe('FlowParser', () => { describe('TypeScriptParser', () => {
const parser = new TypeScriptParser(); const parser = new TypeScriptParser();
describe('getKeyName', () => { describe('getKeyName', () => {
describe('when propertyOrIndex is TSPropertySignature', () => { describe('when propertyOrIndex is TSPropertySignature', () => {
@ -126,4 +143,20 @@ describe('FlowParser', () => {
}); });
}); });
}); });
describe('remapUnionTypeAnnotationMemberNames', () => {
it('returns remaped union annotation member types without duplicates', () => {
const membersType = [
{literal: {type: 'NumericLiteral'}},
{type: 'ObjectTypeAnnotation'},
{literal: {type: 'StringLiteral'}},
{type: 'ObjectTypeAnnotation'},
];
expect(parser.remapUnionTypeAnnotationMemberNames(membersType)).toEqual([
'NumberTypeAnnotation',
'ObjectTypeAnnotation',
'StringTypeAnnotation',
]);
});
});
}); });

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

@ -314,7 +314,7 @@ function translateTypeAnnotation(
nullable, nullable,
hasteModuleName, hasteModuleName,
typeAnnotation, typeAnnotation,
language, parser,
); );
} }
// Fallthrough // Fallthrough

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

@ -10,6 +10,7 @@
'use strict'; 'use strict';
import type {UnionTypeAnnotationMemberType} from '../../CodegenSchema.js';
import type {ParserType} from '../errors'; import type {ParserType} from '../errors';
import type {Parser} from '../parser'; import type {Parser} from '../parser';
@ -63,6 +64,18 @@ class FlowParser implements Parser {
typeArguments.params[0].id.name !== 'Spec' typeArguments.params[0].id.name !== 'Spec'
); );
} }
remapUnionTypeAnnotationMemberNames(
membersTypes: $FlowFixMe[],
): UnionTypeAnnotationMemberType[] {
const remapLiteral = (item: $FlowFixMe) => {
return item.type
.replace('NumberLiteralTypeAnnotation', 'NumberTypeAnnotation')
.replace('StringLiteralTypeAnnotation', 'StringTypeAnnotation');
};
return [...new Set(membersTypes.map(remapLiteral))];
}
} }
module.exports = { module.exports = {

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

@ -10,6 +10,7 @@
'use strict'; 'use strict';
import type {UnionTypeAnnotationMemberType} from '../CodegenSchema.js';
import type {ParserType} from './errors'; import type {ParserType} from './errors';
/** /**
@ -58,4 +59,12 @@ export interface Parser {
* @returns: a boolean specifying if the Module is Invalid. * @returns: a boolean specifying if the Module is Invalid.
*/ */
checkIfInvalidModule(typeArguments: $FlowFixMe): boolean; checkIfInvalidModule(typeArguments: $FlowFixMe): boolean;
/**
* Given a union annotation members types, it returns an array of remaped members names without duplicates.
* @parameter membersTypes: union annotation members types
* @returns: an array of remaped members names without duplicates.
*/
remapUnionTypeAnnotationMemberNames(
types: $FlowFixMe,
): UnionTypeAnnotationMemberType[];
} }

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

@ -10,6 +10,7 @@
'use strict'; 'use strict';
import type {UnionTypeAnnotationMemberType} from '../CodegenSchema.js';
import type {Parser} from './parser'; import type {Parser} from './parser';
import type {ParserType} from './errors'; import type {ParserType} from './errors';
@ -58,4 +59,10 @@ export class MockedParser implements Parser {
checkIfInvalidModule(typeArguments: $FlowFixMe): boolean { checkIfInvalidModule(typeArguments: $FlowFixMe): boolean {
return false; return false;
} }
remapUnionTypeAnnotationMemberNames(
membersTypes: $FlowFixMe[],
): UnionTypeAnnotationMemberType[] {
return [];
}
} }

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

@ -20,7 +20,6 @@ import type {
NativeModuleUnionTypeAnnotation, NativeModuleUnionTypeAnnotation,
Nullable, Nullable,
SchemaType, SchemaType,
UnionTypeAnnotationMemberType,
} from '../CodegenSchema.js'; } from '../CodegenSchema.js';
import type {ParserType} from './errors'; import type {ParserType} from './errors';
import type {Parser} from './parser'; import type {Parser} from './parser';
@ -196,38 +195,14 @@ function parseObjectProperty(
}; };
} }
function remapUnionTypeAnnotationMemberNames(
types: $FlowFixMe,
language: ParserType,
): UnionTypeAnnotationMemberType[] {
const remapLiteral = (item: $FlowFixMe) => {
if (language === 'Flow') {
return item.type
.replace('NumberLiteralTypeAnnotation', 'NumberTypeAnnotation')
.replace('StringLiteralTypeAnnotation', 'StringTypeAnnotation');
}
return item.literal
? item.literal.type
.replace('NumericLiteral', 'NumberTypeAnnotation')
.replace('StringLiteral', 'StringTypeAnnotation')
: 'ObjectTypeAnnotation';
};
return types
.map(remapLiteral)
.filter((value, index, self) => self.indexOf(value) === index);
}
function emitUnionTypeAnnotation( function emitUnionTypeAnnotation(
nullable: boolean, nullable: boolean,
hasteModuleName: string, hasteModuleName: string,
typeAnnotation: $FlowFixMe, typeAnnotation: $FlowFixMe,
language: ParserType, parser: Parser,
): Nullable<NativeModuleUnionTypeAnnotation> { ): Nullable<NativeModuleUnionTypeAnnotation> {
const unionTypes = remapUnionTypeAnnotationMemberNames( const unionTypes = parser.remapUnionTypeAnnotationMemberNames(
typeAnnotation.types, typeAnnotation.types,
language,
); );
// Only support unionTypes of the same kind // Only support unionTypes of the same kind
@ -236,7 +211,7 @@ function emitUnionTypeAnnotation(
hasteModuleName, hasteModuleName,
typeAnnotation, typeAnnotation,
unionTypes, unionTypes,
language, parser.language(),
); );
} }

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

@ -15,7 +15,6 @@ import type {
NativeModuleAliasMap, NativeModuleAliasMap,
NativeModuleArrayTypeAnnotation, NativeModuleArrayTypeAnnotation,
NativeModuleBaseTypeAnnotation, NativeModuleBaseTypeAnnotation,
NativeModuleFunctionTypeAnnotation,
NativeModulePropertyShape, NativeModulePropertyShape,
NativeModuleTypeAnnotation, NativeModuleTypeAnnotation,
NativeModuleSchema, NativeModuleSchema,
@ -320,7 +319,7 @@ function translateTypeAnnotation(
nullable, nullable,
hasteModuleName, hasteModuleName,
typeAnnotation, typeAnnotation,
language, parser,
); );
} }
// Fallthrough // Fallthrough

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

@ -10,6 +10,7 @@
'use strict'; 'use strict';
import type {UnionTypeAnnotationMemberType} from '../../CodegenSchema.js';
import type {ParserType} from '../errors'; import type {ParserType} from '../errors';
import type {Parser} from '../parser'; import type {Parser} from '../parser';
@ -66,6 +67,20 @@ class TypeScriptParser implements Parser {
typeArguments.params[0].typeName.name !== 'Spec' typeArguments.params[0].typeName.name !== 'Spec'
); );
} }
remapUnionTypeAnnotationMemberNames(
membersTypes: $FlowFixMe[],
): UnionTypeAnnotationMemberType[] {
const remapLiteral = (item: $FlowFixMe) => {
return item.literal
? item.literal.type
.replace('NumericLiteral', 'NumberTypeAnnotation')
.replace('StringLiteral', 'StringTypeAnnotation')
: 'ObjectTypeAnnotation';
};
return [...new Set(membersTypes.map(remapLiteral))];
}
} }
module.exports = { module.exports = {
TypeScriptParser, TypeScriptParser,