From 8bcfc493eae3686986372b3764995ba80e7e46fb Mon Sep 17 00:00:00 2001 From: Siddarth Kumar Date: Mon, 31 Jul 2023 07:52:19 -0700 Subject: [PATCH] add `getPaperTopLevelNameDeprecated` to parsers (#38683) Summary: [Codegen 135] This PR introduces `getPaperTopLevelNameDeprecated` to parser base class and abstracts the logic out of typescript and parser events as requested on https://github.com/facebook/react-native/issues/34872 ## Changelog: [Internal] [Changed] - Add `getPaperTopLevelNameDeprecated` to parser base class and update usages. Pull Request resolved: https://github.com/facebook/react-native/pull/38683 Test Plan: Run `yarn jest react-native-codegen` locally and ensure CI is green ## Screenshot of test passing locally: Screenshot 2023-07-30 at 10 04 24 AM Reviewed By: cipolleschi Differential Revision: D47902816 Pulled By: rshest fbshipit-source-id: 6fab53e02cfc3f0aaa3ffd795c3fe1d2f723e060 --- .../src/parsers/flow/components/events.js | 4 +--- packages/react-native-codegen/src/parsers/flow/parser.js | 6 ++++++ packages/react-native-codegen/src/parsers/parser.js | 7 +++++++ packages/react-native-codegen/src/parsers/parserMock.js | 6 ++++++ .../src/parsers/typescript/components/events.js | 4 +--- .../react-native-codegen/src/parsers/typescript/parser.js | 6 ++++++ 6 files changed, 27 insertions(+), 6 deletions(-) diff --git a/packages/react-native-codegen/src/parsers/flow/components/events.js b/packages/react-native-codegen/src/parsers/flow/components/events.js index 351601a301e..5b3d260b503 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/events.js +++ b/packages/react-native-codegen/src/parsers/flow/components/events.js @@ -193,9 +193,7 @@ function findEventArgumentsAndType( } else if (name === 'BubblingEventHandler' || name === 'DirectEventHandler') { const eventType = name === 'BubblingEventHandler' ? 'bubble' : 'direct'; const paperTopLevelNameDeprecated = - typeAnnotation.typeParameters.params.length > 1 - ? typeAnnotation.typeParameters.params[1].value - : null; + parser.getPaperTopLevelNameDeprecated(typeAnnotation); if ( typeAnnotation.typeParameters.params[0].type === parser.nullLiteralTypeAnnotation diff --git a/packages/react-native-codegen/src/parsers/flow/parser.js b/packages/react-native-codegen/src/parsers/flow/parser.js index 0b2a2a00d7e..b54cff76f40 100644 --- a/packages/react-native-codegen/src/parsers/flow/parser.js +++ b/packages/react-native-codegen/src/parsers/flow/parser.js @@ -547,6 +547,12 @@ class FlowParser implements Parser { getLiteralValue(option: $FlowFixMe): $FlowFixMe { return option.value; } + + getPaperTopLevelNameDeprecated(typeAnnotation: $FlowFixMe): $FlowFixMe { + return typeAnnotation.typeParameters.params.length > 1 + ? typeAnnotation.typeParameters.params[1].value + : null; + } } module.exports = { diff --git a/packages/react-native-codegen/src/parsers/parser.js b/packages/react-native-codegen/src/parsers/parser.js index ff855417a95..3c0a2b76551 100644 --- a/packages/react-native-codegen/src/parsers/parser.js +++ b/packages/react-native-codegen/src/parsers/parser.js @@ -421,4 +421,11 @@ export interface Parser { * @returns: the literal value of an union represented. */ getLiteralValue(option: $FlowFixMe): $FlowFixMe; + + /** + * Given a type annotation, it returns top level name in the AST if it exists else returns null. + * @parameter typeAnnotation: the annotation for a type in the AST. + * @returns: the top level name properties in the AST if it exists else null. + */ + getPaperTopLevelNameDeprecated(typeAnnotation: $FlowFixMe): $FlowFixMe; } diff --git a/packages/react-native-codegen/src/parsers/parserMock.js b/packages/react-native-codegen/src/parsers/parserMock.js index 898a6af7307..f8e9cc5d81e 100644 --- a/packages/react-native-codegen/src/parsers/parserMock.js +++ b/packages/react-native-codegen/src/parsers/parserMock.js @@ -486,4 +486,10 @@ export class MockedParser implements Parser { getLiteralValue(option: $FlowFixMe): $FlowFixMe { return option.value; } + + getPaperTopLevelNameDeprecated(typeAnnotation: $FlowFixMe): $FlowFixMe { + return typeAnnotation.typeParameters.params.length > 1 + ? typeAnnotation.typeParameters.params[1].value + : null; + } } diff --git a/packages/react-native-codegen/src/parsers/typescript/components/events.js b/packages/react-native-codegen/src/parsers/typescript/components/events.js index 0b8045ac32e..87bb91bf023 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/events.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/events.js @@ -202,9 +202,7 @@ function findEventArgumentsAndType( } else if (name === 'BubblingEventHandler' || name === 'DirectEventHandler') { const eventType = name === 'BubblingEventHandler' ? 'bubble' : 'direct'; const paperTopLevelNameDeprecated = - typeAnnotation.typeParameters.params.length > 1 - ? typeAnnotation.typeParameters.params[1].literal.value - : null; + parser.getPaperTopLevelNameDeprecated(typeAnnotation); switch (typeAnnotation.typeParameters.params[0].type) { case parser.nullLiteralTypeAnnotation: diff --git a/packages/react-native-codegen/src/parsers/typescript/parser.js b/packages/react-native-codegen/src/parsers/typescript/parser.js index b1c4745282f..80e08ba1207 100644 --- a/packages/react-native-codegen/src/parsers/typescript/parser.js +++ b/packages/react-native-codegen/src/parsers/typescript/parser.js @@ -561,6 +561,12 @@ class TypeScriptParser implements Parser { getLiteralValue(option: $FlowFixMe): $FlowFixMe { return option.literal.value; } + + getPaperTopLevelNameDeprecated(typeAnnotation: $FlowFixMe): $FlowFixMe { + return typeAnnotation.typeParameters.params.length > 1 + ? typeAnnotation.typeParameters.params[1].literal.value + : null; + } } module.exports = {