Commit Graph

1111 Commits

Author SHA1 Message Date
Ruslan Shestopalyuk 96fb708d3e Make enums to work as part of data structures for C++ TurboModules codegen (#36155)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/36155

[Changelog][Internal]

The PR  https://github.com/facebook/react-native/pull/36030 (diff D42884147 (https://github.com/facebook/react-native/commit/ceb1d0dea694739f357d86296b94f5834e5ee7f7)) added support for enum types in JS to C++ bridging in C++ TurboModules.

This only worked for enums as argument types for exposed methods, but not for the cases when enums are members of complex data structures that are also exposed through a codegen.

This diff fixes this problem, so that codegen now correctly works both with enum types as method arguments, but also as data structure members.

Some part of the change is the same as D42008724 (https://github.com/facebook/react-native/commit/963e45afd1c69771d1d26df8282774c948f762e3), but there are also some changes related to the types, that were required.

Reviewed By: christophpurrer

Differential Revision: D43292254

fbshipit-source-id: b2d6cf4a2d4d233b8cc403ecd02b5be16d5d91a7
2023-02-15 06:03:12 -08:00
Vitali Zaidman ceb1d0dea6 Generate enum types that would be allowed to be used as well as string/number in c++ turbo modules generators (#36030)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/36030

Generate enum types in c++ turbo modules.

For enums in the ts schema file such as:
```
export enum NumEnum {
  ONE = 1,
  TWO = 2,
}
```
This would export enums and the relevant Bridging to js and from js code to the spec H files such as:
```
#pragma mark - SampleTurboModuleCxxNumEnum

enum SampleTurboModuleCxxNumEnum { ONE, TWO };

template <>
struct Bridging<SampleTurboModuleCxxNumEnum> {
  static SampleTurboModuleCxxNumEnum fromJs(jsi::Runtime &rt, int32_t value) {

    if (value == 1) {
      return SampleTurboModuleCxxNumEnum::ONE;
    } else if (value == 2) {
      return SampleTurboModuleCxxNumEnum::TWO;
    } else {
      throw jsi::JSError(rt, "No appropriate enum member found for value");
    }
  }

  static jsi::Value toJs(jsi::Runtime &rt, SampleTurboModuleCxxNumEnum value) {
    if (value == SampleTurboModuleCxxNumEnum::ONE) {
      return bridging::toJs(rt, 1);
    } else if (value == SampleTurboModuleCxxNumEnum::TWO) {
      return bridging::toJs(rt, 2);
    } else {
      throw jsi::JSError(rt, "No appropriate enum member found for enum value");
    }
  }
};

```
That code would allow us to use these enums in the cxx files like this:
```
  NativeCxxModuleExampleCxxEnumInt getNumEnum(
      jsi::Runtime &rt,
      NativeCxxModuleExampleCxxEnumInt arg);
```

Changelog: [General] [Added] Generate enum types that would be allowed to be used as well as string/number in c++ turbo modules generators

Reviewed By: christophpurrer

Differential Revision: D42884147

fbshipit-source-id: d34d1fc7ba268b570821dc108444196f69a431b2
2023-02-13 15:09:44 -08:00
Zihan Chen (MSFT) be3845adec Add minimum necessary .d.ts files to react-native-codegen (#36102)
Summary:
Add minimum necessary .d.ts files to react-native-codegen.

I found .d.ts files will be copied to `lib` so I guess no additional script is needed.

## Changelog

[GENERAL] [CHANGED] - Add minimum necessary .d.ts files to react-native-codegen

Pull Request resolved: https://github.com/facebook/react-native/pull/36102

Test Plan: `npm run build` in `packages/react-native-codegen` and see all .d.ts files appear in `lib`.

Reviewed By: cortinico

Differential Revision: D43157233

Pulled By: cipolleschi

fbshipit-source-id: 6f122f0f4cda693ba22af6dd534e9d34d069ecac
2023-02-13 04:10:20 -08:00
Zihan Chen (MSFT) bbed15d4ae Turbo Module supports intersection type for TypeScript (#36037)
Summary:
As [requested from community](https://github.com/reactwg/react-native-new-architecture/discussions/91#discussioncomment-4282384), intersection type is supported for component but no in turbo module. This PR fixed it.

## Changelog

[GENERAL] [CHANGED] - Turbo Module supports intersection type for TypeScript

Pull Request resolved: https://github.com/facebook/react-native/pull/36037

Test Plan: `yarn jest react-native-codegen` passed

Reviewed By: christophpurrer

Differential Revision: D42960338

Pulled By: cipolleschi

fbshipit-source-id: d317d3155cb96643bf549d0ac3d77f503685edbc
2023-02-07 07:41:37 -08:00
Vitali Zaidman 39bdb34178 Parse and collect enum members in turbo module specs (#36044)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/36044

> **Notice**: This commit does not change any behaviour.

Parse and collect enum members in module specs instead of the current behaviour of parsing enum members as 'string' / 'number' and ignoring the member names.

This commit would allow us to generate native Enums corresponding to the schema's enums.

Prior to this commit, enum params were parsed as:
```
                {
                  'name': 'qualityParam',
                  'optional': false,
                  'typeAnnotation': {
                    'type': 'EnumDeclaration',
                    'memberType': 'StringTypeAnnotation'
                  }
                },
```

The name of the enum type and the members of the enum type were ignored.

After this commit, parsed modules would hold a new object member called `enumMap` that would look like this:
```

      'enumMap': {
        'QualityEnum': {
          'name': 'QualityEnum',
          'type': 'EnumDeclarationWithMembers',
          'memberType': 'StringTypeAnnotation',
          'members': [
            {
              'name': 'SD',
              'value': 'sd'
            },
            {
              'name': 'HD',
              'value': 'hd'
            }
          ]
        },
        // ...
      }
```
And enum params would be exported as:
```
	        {
                  'name': 'qualityParam',
                  'optional': false,
                  'typeAnnotation': {
                    'name': 'NativeModuleEnumTypeAnnotation',
                    'type': 'EnumDeclaration',
                    'memberType': 'StringTypeAnnotation'
                  }
                },
```

Combining the two new outputs would allow us to generate Native enums in the Native generators.

Changelog: [Internal] Parse and collect enum members in turbo module specs

Reviewed By: cipolleschi

Differential Revision: D42778258

fbshipit-source-id: 56479342e085bc4e13c5a3e12b265b140e49893c
2023-02-03 03:03:41 -08:00
Vitali Zaidman 53c9786b71 remove 'EnumDeclaration' as a type expected to throw error in module generators
Summary:
Handling of `EnumDeclaration` was introduced in D38967241 (https://github.com/facebook/react-native/commit/745f3ee8c571560406629bc7af3cf4914ef1b211) so it is no longer a type expected to fail generators.

Changelog: [Internal] remove 'EnumDeclaration' as a type expected to throw error in module generators

Reviewed By: cipolleschi

Differential Revision: D42917947

fbshipit-source-id: 16fcb915ccd42c613ca4d30b815d6365681f5fa1
2023-02-03 03:03:41 -08:00
Vitali Zaidman 209d019055 Added an e2e test fixture for Enums and .H+.C TM e2e snapshot tests
Summary:
* Added an e2e test fixture with all the supported enum variations
* Added H and C file TM generators to the TM e2e tests

Changelog: [Internal] Added an e2e test fixture for Enums and .H+.C TM e2e snapshot tests

Reviewed By: cipolleschi

Differential Revision: D42917330

fbshipit-source-id: 8e4adb86b6eb4e2b29a9e6d0cd6e4fd5b002ad1a
2023-02-03 03:03:41 -08:00
Vitali Zaidman 69494a7b1f rename module exports from "aliases" to "aliasMap" (#36042)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/36042

Pull Request resolved: https://github.com/facebook/react-native/pull/36028

Use aliasMap both in parsers and generators instead of using aliasMap in half of the places and aliases in the other.

This would also allow us to introduce "enumMap" more easily in the next commit.

Changelog: [Internal] rename module exports from "aliases" to "aliasMap"

Reviewed By: christophpurrer, cipolleschi

Differential Revision: D42888752

fbshipit-source-id: cf1929fcebde994d07e5c6bda5ab71106d417b21
2023-02-02 09:58:01 -08:00
Zihan Chen (MSFT) bf34810c5c Turbo module codegen support interface with inheritance in module (#36011)
Summary:
The [previous pull request](https://github.com/facebook/react-native/pull/35812) enables defining interfaces and using them in a turbo module, but all members are flattened, this is not the best option for codegen for object oriented languages.

In this pull request, an optional member `baseTypes` is added to aliased objects. Members are still flattened for backward compatibility, if a codegen doesn't care about that nothing needs to be changed.

### Example

```typescript
interface A {
  a : string;
}

interface B extends A {
  b : number;
}
```

#### Before the previous pull request

`interface` is not allowed here, you must use type alias.

#### At the previous pull request

`interface` is allowed, but it is translated to

```typescript
type A = {a : string};
type B = {a : string, b : number};
```

#### At this pull request

Extra information is provided so that you know `B` extends `A`. By comparing `B` to `A` it is easy to know that `B::a` is obtained from `A`.

## Changelog

[GENERAL] [CHANGED] - Turbo module codegen support interface with inheritance in module

Pull Request resolved: https://github.com/facebook/react-native/pull/36011

Test Plan: `yarn jest react-native-codegen` passed

Reviewed By: rshest

Differential Revision: D42882650

Pulled By: cipolleschi

fbshipit-source-id: 32d502e8a99c4151fae0a1f4dfa60db9ac827963
2023-01-31 12:05:46 -08:00
Vitali Zaidman 112c89e810 refactor: Renamed emitPartial to emitObject (#35982)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/35982

This reflects better what the former `emitPartial` was doing - emitting an object with set props.

Changelog: [Internal] - Renamed emitPartial to emitObject

Reviewed By: christophpurrer

Differential Revision: D42740958

fbshipit-source-id: 4f974c6911bc129e698323a8039bbd7aa7602461
2023-01-30 06:34:32 -08:00
Vitali Zaidman 84c1547526 refactor: Renamed emitObject to emitGenericObject (#35981)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/35981

This reflects better what the former `emitObject` was doing - emitting a generic object.

It would also allow us to rename `emitPartial` to `emitObject` in the next diff whence the function name `emitObject` will be free.

Changelog: [Internal] - Renamed emitObject to emitGenericObject

Reviewed By: christophpurrer

Differential Revision: D42740871

fbshipit-source-id: 1b9112464695b8abeccc95eed908b0b45a0e3bf2
2023-01-30 06:34:32 -08:00
Ruslan Shestopalyuk a00cea46bf Add missing C++ include for prop conversion of complex array type (#35984)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/35984

[Changelog][Internal]

Codegen for props parsing was failing to add a required include for the case when the type is an array of objects, which in turn use non-trivial types.

Something like:
```
export type NativeProps = $ReadOnly<{
  ...ViewProps,
  bounds: $ReadOnlyArray<
    $ReadOnly<{
      height?: Float,
      left?: Float,
      top?: Float,
      width?: Float,
    }>,
  >,
}>;
```

would cause compilation errors on C++ side, since the required header for the `Float` conversion wasn't included.

Reviewed By: cipolleschi

Differential Revision: D42781128

fbshipit-source-id: d5b133b931a60e414761db0b3ed09893d3fcc9aa
2023-01-27 05:19:17 -08:00
Genki Kondo d3cc48d9a6 Add codegen support for DimensionValue for components (#35953)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/35953

DimensionValue is a reserved prop type that can be a number or string (such as '50%'). On Java, it will get converted to a YogaValue (converter added to this diff); on C++ it will get converted to a YGValue (converter already exists as it's used in Fabric).

Changelog:
[Internal][Added] - Add codegen support for DimensionValue for components

Reviewed By: cipolleschi

Differential Revision: D42650799

fbshipit-source-id: 1d2bc30bbd93837dedbbb4c74f814963c8140957
2023-01-26 18:52:10 -08:00
Vitali Zaidman 97e707d897 simple support to the Partial<T> annotation (#35961)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/35961

Pull Request resolved: https://github.com/facebook/react-native/pull/35960

This fixes #35864

This feature allows using `$Partial<Obj>` in flow and `Partial<Obj>` in TypeScript based on the spec mentioned here: https://flow.org/en/docs/types/utilities/#toc-partial.

We currently only allow passing an Obj to Partial so
```
export type SomeObj = {
  a: string,
  b?: boolean,
};

export type PartialSomeObj = Partial<SomeObj>;
```
should work.
and also-
```
export type PartialSomeObj = Partial<{
  a: string,
  b?: boolean,
}>;
```
But not
```
export type PartialSomeObj = Partial<Partial<{
  a: string,
  b?: boolean,
}>>;
```
This can be improved in the future by a recursive unwrapping of the value inside the `Partial` annotation.

Changelog:
[General] [Added] -  Allow the use of "Partial<T>" in Turbo Module specs.

Reviewed By: christophpurrer, cipolleschi

Differential Revision: D42640880

fbshipit-source-id: 03a3fccc38ccfc7a5440fe11893beb68e77753f3
2023-01-26 12:30:38 -08:00
MaeIg 462815001b Extract parseString and parseModuleFixture functions in typescript and flow parsers (#35928)
Summary:
This PR aims to extract parseString and parseModuleFixture functions into the typescript and flow parsers. This task was proposed in https://github.com/facebook/react-native/issues/35158 and helps https://github.com/facebook/react-native/issues/34872.

## Changelog

<!-- 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
-->
[Internal] [Changed] - Extract parseString and parseModuleFixture functions in typescript and flow parsers

Pull Request resolved: https://github.com/facebook/react-native/pull/35928

Test Plan:
yarn test:
<img width="386" alt="image" src="https://user-images.githubusercontent.com/40902940/213889984-f0cadaff-4472-42d6-b55b-4901023aad1e.png">

yarn flow:
<img width="166" alt="image" src="https://user-images.githubusercontent.com/40902940/213889974-21ac2483-2731-4cb1-a2b5-195d98619649.png">

yarn lint:
<img width="514" alt="image" src="https://user-images.githubusercontent.com/40902940/213889980-090af354-346f-4a9c-90bc-7006899f0819.png">

Reviewed By: jacdebug

Differential Revision: D42673866

Pulled By: cipolleschi

fbshipit-source-id: f1b5f8a7b3944e7e8223b25c0fb9bf4e8b512aa7
2023-01-25 12:38:52 -08:00
Genki Kondo 04cf92fa9e Fix codegen for Array<EdgeInsetsValue>
Summary:
Changelog:
[Internal][Added] - Add support for props of type Array<EdgeInsetsValue>

Reviewed By: christophpurrer

Differential Revision: D42651078

fbshipit-source-id: 3b8683ab199c3d590136cec0e6a67e9e85aaa2c0
2023-01-23 11:06:09 -08:00
Mikhail Mikhaylov 1bab3e24b8 Removes duplicate DoubleTypeAnnotation label (#35920)
Summary:
Removes duplicate `DoubleTypeAnnotation` label since it is already presented in the same `switch` statement on https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/generators/components/GeneratePropsH.js#L658

## Changelog

<!-- 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
-->
[GENERAL] [FIXED] - Removes duplicate DoubleTypeAnnotation label

Pull Request resolved: https://github.com/facebook/react-native/pull/35920

Test Plan: No tests needed

Reviewed By: cortinico

Differential Revision: D42673897

Pulled By: jacdebug

fbshipit-source-id: d4603364a26bda83990b65f76b93781a7152f1cb
2023-01-23 05:10:41 -08:00
Vicary A a69a924d51 fix(deps): expose yarn peer dependencies (#35915)
Summary:
Fixes https://github.com/facebook/react-native/issues/35913

## CHANGELOG

[General] [Fixed] - Peer dependency warnings for Yarn 3

Pull Request resolved: https://github.com/facebook/react-native/pull/35915

Test Plan:
```bash
yarn set version berry
yarn add react-native-changelog
```

Reviewed By: cortinico

Differential Revision: D42674105

Pulled By: jacdebug

fbshipit-source-id: 4270512b1b4857765a183389981b797cfecefcbd
2023-01-23 04:25:32 -08:00
Sam Zhou f4072b1e00 Pre-suppress errors ahead of 0.197.0 release
Summary: Changelog: [internal]

Reviewed By: mroch

Differential Revision: D42558696

fbshipit-source-id: 3367fb1233c9630bd31b0ae9950f7b2f13438057
2023-01-17 22:24:49 -08:00
Zihan Chen (MSFT) 8befb740d6 Turbo module codegen support interface like alias in module (#35812)
Summary:
Turbo module codegen support interface like alias in module.

In typescript, interface allows inheritance, but the codegen schema doesn't have such information. In this PR the codegen schema is not changed, interfaces are flattened.

In the next change, I will update the codegen schema so that it allows inheritance, in order to generate more precise C++ code.

## Changelog

[GENERAL] [CHANGED] - Turbo module codegen support interface like alias in module

Pull Request resolved: https://github.com/facebook/react-native/pull/35812

Test Plan: `yarn jest react-native-codegen` passed

Reviewed By: cortinico

Differential Revision: D42475368

Pulled By: cipolleschi

fbshipit-source-id: 26025f8a55b600dfea27026c998cd8b5fe752af4
2023-01-12 08:04:40 -08:00
MaeIg dc4d73c954 Extract the buildSchema function in the parsers-commons.js (#35158)
Summary:
This PR aims to extract  the buildSchema function into parsers-commons that is shared between typescript and flow. It is a task of https://github.com/facebook/react-native/issues/34872:
> Extract the buildSchema function ([Flow](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/flow/index.js#L66), [TypeScript](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/typescript/index.js#L72)) in the parsers-commons.js file to a top level buildSchema function which takes additional parameters to properly parse the content, get the config type and to build the schema, based on the language used.

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->
[Internal] [Changed] - Extract the buildSchema function in the parsers-commons.js

Pull Request resolved: https://github.com/facebook/react-native/pull/35158

Test Plan:
yarn test:
<img width="380" alt="image" src="https://user-images.githubusercontent.com/40902940/209584411-40f66047-e25d-43d4-975d-af10cd202f24.png">

yarn flow:
<img width="150" alt="image" src="https://user-images.githubusercontent.com/40902940/209584423-4cf2cb5a-a300-40a6-962c-e57934f19ad2.png">

yarn lint:
<img width="510" alt="image" src="https://user-images.githubusercontent.com/40902940/209584440-2d2b2658-73d8-47e2-bb8c-64d4633369a2.png">

Reviewed By: cortinico

Differential Revision: D42386804

Pulled By: cipolleschi

fbshipit-source-id: 2a238f7cec982d8ef3fd57a34dc9f58171e32b53
2023-01-12 04:23:42 -08:00
Luna Wei fdc2836305 Fix processColor ESM for codegen
Summary: Changelog: [Internal] Fix broken usage of `processColor`

Reviewed By: GijsWeterings

Differential Revision: D42346452

fbshipit-source-id: 20be00210f5b68c15292c8a8c1cd918b23fb1fd6
2023-01-04 13:58:40 -08:00
Antoine Doubovetzky 29a0791658 Move language ternaries logic to FlowParser and TypeScriptParser (#35742)
Summary:
This is not a task from https://github.com/facebook/react-native/issues/34872 but it goes towards the same goal: removing language-specific logic from shared code and moving it to the FlowParser and TypeScriptParser.

I'm not sure about the documentation of the functions. It's been quite difficult for me to understand what the arguments are and what is returned by the functions because I find the naming quite confusing and nothing is typed.

## Changelog

<!-- 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
-->

[INTERNAL] [CHANGED] - [Codegen] Move language ternaries logic to FlowParser and TypeScriptParser

Pull Request resolved: https://github.com/facebook/react-native/pull/35742

Test Plan: I tested using Flow and Jest commands. I made sure that tests were broken when Flow and TypeScript implementations were reversed.

Reviewed By: NickGerleman

Differential Revision: D42280934

Pulled By: rshest

fbshipit-source-id: 580ebdf424a5c179c9928ac5f9441899fb04d0c7
2022-12-30 00:55:35 -08:00
Ruslan Shestopalyuk 988a23169a Make generated EventEmitter C++ code not cause compiler warnings
Summary:
Changelog: [Internal]

While working on implementing [Event Timing API](https://www.w3.org/TR/event-timing/) I've noticed that there are multiple compiler warnings about unused lambda captures, which are coming from generated C++ code for EventEmitters.

This modifies the codegen so that the corresponding lambda doesn't capture event variable if it's not used in the event handler, thus getting rid of warnings.

Reviewed By: christophpurrer

Differential Revision: D42281899

fbshipit-source-id: 98442bb9f3ce374755188d818a9b2d6a8050bf15
2022-12-29 14:28:04 -08:00
Antoine Doubovetzky f16348ca03 Remove unused language argument in codegen errors (#35732)
Summary:
This is not a task from https://github.com/facebook/react-native/issues/34872 but I noticed that we were passing `language` arguments that were never used for several errors so I removed them.

## Changelog

<!-- 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
-->

[INTERNAL] [CHANGED] - Remove unused language argument in Codegen errors

Pull Request resolved: https://github.com/facebook/react-native/pull/35732

Test Plan: I tested using Flow and Jest.

Reviewed By: christophpurrer

Differential Revision: D42266490

Pulled By: rshest

fbshipit-source-id: 7953a98586bf9e927a58222cc27cf88e9c1c1163
2022-12-28 08:46:19 -08:00
Christoph Purrer c7e1e00b82 Make C++ struct generator type-safe (#35656)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/35656

Changelog: [Internal]

## Change:

https://github.com/facebook/react-native/pull/35265 added a struct generator - but it is not type safe :-(

E.g. you can write a TM Spec type as:

```
export type CustomType = {
   key: string;
   enabled: boolean;
   time?: number;
 }
```
And a C++ type as:
```
using CustomType = NativeSampleModuleBaseCustomType<float, bool, std::optional<int32_t>>;
template <>
struct Bridging<CustomType>
    : NativeSampleModuleBaseCustomTypeBridging<float, bool, std::optional<int32_t>> {};
```
and it will still compile :-( - which should not.

The reason is that the generated structs don't validate the members type :-(
```
template <typename P0, typename P1, typename P2>
struct NativeSampleModuleBaseCustomType {
  P0 key;
  P1 enabled;
  P2 time;
  bool operator==(const NativeSampleModuleBaseCustomType &other) const {
    return key == other.key && enabled == other.enabled && time == other.time;
  }
};

template <typename P0, typename P1, typename P2>
struct NativeSampleModuleBaseCustomTypeBridging {
  static NativeSampleModuleBaseCustomType<P0, P1, P2> fromJs(
      jsi::Runtime &rt,
      const jsi::Object &value,
      const std::shared_ptr<CallInvoker> &jsInvoker) {
    NativeSampleModuleBaseCustomType<P0, P1, P2> result{
      bridging::fromJs<P0>(rt, value.getProperty(rt, "key"), jsInvoker),
      bridging::fromJs<P1>(rt, value.getProperty(rt, "enabled"), jsInvoker),
      bridging::fromJs<P2>(rt, value.getProperty(rt, "time"), jsInvoker)};
    return result;
  }

  static jsi::Object toJs(
      jsi::Runtime &rt,
      const NativeSampleModuleBaseCustomType<P0, P1, P2> &value) {
    auto result = facebook::jsi::Object(rt);
    result.setProperty(rt, "key", bridging::toJs(rt, value.key));
    result.setProperty(rt, "enabled", bridging::toJs(rt, value.enabled));
    if (value.time) {
      result.setProperty(rt, "time", bridging::toJs(rt, value.time.value()));
    }
    keyToJs(rt, value.key);
    return result;
  }
};
```

This fixes that, by simply emitting conversion functions for each member such as
```
#ifdef DEBUG
  static bool keyToJs(jsi::Runtime &rt, P0 value) {
    return bridging::toJs(rt, value);
  }
  static double enabledToJs(jsi::Runtime &rt, P1 value) {
    return bridging::toJs(rt, value);
  }
  static jsi::String timeToJs(jsi::Runtime &rt, P2 value) {
    return bridging::toJs(rt, value);
  }
#endif
```

Reviewed By: cipolleschi

Differential Revision: D42082423

fbshipit-source-id: 5133f14e2aa8351e9bbbf614117a3d5894b17fa6
2022-12-16 04:26:43 -08:00
Ruslan Shestopalyuk 963e45afd1 Recursively pass invoker into ::toJS data structures for generated C++ modules
Summary:
While working on D42008409 I found out that codegen for pure C++ modules doesn't work with container types that are nested inside generated data structures, which happens because they don't have a specialization of `bridging::toJS` that wouldn't pass the `invoker` instance through.

It looks like an easiest option would be just to use `invoker` in codegen for `toJS` as well, which this diff does.

Note that I also experimented with removing `invoker` from being used in the `::toJS` specializations for containers altogether (see D42008410), as there doesn't seem to be a single use case when `invoker` would be ever needed in any `::toJS` specialization (and imagining such a scenario would be a stretch, tbh - why a conversion function would invoke anything running on JS side, given that invoker provides no return values anyway?..)

But since I am still not 100% about the invoker purpose there, I went with the codegen change.

Changelog: [Internal]

Reviewed By: christophpurrer

Differential Revision: D42008724

fbshipit-source-id: 6302d3ceacdfc8fed296ee1ef1a985f7273c2261
2022-12-13 17:07:13 -08:00
MaeIg 3f2691cf84 Extract the parseFile function in the typescript and flow parsers (#35318)
Summary:
This PR aims to extract  the parseFile function in the typescript and flow parsers.  This is to solve the problem described [here](https://github.com/facebook/react-native/pull/35158#issuecomment-1298330753) and help with the work done in https://github.com/facebook/react-native/issues/34872.

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->
[Internal] [Changed] - Extract the parseFile function in the typescript and flow parsers

Pull Request resolved: https://github.com/facebook/react-native/pull/35318

Test Plan:
yarn flow:
<img width="496" alt="image" src="https://user-images.githubusercontent.com/40902940/206518024-83084c3d-ab0d-4a04-810a-d40270add4b0.png">

yarn lint:
<img width="495" alt="image" src="https://user-images.githubusercontent.com/40902940/206518076-9e07eafe-db61-4c6e-8aaa-f92f190cf4f3.png">

yarn test:
<img width="389" alt="image" src="https://user-images.githubusercontent.com/40902940/206518118-5633b28c-b79b-4421-80f7-de1e03fb8ff2.png">

Reviewed By: cortinico

Differential Revision: D41248581

Pulled By: cipolleschi

fbshipit-source-id: f5b878a28a7de612fcdd1528f064b44f668503af
2022-12-13 09:00:46 -08:00
Zihan Chen (MSFT) f07490b1f1 Fix codegen output for object with indexer (#35344)
Summary:
The current implementation think `{[key:T]:U}` and `{key:object}` are the same type, which is semantically wrong.

This pull request fixes the problem and return `{type:'GenericObjectTypeAnnotation'}`, so that `{[key:T]:U}` is `Object`. The current schema cannot represent dictionary type, `Object` is the closest one.

The previous incorrect implementation actually bring in code with logic that doesn't make sense (treating indexer as property), those and related unit test are all undone.

## Changelog

[General] [Changed] - Fix codegen output for object with indexer

Pull Request resolved: https://github.com/facebook/react-native/pull/35344

Test Plan: `yarn jest react-native-codegen` passed

Reviewed By: rshest

Differential Revision: D41304475

Pulled By: cipolleschi

fbshipit-source-id: caab8e458d83f9850c5c28b67cc561a764738372
2022-12-13 03:21:02 -08:00
Christoph Purrer e327f9e20c BUCK > Enable ObjectiveC TM for MacOS (#35609)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/35609

Changelog: [Internal]

Reviewed By: shwanton, cortinico

Differential Revision: D41893371

fbshipit-source-id: c60ca66d6c3957dd7850a14a1394a6b5227ff415
2022-12-12 03:23:45 -08:00
Nicola Corti 177f30a323 Bump all the @react-native/ packages to publish on main
Summary:
Nightlies are currently broken on main. That's because nightlies rely on packages that got
re-scoped under `react-native`. We need to publish them to NPM.
In order to do so, I'm bumping versions for the one that have changes on main so that they
can be published to NPM to unblock nightlies.

Changelog:
[Internal] [Changed] - Bump all the react-native/ packages to publish on main

Reviewed By: hoxyq

Differential Revision: D41840985

fbshipit-source-id: 45b691611e33668df0922d4ff753738a773f162c
2022-12-09 02:10:17 -08:00
Pieter De Baets 90538909f9 Emit name constant as part of Java codegen
Summary:
We have the expected module name available as part of the codegen schema, so we can remove the need for developers to implement the `getName` method as part of their module implementation.

Note that this method is not actually used when the TurboModules infra is used, as the moduleName from the turbo module manager is passed through to the TurboModule base class instead. Moving the method to codegen will make it easier to remove this method altogether once the old architecture is fully removed.

Changelog: [Android][Added] Support generating `getName` in react-native-codegen for Java TurboModules

Reviewed By: mdvacca

Differential Revision: D41615387

fbshipit-source-id: 6b117645fa39e5e9ab014b21198496a52f6f2ae2
2022-12-07 06:35:01 -08:00
Mike Vitousek 78b599f5b6 Add type arguments to calls to new Array() in xplat
Summary:
This diff adds type parameters to all uses of `new Array`.

Changelog: [internal]

Reviewed By: SamChou19815

Differential Revision: D41746116

fbshipit-source-id: 8aa2777dd13ef4cd9f8613adaa3509d3573d4446
2022-12-06 22:46:38 -08:00
Sam Zhou ccefad049a Enable LTI in react-native
Summary: Changelog: [Internal]

Reviewed By: panagosg7

Differential Revision: D41788271

fbshipit-source-id: 8e40dc3279ee0283b2845b9559a80862fdf59a17
2022-12-06 19:34:14 -08:00
Sam Zhou 17f221c852 Add annotations to xplat to prepare for LTI
Summary: Changelog: [Internal]

Reviewed By: mvitousek

Differential Revision: D41739285

fbshipit-source-id: 79db8afaad87a2fbba8f6d30d8468a5997c8509d
2022-12-05 14:58:24 -08:00
Nicola Corti 7933dd78da Remove .mk prebuilt file and .mk file generation from codegen (#35540)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/35540

We now don't need to generate .mk files anymore, therefore I'm removing
this logic from the codegen. In RN 0.72 users should be fully migrated
to CMake.

Changelog:
[Android] [Removed] - Remove .mk prebuilt file and .mk file generation from codegen

Reviewed By: rshest

Differential Revision: D41654122

fbshipit-source-id: 3a3c01fa8ab4d48460338e1a9ce2ecbd6df25f47
2022-12-05 03:27:37 -08:00
Gabriel Donadel Dall'Agnol ea9e78d426 feat: Extract case Array and ReadOnlyArray into a single emitArrayType function (#35546)
Summary:
This PR extracts the content of the codegen case `'Array'` and `'ReadOnlyArray'` into a single `emitArrayType` function inside the `parsers-primitives.js` file and uses it in both Flow and TypeScript parsers as requested on https://github.com/facebook/react-native/issues/34872. This also adds unit tests to the new `emitArrayType` function.

## Changelog

[Internal] [Changed]  - Extract the content of the case 'Array' and 'ReadOnlyArray'  into a single emitArrayType function

Pull Request resolved: https://github.com/facebook/react-native/pull/35546

Test Plan:
Run `yarn jest react-native-codegen` and ensure CI is green

![image](https://user-images.githubusercontent.com/11707729/205375599-3bbf02bf-85b5-41e6-ae77-fc6e12bee538.png)

Reviewed By: christophpurrer

Differential Revision: D41700084

Pulled By: rshest

fbshipit-source-id: 4bfd2d3ab3f1343bb401ba9c278dc0e0e1ea0989
2022-12-03 14:18:46 -08:00
Sam Zhou 3905fd4bd7 Annotate implicit instantiation in xplat
Summary: Changelog: [Internal]

Reviewed By: jbrown215

Differential Revision: D41662414

fbshipit-source-id: 763cde4ecc19ae2407cd9cf13557248544d2e0d1
2022-12-02 12:26:04 -08:00
MaeIg ee8701e13e Move emitUnionTypeAnnotation into parsers-primitives (#35502)
Summary:
This PR aims to move  the emitUnionTypeAnnotation function into parsers-primitives.
It was extracted to parsers-commons in a task of https://github.com/facebook/react-native/issues/34872. But, all other emit* functions are in this file, so I think it was a mistake.

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->
[Internal] [Changed] - Move emitUnionTypeAnnotation function into parsers-primitives

Pull Request resolved: https://github.com/facebook/react-native/pull/35502

Test Plan:
yarn flow:
<img width="506" alt="image" src="https://user-images.githubusercontent.com/40902940/204494702-4a190d29-38bb-4da3-beb1-236f685ce671.png">

yarn lint:
<img width="261" alt="image" src="https://user-images.githubusercontent.com/40902940/204494798-3dfb0a8a-8535-455c-9483-ec17a7d8a710.png">

yarn test:
<img width="381" alt="image" src="https://user-images.githubusercontent.com/40902940/204494766-27fdaccc-f90d-4b77-b53b-2ff0d720c74e.png">

Reviewed By: rshest

Differential Revision: D41577409

Pulled By: cortinico

fbshipit-source-id: c37b85e8a53c2b1c2bb0e00bc772dd615d1269dd
2022-12-01 09:23:45 -08:00
Rob Hogan 3e19c97646 Bump remaining build-time Babel deps
Summary:
Update `babel/*` dependencies specifying `^7.x.y` where `x > 0` to the latest available semver minor, and corresponding superficial snapshot updates reflecting a small decrease in JS bundle size.

 - `babel/core` to `^7.20.0`
 - `babel/parser` to `^7.20.0`
 - `babel/preset-env` to `^7.20.0`
 - `babel/traverse` to `^7.20.0`
 - `babel/cli` to `^7.19.0`
 - `babel/eslint-parser` to `^7.19.0`
 - `babel/preset-flow` to `^7.18.0`
 - `babel/preset-syntax-flow` to `^7.18.0`
 - Deduplicate / refresh others to take in patch updates

Changelog: [Internal] Bump Babel dependencies to latest 7.x

Reviewed By: JoeyMou

Differential Revision: D41449678

fbshipit-source-id: f04fe837a7961c4e2dde45fed59fcd138c2f8723
2022-11-30 19:16:23 -08:00
Pranav Yadav 5a20bd5b10 refactor: mv translateArrayTypeAnnotation (Flow,TS) fns > parsers-primitives.js (#35479)
Summary:
This PR is a subtask of https://github.com/facebook/react-native/issues/34872
Moved `translateArrayTypeAnnotation` (Flow,TS) fns to `parsers-primitives.js`
- combined `Flow` and `TS` `translateArrayTypeAnnotation` fn 's into common fn
- moved it to `parsers-primitives.js`
- re-organized imports and exports :)

## Changelog

[Internal] [Changed] - Moved `translateArrayTypeAnnotation` (Flow,TS) fns to `parsers-primitives.js`

Pull Request resolved: https://github.com/facebook/react-native/pull/35479

Test Plan:
- ensure 👇 is `#00ff00`
`yarn lint && yarn flow && yarn test-ci`

Reviewed By: cipolleschi

Differential Revision: D41548046

Pulled By: GijsWeterings

fbshipit-source-id: 8fd7214f8b1e669ba42f326f814674eec179fed5
2022-11-30 04:53:34 -08:00
Zihan Chen (MSFT) 8a38e03e0f Fix codegen to add T of Promise<T> in CodegenSchema.js (#35345)
Summary:
`Promise<T>` is used very often in turbo module as function return types. So `T` is a critical thing for type safety. To enable future generator to produce more specific type for `Promise`, `T` is added to the schema.

## Changelog

[General] [Changed] - Fix codegen to add `T` of `Promise<T>` in CodegenSchema.js

Pull Request resolved: https://github.com/facebook/react-native/pull/35345

Test Plan: `yarn jest react-native-codegen` passed

Reviewed By: lunaleaps

Differential Revision: D41304647

Pulled By: cipolleschi

fbshipit-source-id: 6cdd2357b83d4d8007c881a7090cbb8969f3ae9d
2022-11-29 03:37:59 -08:00
shivenmian b7a85b59b5 chore: renamed react-native-codegen to @react-native/codegen (#34804)
Summary:
Renamed react-native-codegen package to react-native/codegen and updated references, without changing the folder name; part of RFC480 (https://github.com/facebook/react-native/issues/34692). Follow-up from https://github.com/facebook/react-native/pull/34578

## Changelog

[General] [Changed] - Renamed react-native-codegen package to react-native/codegen and updated references

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

Pull Request resolved: https://github.com/facebook/react-native/pull/34804

Reviewed By: cortinico

Differential Revision: D39883584

Pulled By: hoxyq

fbshipit-source-id: 0ef384b75c6edd248b31e37b8f05f64b4d39ca6f
2022-11-28 08:28:51 -08:00
Vojtech Novak 90871861ce fix: codegen crash when parsing TS interfaces (#35492)
Summary:
when I'm defining a turbomodule spec, I tried to extract a common parameter into an interface.

The error I get is this:

```
[Codegen] >>>>> Processing RNSimpleToastSpec

[Codegen] Done.
/Users/vojta/_dev/_own/simple-toast/example/node_modules/react-native-codegen/lib/parsers/typescript/utils.js:111
        throw error;
        ^

TypeError: Cannot read properties of undefined (reading 'length')
    at isModuleInterface (/Users/vojta/_dev/_own/simple-toast/example/node_modules/react-native-codegen/lib/parsers/typescript/modules/index.js:695:18)
    at Array.filter (<anonymous>)
    at buildModuleSchema (/Users/vojta/_dev/_own/simple-toast/example/node_modules/react-native-codegen/lib/parsers/typescript/modules/index.js:709:44)
    at /Users/vojta/_dev/_own/simple-toast/example/node_modules/react-native-codegen/lib/parsers/typescript/index.js:158:9
    at guard (/Users/vojta/_dev/_own/simple-toast/example/node_modules/react-native-codegen/lib/parsers/typescript/utils.js:108:14)
    at buildSchema (/Users/vojta/_dev/_own/simple-toast/example/node_modules/react-native-codegen/lib/parsers/typescript/index.js:157:22)
    at Object.parseFile (/Users/vojta/_dev/_own/simple-toast/example/node_modules/react-native-codegen/lib/parsers/typescript/index.js:185:10)
```

After the fix I get this:

```
[Codegen] >>>>> Processing RNSimpleToastSpec

[Codegen] Done.
/Users/vojta/_dev/_own/simple-toast/example/node_modules/react-native-codegen/lib/parsers/typescript/utils.js:111
        throw error;
        ^

Invariant Violation: GenericTypeAnnotation 'Styles' must resolve to a TSTypeAliasDeclaration. Instead, it resolved to a 'TSInterfaceDeclaration'
```

Then I know that I should not be using an `interface` but a `type`

## Changelog

[Internal] [Fixed] - TS codegen crash when parsing interfaces

Pull Request resolved: https://github.com/facebook/react-native/pull/35492

Test Plan:
tested locally

let me know if you want an automated test

Reviewed By: cortinico

Differential Revision: D41548015

Pulled By: cipolleschi

fbshipit-source-id: 9acf02dffbb084831690f665357fb80225cbce0d
2022-11-28 04:19:47 -08:00
Phillip Pan 6c10df5c90 update graphics imports
Summary:
i recently made a change to modularize some of our graphics dependencies

i think this codegen will be incorrect now after my diff, so i updated it so we would codegen the correct deps

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D41451842

fbshipit-source-id: 98b5576e9fbd2d693c8bcfeac39d8dfb1b1e0584
2022-11-23 01:41:23 -08:00
matiassalles99 cc311ff01a Extract the UnsupportedArrayElementTypeAnnotationParserError in its o… (#35167)
Summary:
This PR is part of https://github.com/facebook/react-native/issues/34872
This PR extracts the [UnsupportedArrayElementTypeAnnotationParserError](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/flow/modules/index.js#L132) in its own throwing function.

## Changelog
[Internal] [Changed] - Extract the UnsupportedArrayElementTypeAnnotationParserError in its own throwing function

Pull Request resolved: https://github.com/facebook/react-native/pull/35167

Test Plan: <img width="454" alt="Screen Shot 2022-11-02 at 15 21 15" src="https://user-images.githubusercontent.com/57004457/199582495-23e4e3be-cb7e-41e8-a1fa-0250e127993c.png">

Reviewed By: cipolleschi

Differential Revision: D41437971

Pulled By: rshest

fbshipit-source-id: 14a6e09297d96f3b57568e0303e5cafff76e6f32
2022-11-22 06:16:24 -08:00
Stian Jensen 462d93d9c1 Bump jscodeshift to 0.14 (#35356)
Summary:
Jscodeshift has become maintained again in the past year, and has gotten rid of quite a good chunk of old dependencies that are no longer needed!

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

[Internal] [Changed] - update jscodeshift

Pull Request resolved: https://github.com/facebook/react-native/pull/35356

Reviewed By: cipolleschi

Differential Revision: D41325527

Pulled By: rshest

fbshipit-source-id: 666b25c9bb3b1720479e9e098968b3b983adc2b4
2022-11-17 08:04:00 -08:00
Pranav Yadav fb37dea38e Refactor buildPropertySchema fn of (Flow, TS) into common fn (#35288)
Summary:
This PR is a task of https://github.com/facebook/react-native/issues/34872
- combined `Flow` and `TS` `buildPropertySchema` fn 's into common fn
- added callback param `resolveTypeAnnotation` to the same
- moved it to `parsers-commons.js`
- re-organized imports and exports

## Changelog

[INTERNAL] [CHANGED] - [Codegen]: Refactored `buildPropertySchema` fn of (Flow, TS) into common fn in `parsers-commons.js`

Pull Request resolved: https://github.com/facebook/react-native/pull/35288

Test Plan:
- ensure 👇  is `#00ff00`
```bash
yarn lint && yarn flow && yarn test-ci
```

Reviewed By: christophpurrer

Differential Revision: D41247738

Pulled By: cipolleschi

fbshipit-source-id: aecc0ed8d07efa1c2c39e8a8e64b4ee73b720b8f
2022-11-16 07:45:20 -08:00
Pranav Yadav adee7be5d8 Chore: mv translateFunctionTypeAnnotation fn > parsers-commons.js (#35343)
Summary:
This PR should solve the `"Circular Dependencies"` problem/issue because of which some PRs are getting blocked as discussed here https://github.com/facebook/react-native/pull/35288#issuecomment-1314081952

- also moved below helpers to `parsers-commons.js`;
- `getTypeAnnotationParameters`
- `getFunctionNameFromParameter`
- `getParameterName`
- `getParameterTypeAnnotation`
- `getTypeAnnotationReturnType`

<3

## Changelog

[INTERNAL] [CHANGED] - Moved `translateFunctionTypeAnnotation` fn from `parsers-primitives.js` to `parsers-commons.js` also, moved it's helpers

Pull Request resolved: https://github.com/facebook/react-native/pull/35343

Test Plan:
- ensure 👇 is `#00ff00`

`yarn lint && yarn flow && yarn test-ci`

Reviewed By: christophpurrer

Differential Revision: D41273191

Pulled By: rshest

fbshipit-source-id: cc1839a91579e7914f05516a90b280a776510c9d
2022-11-15 06:47:55 -08:00
Christoph Purrer 2eccd59d7c react-native-code-gen Add Union Type support for Java/ObjC TurboModules (#35312)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/35312

Changelog:
[General][Added] react-native-code-gen Add Union Type support for Java/ObjC TurboModules

Reviewed By: javache

Differential Revision: D41216605

fbshipit-source-id: d411abed66c694d855ede40725667cbc7067538f
2022-11-14 19:11:26 -08:00