Skip to content

Commit

Permalink
refactor(codegen): add getLiteralValue in parser (#38651)
Browse files Browse the repository at this point in the history
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 #34872

## Changelog:

[INTERNAL] [ADDED] - Add `getLiteralValue` function to codegen Parser

Pull Request resolved: #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
  • Loading branch information
Zwem authored and facebook-github-bot committed Jul 31, 2023
1 parent 8a63b91 commit 6d5be26
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -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'};
Expand Down
4 changes: 4 additions & 0 deletions packages/react-native-codegen/src/parsers/flow/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,10 @@ class FlowParser implements Parser {
getObjectProperties(typeAnnotation: $FlowFixMe): $FlowFixMe {
return typeAnnotation.properties;
}

getLiteralValue(option: $FlowFixMe): $FlowFixMe {
return option.value;
}
}

module.exports = {
Expand Down
7 changes: 7 additions & 0 deletions packages/react-native-codegen/src/parsers/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 4 additions & 0 deletions packages/react-native-codegen/src/parsers/parserMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,4 +482,8 @@ export class MockedParser implements Parser {
getObjectProperties(typeAnnotation: $FlowFixMe): $FlowFixMe {
return typeAnnotation.properties;
}

getLiteralValue(option: $FlowFixMe): $FlowFixMe {
return option.value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 6d5be26

Please sign in to comment.