Skip to content

Commit 8bcfc49

Browse files
siddarthkayfacebook-github-bot
authored andcommitted
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 #34872 ## Changelog: [Internal] [Changed] - Add `getPaperTopLevelNameDeprecated` to parser base class and update usages. Pull Request resolved: #38683 Test Plan: Run `yarn jest react-native-codegen` locally and ensure CI is green ## Screenshot of test passing locally: <img width="1060" alt="Screenshot 2023-07-30 at 10 04 24 AM" src="https://github.com/facebook/react-native/assets/64726664/3f61377b-9f44-45e8-bece-d4fd6bcc4567"> Reviewed By: cipolleschi Differential Revision: D47902816 Pulled By: rshest fbshipit-source-id: 6fab53e02cfc3f0aaa3ffd795c3fe1d2f723e060
1 parent 1ba7bc0 commit 8bcfc49

File tree

6 files changed

+27
-6
lines changed

6 files changed

+27
-6
lines changed

packages/react-native-codegen/src/parsers/flow/components/events.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,7 @@ function findEventArgumentsAndType(
193193
} else if (name === 'BubblingEventHandler' || name === 'DirectEventHandler') {
194194
const eventType = name === 'BubblingEventHandler' ? 'bubble' : 'direct';
195195
const paperTopLevelNameDeprecated =
196-
typeAnnotation.typeParameters.params.length > 1
197-
? typeAnnotation.typeParameters.params[1].value
198-
: null;
196+
parser.getPaperTopLevelNameDeprecated(typeAnnotation);
199197
if (
200198
typeAnnotation.typeParameters.params[0].type ===
201199
parser.nullLiteralTypeAnnotation

packages/react-native-codegen/src/parsers/flow/parser.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,12 @@ class FlowParser implements Parser {
547547
getLiteralValue(option: $FlowFixMe): $FlowFixMe {
548548
return option.value;
549549
}
550+
551+
getPaperTopLevelNameDeprecated(typeAnnotation: $FlowFixMe): $FlowFixMe {
552+
return typeAnnotation.typeParameters.params.length > 1
553+
? typeAnnotation.typeParameters.params[1].value
554+
: null;
555+
}
550556
}
551557

552558
module.exports = {

packages/react-native-codegen/src/parsers/parser.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,4 +421,11 @@ export interface Parser {
421421
* @returns: the literal value of an union represented.
422422
*/
423423
getLiteralValue(option: $FlowFixMe): $FlowFixMe;
424+
425+
/**
426+
* Given a type annotation, it returns top level name in the AST if it exists else returns null.
427+
* @parameter typeAnnotation: the annotation for a type in the AST.
428+
* @returns: the top level name properties in the AST if it exists else null.
429+
*/
430+
getPaperTopLevelNameDeprecated(typeAnnotation: $FlowFixMe): $FlowFixMe;
424431
}

packages/react-native-codegen/src/parsers/parserMock.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,4 +486,10 @@ export class MockedParser implements Parser {
486486
getLiteralValue(option: $FlowFixMe): $FlowFixMe {
487487
return option.value;
488488
}
489+
490+
getPaperTopLevelNameDeprecated(typeAnnotation: $FlowFixMe): $FlowFixMe {
491+
return typeAnnotation.typeParameters.params.length > 1
492+
? typeAnnotation.typeParameters.params[1].value
493+
: null;
494+
}
489495
}

packages/react-native-codegen/src/parsers/typescript/components/events.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,7 @@ function findEventArgumentsAndType(
202202
} else if (name === 'BubblingEventHandler' || name === 'DirectEventHandler') {
203203
const eventType = name === 'BubblingEventHandler' ? 'bubble' : 'direct';
204204
const paperTopLevelNameDeprecated =
205-
typeAnnotation.typeParameters.params.length > 1
206-
? typeAnnotation.typeParameters.params[1].literal.value
207-
: null;
205+
parser.getPaperTopLevelNameDeprecated(typeAnnotation);
208206

209207
switch (typeAnnotation.typeParameters.params[0].type) {
210208
case parser.nullLiteralTypeAnnotation:

packages/react-native-codegen/src/parsers/typescript/parser.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,12 @@ class TypeScriptParser implements Parser {
561561
getLiteralValue(option: $FlowFixMe): $FlowFixMe {
562562
return option.literal.value;
563563
}
564+
565+
getPaperTopLevelNameDeprecated(typeAnnotation: $FlowFixMe): $FlowFixMe {
566+
return typeAnnotation.typeParameters.params.length > 1
567+
? typeAnnotation.typeParameters.params[1].literal.value
568+
: null;
569+
}
564570
}
565571

566572
module.exports = {

0 commit comments

Comments
 (0)