From 6d5be2630cbd8569bb3e2f50dddb05eaf79924c2 Mon Sep 17 00:00:00 2001 From: Denis Date: Mon, 31 Jul 2023 04:56:25 -0700 Subject: [PATCH] refactor(codegen): add getLiteralValue in parser (#38651) Summary: [Codegen 130] This PR add a `getLiteralValue` function to the Parser interface, which returns the literal value of an union represented, given an option. as requested on https://github.com/facebook/react-native/issues/34872 ## Changelog: [INTERNAL] [ADDED] - Add `getLiteralValue` function to codegen Parser Pull Request resolved: https://github.com/facebook/react-native/pull/38651 Test Plan: Run `yarn jest react-native-codegen` and ensure CI is green Reviewed By: cipolleschi Differential Revision: D47912960 Pulled By: rshest fbshipit-source-id: d9426fef4c0f92c5244d5c4c72202ec29099b76e --- .../src/parsers/__tests__/parsers-test.js | 38 +++++++++++++++++++ .../src/parsers/flow/components/events.js | 8 +++- .../src/parsers/flow/parser.js | 4 ++ .../src/parsers/parser.js | 7 ++++ .../src/parsers/parserMock.js | 4 ++ .../parsers/typescript/components/events.js | 8 +++- .../src/parsers/typescript/parser.js | 4 ++ 7 files changed, 69 insertions(+), 4 deletions(-) diff --git a/packages/react-native-codegen/src/parsers/__tests__/parsers-test.js b/packages/react-native-codegen/src/parsers/__tests__/parsers-test.js index 4df7f89cc1d..b6c411532f8 100644 --- a/packages/react-native-codegen/src/parsers/__tests__/parsers-test.js +++ b/packages/react-native-codegen/src/parsers/__tests__/parsers-test.js @@ -430,6 +430,22 @@ describe('FlowParser', () => { expect(parser.getObjectProperties(declaration)).toEqual(undefined); }); }); + describe('getLiteralValue', () => { + it('returns value of an union represented, given an option', () => { + const option = { + value: 'LiteralValue', + }; + const expected = option.value; + + expect(parser.getLiteralValue(option)).toEqual(expected); + }); + + it('returns undefined if option does not have value', () => { + const option = {}; + + expect(parser.getLiteralValue(option)).toEqual(undefined); + }); + }); }); describe('TypeScriptParser', () => { @@ -821,4 +837,26 @@ describe('TypeScriptParser', () => { expect(parser.getObjectProperties(declaration)).toEqual(undefined); }); }); + + describe('getLiteralValue', () => { + it('returns literal value of an union represented, given an option', () => { + const literal = { + value: 'LiteralValue', + }; + const option = { + literal, + }; + const expected = literal.value; + + expect(parser.getLiteralValue(option)).toEqual(expected); + }); + + it('returns undefined if literal does not have value', () => { + const option = { + literal: {}, + }; + + expect(parser.getLiteralValue(option)).toEqual(undefined); + }); + }); }); diff --git a/packages/react-native-codegen/src/parsers/flow/components/events.js b/packages/react-native-codegen/src/parsers/flow/components/events.js index f0fd64e4eb1..351601a301e 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/events.js +++ b/packages/react-native-codegen/src/parsers/flow/components/events.js @@ -78,7 +78,9 @@ function getPropertyType( optional, typeAnnotation: { type: 'StringEnumTypeAnnotation', - options: typeAnnotation.types.map(option => option.value), + options: typeAnnotation.types.map(option => + parser.getLiteralValue(option), + ), }, }; case 'UnsafeMixed': @@ -119,7 +121,9 @@ function extractArrayElementType( case 'UnionTypeAnnotation': return { type: 'StringEnumTypeAnnotation', - options: typeAnnotation.types.map(option => option.value), + options: typeAnnotation.types.map(option => + parser.getLiteralValue(option), + ), }; case 'UnsafeMixed': return {type: 'MixedTypeAnnotation'}; diff --git a/packages/react-native-codegen/src/parsers/flow/parser.js b/packages/react-native-codegen/src/parsers/flow/parser.js index 676a206588d..0b2a2a00d7e 100644 --- a/packages/react-native-codegen/src/parsers/flow/parser.js +++ b/packages/react-native-codegen/src/parsers/flow/parser.js @@ -543,6 +543,10 @@ class FlowParser implements Parser { getObjectProperties(typeAnnotation: $FlowFixMe): $FlowFixMe { return typeAnnotation.properties; } + + getLiteralValue(option: $FlowFixMe): $FlowFixMe { + return option.value; + } } module.exports = { diff --git a/packages/react-native-codegen/src/parsers/parser.js b/packages/react-native-codegen/src/parsers/parser.js index 98901b41755..ff855417a95 100644 --- a/packages/react-native-codegen/src/parsers/parser.js +++ b/packages/react-native-codegen/src/parsers/parser.js @@ -414,4 +414,11 @@ export interface Parser { * @returns: the properties of an object represented by a type annotation. */ getObjectProperties(typeAnnotation: $FlowFixMe): $FlowFixMe; + + /** + * Given a option return the literal value. + * @parameter option + * @returns: the literal value of an union represented. + */ + getLiteralValue(option: $FlowFixMe): $FlowFixMe; } diff --git a/packages/react-native-codegen/src/parsers/parserMock.js b/packages/react-native-codegen/src/parsers/parserMock.js index 6e5f78de77d..898a6af7307 100644 --- a/packages/react-native-codegen/src/parsers/parserMock.js +++ b/packages/react-native-codegen/src/parsers/parserMock.js @@ -482,4 +482,8 @@ export class MockedParser implements Parser { getObjectProperties(typeAnnotation: $FlowFixMe): $FlowFixMe { return typeAnnotation.properties; } + + getLiteralValue(option: $FlowFixMe): $FlowFixMe { + return option.value; + } } diff --git a/packages/react-native-codegen/src/parsers/typescript/components/events.js b/packages/react-native-codegen/src/parsers/typescript/components/events.js index 230b9b512a6..0b8045ac32e 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/events.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/events.js @@ -77,7 +77,9 @@ function getPropertyType( optional, typeAnnotation: { type: 'StringEnumTypeAnnotation', - options: typeAnnotation.types.map(option => option.literal.value), + options: typeAnnotation.types.map(option => + parser.getLiteralValue(option), + ), }, }; case 'UnsafeMixed': @@ -127,7 +129,9 @@ function extractArrayElementType( case 'TSUnionType': return { type: 'StringEnumTypeAnnotation', - options: typeAnnotation.types.map(option => option.literal.value), + options: typeAnnotation.types.map(option => + parser.getLiteralValue(option), + ), }; case 'TSTypeLiteral': return { diff --git a/packages/react-native-codegen/src/parsers/typescript/parser.js b/packages/react-native-codegen/src/parsers/typescript/parser.js index 4a18396b5c9..b1c4745282f 100644 --- a/packages/react-native-codegen/src/parsers/typescript/parser.js +++ b/packages/react-native-codegen/src/parsers/typescript/parser.js @@ -557,6 +557,10 @@ class TypeScriptParser implements Parser { getObjectProperties(typeAnnotation: $FlowFixMe): $FlowFixMe { return typeAnnotation.members; } + + getLiteralValue(option: $FlowFixMe): $FlowFixMe { + return option.literal.value; + } } module.exports = {