Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Codegen] Add getObjectProperties function to Parser #37789

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,53 @@ describe('FlowParser', () => {
);
});
});

describe('getObjectProperties', () => {
it('returns properties of an object represented by a type annotation', () => {
const properties = [
{
type: 'ObjectTypeProperty',
key: {
type: 'Identifier',
name: 'a',
},
value: {
type: 'StringTypeAnnotation',
range: [],
},
},
{
type: 'ObjectTypeProperty',
key: {
type: 'Identifier',
name: 'b',
},
optional: true,
value: {
type: 'BooleanTypeAnnotation',
range: [],
},
},
];

const typeAnnotation = {
type: 'TypeAlias',
properties: properties,
};

const expected = properties;

expect(parser.getObjectProperties(typeAnnotation)).toEqual(expected);
});

it('returns undefined if typeAnnotation does not have properties', () => {
const declaration = {
type: 'TypeAlias',
};

expect(parser.getObjectProperties(declaration)).toEqual(undefined);
});
});
});

describe('TypeScriptParser', () => {
Expand Down Expand Up @@ -727,4 +774,51 @@ describe('TypeScriptParser', () => {
);
});
});

describe('getObjectProperties', () => {
it('returns members of an object represented by a type annotation', () => {
const members = [
{
type: 'ObjectTypeProperty',
key: {
type: 'Identifier',
name: 'a',
},
value: {
type: 'StringTypeAnnotation',
range: [],
},
},
{
type: 'ObjectTypeProperty',
key: {
type: 'Identifier',
name: 'b',
},
optional: true,
value: {
type: 'BooleanTypeAnnotation',
range: [],
},
},
];

const typeAnnotation = {
type: 'TypeAlias',
members: members,
};

const expected = members;

expect(parser.getObjectProperties(typeAnnotation)).toEqual(expected);
});

it('returns undefined if typeAnnotation does not have members', () => {
const declaration = {
type: 'TypeAlias',
};

expect(parser.getObjectProperties(declaration)).toEqual(undefined);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ function getPropertyType(
optional,
typeAnnotation: {
type: 'ObjectTypeAnnotation',
properties: typeAnnotation.properties.map(member =>
buildPropertiesForEvent(member, parser, getPropertyType),
),
properties: parser
.getObjectProperties(typeAnnotation)
.map(member =>
buildPropertiesForEvent(member, parser, getPropertyType),
),
},
};
case 'UnionTypeAnnotation':
Expand Down Expand Up @@ -128,9 +130,11 @@ function extractArrayElementType(
case 'ObjectTypeAnnotation':
return {
type: 'ObjectTypeAnnotation',
properties: typeAnnotation.properties.map(member =>
buildPropertiesForEvent(member, parser, getPropertyType),
),
properties: parser
.getObjectProperties(typeAnnotation)
.map(member =>
buildPropertiesForEvent(member, parser, getPropertyType),
),
};
case 'ArrayTypeAnnotation':
return {
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 @@ -539,6 +539,10 @@ class FlowParser implements Parser {
? typeAnnotation.id.name
: typeAnnotation.type;
}

getObjectProperties(typeAnnotation: $FlowFixMe): $FlowFixMe {
return typeAnnotation.properties;
}
}

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 @@ -407,4 +407,11 @@ export interface Parser {
* @returns: the extracted type.
*/
extractTypeFromTypeAnnotation(typeAnnotation: $FlowFixMe): string;

/**
* Given a typeAnnotation return the properties of an object.
* @parameter property
* @returns: the properties of an object represented by a type annotation.
*/
getObjectProperties(typeAnnotation: $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 @@ -478,4 +478,8 @@ export class MockedParser implements Parser {
? typeAnnotation.id.name
: typeAnnotation.type;
}

getObjectProperties(typeAnnotation: $FlowFixMe): $FlowFixMe {
return typeAnnotation.properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ function getPropertyType(
optional,
typeAnnotation: {
type: 'ObjectTypeAnnotation',
properties: typeAnnotation.members.map(member =>
buildPropertiesForEvent(member, parser, getPropertyType),
),
properties: parser
.getObjectProperties(typeAnnotation)
.map(member =>
buildPropertiesForEvent(member, parser, getPropertyType),
),
},
};
case 'TSUnionType':
Expand Down Expand Up @@ -138,9 +140,11 @@ function extractArrayElementType(
case 'TSTypeLiteral':
return {
type: 'ObjectTypeAnnotation',
properties: typeAnnotation.members.map(member =>
buildPropertiesForEvent(member, parser, getPropertyType),
),
properties: parser
.getObjectProperties(typeAnnotation)
.map(member =>
buildPropertiesForEvent(member, parser, getPropertyType),
),
};
case 'TSArrayType':
return {
Expand Down Expand Up @@ -183,7 +187,7 @@ function findEventArgumentsAndType(

if (typeAnnotation.type === 'TSTypeLiteral') {
return {
argumentProps: typeAnnotation.members,
argumentProps: parser.getObjectProperties(typeAnnotation),
paperTopLevelNameDeprecated: paperName,
bubblingType,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,10 @@ class TypeScriptParser implements Parser {
? typeAnnotation.typeName.name
: typeAnnotation.type;
}

getObjectProperties(typeAnnotation: $FlowFixMe): $FlowFixMe {
return typeAnnotation.members;
}
}

module.exports = {
Expand Down