From 9301c8fea57e8fb4bbe1f1a0476b22098bc1e1fa Mon Sep 17 00:00:00 2001 From: shubham0142 Date: Wed, 17 May 2023 03:45:34 -0700 Subject: [PATCH] codegen 107: add interfaceDeclaration prop in parsers (#37361) Summary: part of codegen issue https://github.com/facebook/react-native/issues/34872 > Add a interfaceDeclaration: string property to the Parser class. Implement it in the Flow parser so that it returns InterfaceDeclaration and in the TypeScriptParser so that it returns TSInterfaceDeclaration. Replace the case in the switch in the [parsers/typescript/utils.js](https://github.com/facebook/react-native/blob/e133100721939108b0f28dfa9f60ac627c804018/packages/react-native-codegen/src/parsers/typescript/utils.js#L69) with this prop. bypass-github-export-checks ## Changelog: [Internal][Added]: Add interfaceDeclaration property in parsers Pull Request resolved: https://github.com/facebook/react-native/pull/37361 Test Plan: yarn test Differential Revision: D45769316 Pulled By: cipolleschi fbshipit-source-id: a7f3c6339984e643e3e35fb5b920442aea027b78 --- .../src/parsers/__tests__/parsers-test.js | 12 ++++++++++++ .../react-native-codegen/src/parsers/flow/parser.js | 1 + packages/react-native-codegen/src/parsers/parser.js | 5 +++++ .../react-native-codegen/src/parsers/parserMock.js | 1 + .../src/parsers/typescript/parser.js | 1 + .../src/parsers/typescript/utils.js | 4 ++-- 6 files changed, 22 insertions(+), 2 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 45b9497b1e..92366bc181 100644 --- a/packages/react-native-codegen/src/parsers/__tests__/parsers-test.js +++ b/packages/react-native-codegen/src/parsers/__tests__/parsers-test.js @@ -317,6 +317,12 @@ describe('FlowParser', () => { expect(parser.enumDeclaration).toEqual('EnumDeclaration'); }); }); + + describe('interfaceDelcaration', () => { + it('returns interfaceDelcaration Property', () => { + expect(parser.interfaceDelcaration).toEqual('InterfaceDeclaration'); + }); + }); }); describe('TypeScriptParser', () => { @@ -606,4 +612,10 @@ describe('TypeScriptParser', () => { expect(parser.enumDeclaration).toEqual('TSEnumDeclaration'); }); }); + + describe('interfaceDelcaration', () => { + it('returns interfaceDelcaration Property', () => { + expect(parser.interfaceDelcaration).toEqual('TSInterfaceDeclaration'); + }); + }); }); diff --git a/packages/react-native-codegen/src/parsers/flow/parser.js b/packages/react-native-codegen/src/parsers/flow/parser.js index 841c472b3f..1eb2f1d387 100644 --- a/packages/react-native-codegen/src/parsers/flow/parser.js +++ b/packages/react-native-codegen/src/parsers/flow/parser.js @@ -64,6 +64,7 @@ class FlowParser implements Parser { typeParameterInstantiation: string = 'TypeParameterInstantiation'; typeAlias: string = 'TypeAlias'; enumDeclaration: string = 'EnumDeclaration'; + interfaceDelcaration: string = 'InterfaceDeclaration'; isProperty(property: $FlowFixMe): boolean { return property.type === 'ObjectTypeProperty'; diff --git a/packages/react-native-codegen/src/parsers/parser.js b/packages/react-native-codegen/src/parsers/parser.js index c3f83afcae..8ed0526499 100644 --- a/packages/react-native-codegen/src/parsers/parser.js +++ b/packages/react-native-codegen/src/parsers/parser.js @@ -93,6 +93,11 @@ export interface Parser { */ enumDeclaration: string; + /** + * InterfaceDelcaration property of the Parser + */ + interfaceDelcaration: string; + /** * Given a declaration, it returns true if it is a property */ diff --git a/packages/react-native-codegen/src/parsers/parserMock.js b/packages/react-native-codegen/src/parsers/parserMock.js index 65290d70f7..ef3c80d029 100644 --- a/packages/react-native-codegen/src/parsers/parserMock.js +++ b/packages/react-native-codegen/src/parsers/parserMock.js @@ -62,6 +62,7 @@ export class MockedParser implements Parser { typeParameterInstantiation: string = 'TypeParameterInstantiation'; typeAlias: string = 'TypeAlias'; enumDeclaration: string = 'EnumDeclaration'; + interfaceDelcaration: string = 'InterfaceDelcaration'; isProperty(property: $FlowFixMe): boolean { return property.type === 'ObjectTypeProperty'; diff --git a/packages/react-native-codegen/src/parsers/typescript/parser.js b/packages/react-native-codegen/src/parsers/typescript/parser.js index 61c7dc050f..4213cb9212 100644 --- a/packages/react-native-codegen/src/parsers/typescript/parser.js +++ b/packages/react-native-codegen/src/parsers/typescript/parser.js @@ -62,6 +62,7 @@ class TypeScriptParser implements Parser { typeParameterInstantiation: string = 'TSTypeParameterInstantiation'; typeAlias: string = 'TSTypeAliasDeclaration'; enumDeclaration: string = 'TSEnumDeclaration'; + interfaceDelcaration: string = 'TSInterfaceDeclaration'; isProperty(property: $FlowFixMe): boolean { return property.type === 'TSPropertySignature'; diff --git a/packages/react-native-codegen/src/parsers/typescript/utils.js b/packages/react-native-codegen/src/parsers/typescript/utils.js index 1cdf0fe326..e7474f70cb 100644 --- a/packages/react-native-codegen/src/parsers/typescript/utils.js +++ b/packages/react-native-codegen/src/parsers/typescript/utils.js @@ -67,7 +67,7 @@ function resolveTypeAnnotation( node = resolvedTypeAnnotation.typeAnnotation; break; } - case 'TSInterfaceDeclaration': { + case parser.interfaceDelcaration: { typeResolutionStatus = { successful: true, type: 'alias', @@ -87,7 +87,7 @@ function resolveTypeAnnotation( } default: { throw new TypeError( - `A non GenericTypeAnnotation must be a type declaration ('${parser.typeAlias}'), an interface ('TSInterfaceDeclaration'), or enum ('${parser.enumDeclaration}'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`, + `A non GenericTypeAnnotation must be a type declaration ('${parser.typeAlias}'), an interface ('${parser.interfaceDelcaration}'), or enum ('${parser.enumDeclaration}'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`, ); } }