Skip to content

Commit

Permalink
move remapUnionTypeAnnotationMemberNames to Parser (#35314)
Browse files Browse the repository at this point in the history
Summary:
Part of #34872 (comment)

> [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: #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
  • Loading branch information
youedd authored and facebook-github-bot committed Nov 14, 2022
1 parent 1bb67e7 commit a7ae988
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ const {

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');
Expand Down Expand Up @@ -399,8 +401,6 @@ describe('emitUnionTypeAnnotation', () => {
const hasteModuleName = 'SampleTurboModule';

describe('when language is flow', () => {
const language: ParserType = 'Flow';

describe('when members type is numeric', () => {
const typeAnnotation = {
type: 'UnionTypeAnnotation',
Expand All @@ -415,7 +415,7 @@ describe('emitUnionTypeAnnotation', () => {
true,
hasteModuleName,
typeAnnotation,
language,
flowParser,
);

const expected = {
Expand All @@ -436,7 +436,7 @@ describe('emitUnionTypeAnnotation', () => {
false,
hasteModuleName,
typeAnnotation,
language,
flowParser,
);

const expected = {
Expand All @@ -463,7 +463,7 @@ describe('emitUnionTypeAnnotation', () => {
true,
hasteModuleName,
typeAnnotation,
language,
flowParser,
);

const expected = {
Expand All @@ -484,7 +484,7 @@ describe('emitUnionTypeAnnotation', () => {
false,
hasteModuleName,
typeAnnotation,
language,
flowParser,
);

const expected = {
Expand All @@ -508,7 +508,7 @@ describe('emitUnionTypeAnnotation', () => {
true,
hasteModuleName,
typeAnnotation,
language,
flowParser,
);

const expected = {
Expand All @@ -529,7 +529,7 @@ describe('emitUnionTypeAnnotation', () => {
false,
hasteModuleName,
typeAnnotation,
language,
flowParser,
);

const expected = {
Expand Down Expand Up @@ -562,15 +562,15 @@ describe('emitUnionTypeAnnotation', () => {
hasteModuleName,
typeAnnotation,
unionTypes,
language,
flowParser.language(),
);

expect(() => {
emitUnionTypeAnnotation(
true,
hasteModuleName,
typeAnnotation,
language,
flowParser,
);
}).toThrow(expected);
});
Expand All @@ -582,15 +582,15 @@ describe('emitUnionTypeAnnotation', () => {
hasteModuleName,
typeAnnotation,
unionTypes,
language,
flowParser.language(),
);

expect(() => {
emitUnionTypeAnnotation(
false,
hasteModuleName,
typeAnnotation,
language,
flowParser,
);
}).toThrow(expected);
});
Expand All @@ -599,8 +599,6 @@ describe('emitUnionTypeAnnotation', () => {
});

describe('when language is typescript', () => {
const language: ParserType = 'TypeScript';

describe('when members type is numeric', () => {
const typeAnnotation = {
type: 'TSUnionType',
Expand All @@ -621,7 +619,7 @@ describe('emitUnionTypeAnnotation', () => {
true,
hasteModuleName,
typeAnnotation,
language,
typeScriptParser,
);

const expected = {
Expand All @@ -642,7 +640,7 @@ describe('emitUnionTypeAnnotation', () => {
false,
hasteModuleName,
typeAnnotation,
language,
typeScriptParser,
);

const expected = {
Expand Down Expand Up @@ -675,7 +673,7 @@ describe('emitUnionTypeAnnotation', () => {
true,
hasteModuleName,
typeAnnotation,
language,
typeScriptParser,
);

const expected = {
Expand All @@ -696,7 +694,7 @@ describe('emitUnionTypeAnnotation', () => {
false,
hasteModuleName,
typeAnnotation,
language,
typeScriptParser,
);

const expected = {
Expand Down Expand Up @@ -727,7 +725,7 @@ describe('emitUnionTypeAnnotation', () => {
true,
hasteModuleName,
typeAnnotation,
language,
typeScriptParser,
);

const expected = {
Expand All @@ -748,7 +746,7 @@ describe('emitUnionTypeAnnotation', () => {
false,
hasteModuleName,
typeAnnotation,
language,
typeScriptParser,
);

const expected = {
Expand Down Expand Up @@ -789,15 +787,15 @@ describe('emitUnionTypeAnnotation', () => {
hasteModuleName,
typeAnnotation,
unionTypes,
language,
typeScriptParser.language(),
);

expect(() => {
emitUnionTypeAnnotation(
true,
hasteModuleName,
typeAnnotation,
language,
typeScriptParser,
);
}).toThrow(expected);
});
Expand All @@ -809,15 +807,15 @@ describe('emitUnionTypeAnnotation', () => {
hasteModuleName,
typeAnnotation,
unionTypes,
language,
typeScriptParser.language(),
);

expect(() => {
emitUnionTypeAnnotation(
false,
hasteModuleName,
typeAnnotation,
language,
typeScriptParser,
);
}).toThrow(expected);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {TypeScriptParser} from '../typescript/parser';
import {FlowParser} from '../flow/parser';

const hasteModuleName = 'moduleName';
describe('TypeScriptParser', () => {
describe('FlowParser', () => {
const parser = new FlowParser();
describe('getKeyName', () => {
describe('when propertyOrIndex is ObjectTypeProperty', () => {
Expand Down Expand Up @@ -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();
describe('getKeyName', () => {
describe('when propertyOrIndex is TSPropertySignature', () => {
Expand Down Expand Up @@ -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',
]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ function translateTypeAnnotation(
nullable,
hasteModuleName,
typeAnnotation,
language,
parser,
);
}
// Fallthrough
Expand Down
13 changes: 13 additions & 0 deletions packages/react-native-codegen/src/parsers/flow/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

'use strict';

import type {UnionTypeAnnotationMemberType} from '../../CodegenSchema.js';
import type {ParserType} from '../errors';
import type {Parser} from '../parser';

Expand Down Expand Up @@ -63,6 +64,18 @@ class FlowParser implements Parser {
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 = {
Expand Down
9 changes: 9 additions & 0 deletions packages/react-native-codegen/src/parsers/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

'use strict';

import type {UnionTypeAnnotationMemberType} from '../CodegenSchema.js';
import type {ParserType} from './errors';

/**
Expand Down Expand Up @@ -58,4 +59,12 @@ export interface Parser {
* @returns: a boolean specifying if the Module is Invalid.
*/
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[];
}
7 changes: 7 additions & 0 deletions packages/react-native-codegen/src/parsers/parserMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

'use strict';

import type {UnionTypeAnnotationMemberType} from '../CodegenSchema.js';
import type {Parser} from './parser';
import type {ParserType} from './errors';

Expand Down Expand Up @@ -58,4 +59,10 @@ export class MockedParser implements Parser {
checkIfInvalidModule(typeArguments: $FlowFixMe): boolean {
return false;
}

remapUnionTypeAnnotationMemberNames(
membersTypes: $FlowFixMe[],
): UnionTypeAnnotationMemberType[] {
return [];
}
}
Loading

0 comments on commit a7ae988

Please sign in to comment.