Skip to content

Commit

Permalink
Extract getTypeAnnotationFromProperty from buildPropertiesForEvent in…
Browse files Browse the repository at this point in the history
…to specific parsers (#37573)

Summary:
This PR aims to remove the duplicated logic in [flow|typescript]/components/events.js files to move it in specific parsers. It is a task of #34872:
> [Codegen 114 - Assigned to MaeIg] Add a function getTypeAnnotationFromProperty(property) in the Parser object and implement it in FlowParser and TypeScriptParser, using the implementation you can find in the [parsers/flow/components/events.js](https://github.com/facebook/react-native/blob/e133100721939108b0f28dfa9f60ac627c804018/packages/react-native-codegen/src/parsers/flow/components/events.js#L174-L177) and parsers/typescript/components/events.js. Use the parsers in the buildPropertiesForEvent.

## Changelog:

[Internal] [Changed] - Extract getTypeAnnotationFromProperty from buildPropertiesForEvent into specific parsers

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests

Pull Request resolved: #37573

Test Plan:
Flow:
<img width="600" alt="image" src="https://github.com/facebook/react-native/assets/40902940/554bb82d-b492-4550-9a84-254fc4f78285">

Eslint:
<img width="498" alt="image" src="https://github.com/facebook/react-native/assets/40902940/53a302b7-c2aa-4008-9583-2e3b4cddc14c">

Jest:
<img width="395" alt="image" src="https://github.com/facebook/react-native/assets/40902940/c7ff53f1-2be1-4099-b2e6-081128cf5333">

Reviewed By: dmytrorykun

Differential Revision: D46190831

Pulled By: cipolleschi

fbshipit-source-id: 393a4c4968139ee7061ed4ea524d083af6950e38
  • Loading branch information
MaeIg authored and facebook-github-bot committed Jun 6, 2023
1 parent 8ffaede commit 663a018
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,41 @@ describe('FlowParser', () => {
});
});

describe('getTypeAnnotationFromProperty', () => {
describe('when property value type is NullableTypeAnnotation', () => {
it('returns typeAnnotation of the value', () => {
const typeAnnotation = {
type: 'StringTypeAnnotation',
};

const property = {
value: {
type: 'NullableTypeAnnotation',
typeAnnotation: typeAnnotation,
},
};

expect(parser.getTypeAnnotationFromProperty(property)).toEqual(
typeAnnotation,
);
});
});

describe('when property value type is not NullableTypeAnnotation', () => {
it('returns the value', () => {
const value = {
type: 'StringTypeAnnotation',
};

const property = {
value: value,
};

expect(parser.getTypeAnnotationFromProperty(property)).toEqual(value);
});
});
});

describe('typeAlias', () => {
it('returns typeAlias Property', () => {
expect(parser.typeAlias).toEqual('TypeAlias');
Expand Down Expand Up @@ -601,6 +636,30 @@ describe('TypeScriptParser', () => {
});
});

describe('getTypeAnnotationFromProperty', () => {
it('returns the type annotation', () => {
const typeAnnotation = {
type: 'TSStringKeyword',
key: {
type: 'Identifier',
name: 'b',
},
members: [],
};

const property = {
typeAnnotation: {
type: 'TSTypeAnnotation',
typeAnnotation: typeAnnotation,
},
};

expect(parser.getTypeAnnotationFromProperty(property)).toEqual(
typeAnnotation,
);
});
});

describe('typeAlias', () => {
it('returns typeAlias Property', () => {
expect(parser.typeAlias).toEqual('TSTypeAliasDeclaration');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,7 @@ function buildPropertiesForEvent(
): NamedShape<EventTypeAnnotation> {
const name = property.key.name;
const optional = parser.isOptionalProperty(property);
let typeAnnotation =
property.value.type === 'NullableTypeAnnotation'
? property.value.typeAnnotation
: property.value;
const typeAnnotation = parser.getTypeAnnotationFromProperty(property);

return getPropertyType(name, optional, typeAnnotation, parser);
}
Expand Down
6 changes: 6 additions & 0 deletions packages/react-native-codegen/src/parsers/flow/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,12 @@ class FlowParser implements Parser {
return getSchemaInfo;
}

getTypeAnnotationFromProperty(property: PropAST): $FlowFixMe {
return property.value.type === 'NullableTypeAnnotation'
? property.value.typeAnnotation
: property.value;
}

getGetTypeAnnotationFN(): GetTypeAnnotationFN {
return getTypeAnnotation;
}
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 @@ -357,6 +357,13 @@ export interface Parser {

getGetSchemaInfoFN(): GetSchemaInfoFN;

/**
* Given a property return the type annotation.
* @parameter property
* @returns: the annotation for a type in the AST.
*/
getTypeAnnotationFromProperty(property: PropAST): $FlowFixMe;

getResolvedTypeAnnotation(
typeAnnotation: $FlowFixMe,
types: TypeDeclarationMap,
Expand Down
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 @@ -282,6 +282,10 @@ export class MockedParser implements Parser {
return property.optional || false;
}

getTypeAnnotationFromProperty(property: PropAST): $FlowFixMe {
return property.typeAnnotation.typeAnnotation;
}

getGetTypeAnnotationFN(): GetTypeAnnotationFN {
return () => {
return {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ function buildPropertiesForEvent(
): NamedShape<EventTypeAnnotation> {
const name = property.key.name;
const optional = parser.isOptionalProperty(property);
let typeAnnotation = property.typeAnnotation.typeAnnotation;
const typeAnnotation = parser.getTypeAnnotationFromProperty(property);

return getPropertyType(name, optional, typeAnnotation, parser);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ class TypeScriptParser implements Parser {
return getSchemaInfo;
}

getTypeAnnotationFromProperty(property: PropAST): $FlowFixMe {
return property.typeAnnotation.typeAnnotation;
}

getGetTypeAnnotationFN(): GetTypeAnnotationFN {
return getTypeAnnotation;
}
Expand Down

0 comments on commit 663a018

Please sign in to comment.