Skip to content

Conversation

@kyawthura-gg
Copy link
Contributor

@kyawthura-gg kyawthura-gg commented Mar 16, 2023

Summary

[Codegen 101] The code of getCommandProperties is almost identical in Flow and TS. There are small differences between flow/ts, so we need for it to accept a Parser object. Enrich the parser object with the required methods if necessary.

Changelog:

[Internal] [Changed] - Enrich getCommandProperties with the parser object

Test Plan

yarn test react-native-codegen

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Mar 16, 2023
Copy link
Contributor

@cipolleschi cipolleschi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a very good starting points.
There are a few other improvements we can do:

  1. Flow, line 126 (it doesn't let me add a comment at that line) and Typescript, line 126: that check can go in the Parser object. So the Flow parser can return InterfaceDeclaration and the TS parser can return TSInterfaceDeclaration
  2. Line 134: same as above. Parser can have a bodyProperties function that, given a typeAlias returns body.properties for Flow and body.body for TS.
  3. Line 141: we can add the propertyNames function to the parsers that extract the right array of property names.
  4. After these changes, the two functions should be actually identical and we can move it in a single file in parser-commons.js, for example.

What do you think?

@analysis-bot
Copy link

analysis-bot commented Mar 16, 2023

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 8,519,533 +0
android hermes armeabi-v7a 7,835,743 +0
android hermes x86 8,998,692 +0
android hermes x86_64 8,854,863 +0
android jsc arm64-v8a 9,140,865 +0
android jsc armeabi-v7a 8,333,013 +0
android jsc x86 9,194,556 +0
android jsc x86_64 9,453,649 +0

Base commit: d9f7c4c
Branch: main

@kyawthura-gg
Copy link
Contributor Author

kyawthura-gg commented Mar 17, 2023

Moved propertyName to parser common file.
In Flow, from line 114 to 121 are almost the same as TypeScript. If we like to move those lines, propertyName function will need to accept language param. Is this good idea? Let me know what you think.

@cipolleschi
Copy link
Contributor

Yes and no. If property name has to switch over the language, it should not accept a language parameter, but it should accept a Parser. The parser encapsulates the language specific logic, so the TypeScriptParser knows how to return something for TypeScript and the FlowParser knows how to work with Flow.

Does this make sense?

@kyawthura-gg
Copy link
Contributor Author

I meant these lines. Sorry for confusion.

In Flow,

  let properties;
  try {
    properties = typeAlias.body.properties;
  } catch (e) {
    throw new Error(
      `Failed to find type definition for "${commandTypeName}", please check that you have a valid codegen flow file`,
    );
  }

In TypeScript,

  let properties;
  try {
    properties = typeAlias.body.body;
  } catch (e) {
    throw new Error(
      `Failed to find type definition for "${commandTypeName}", please check that you have a valid codegen typescript file`,
    );
  }

@cipolleschi
Copy link
Contributor

yes, the check should become for both of them:

let properties = parser.bodyProperties(typeAlias);
if (!properties) {
  throw new ....
}

@kyawthura-gg
Copy link
Contributor Author

Added bodyProperties

Copy link
Contributor

@cipolleschi cipolleschi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! It looks good to me!

@facebook-github-bot
Copy link
Contributor

@cipolleschi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@cipolleschi
Copy link
Contributor

/rebase

@facebook-github-bot
Copy link
Contributor

@cipolleschi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Mar 29, 2023
@facebook-github-bot
Copy link
Contributor

@cipolleschi merged this pull request in 969a8d0.

@kyawthura-gg kyawthura-gg deleted the chore/refactor-getCommandProperties branch March 31, 2023 05:51
jeongshin pushed a commit to jeongshin/react-native that referenced this pull request May 7, 2023
Summary:
> [Codegen 101] The code of getCommandProperties is almost identical in [Flow](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/flow/components/index.js#L128) and [TS](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/typescript/components/index.js#L129). There are small differences between flow/ts, so we need for it to accept a Parser object. Enrich the parser object with the required methods if necessary.

## Changelog:

[Internal] [Changed] - Enrich getCommandProperties with the parser object

Pull Request resolved: facebook#36500

Test Plan: `yarn test react-native-codegen`

Reviewed By: cortinico

Differential Revision: D44415265

Pulled By: cipolleschi

fbshipit-source-id: ed13b553a6f782beb0f1aec79bd17d865a96fac9
OlimpiaZurek pushed a commit to OlimpiaZurek/react-native that referenced this pull request May 22, 2023
Summary:
> [Codegen 101] The code of getCommandProperties is almost identical in [Flow](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/flow/components/index.js#L128) and [TS](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/typescript/components/index.js#L129). There are small differences between flow/ts, so we need for it to accept a Parser object. Enrich the parser object with the required methods if necessary.

## Changelog:

[Internal] [Changed] - Enrich getCommandProperties with the parser object

Pull Request resolved: facebook#36500

Test Plan: `yarn test react-native-codegen`

Reviewed By: cortinico

Differential Revision: D44415265

Pulled By: cipolleschi

fbshipit-source-id: ed13b553a6f782beb0f1aec79bd17d865a96fac9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants