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
This commit is contained in:
Denis
2023-07-31 04:56:25 -07:00
committed by Facebook GitHub Bot
parent 8a63b91525
commit 6d5be2630c
7 changed files with 69 additions and 4 deletions
@@ -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);
});
});
});
@@ -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'};
@@ -543,6 +543,10 @@ class FlowParser implements Parser {
getObjectProperties(typeAnnotation: $FlowFixMe): $FlowFixMe {
return typeAnnotation.properties;
}
getLiteralValue(option: $FlowFixMe): $FlowFixMe {
return option.value;
}
}
module.exports = {
+7
View File
@@ -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;
}
@@ -482,4 +482,8 @@ export class MockedParser implements Parser {
getObjectProperties(typeAnnotation: $FlowFixMe): $FlowFixMe {
return typeAnnotation.properties;
}
getLiteralValue(option: $FlowFixMe): $FlowFixMe {
return option.value;
}
}
@@ -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 {
@@ -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 = {