Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/34796
This diff introduces the generation of custom native states using basic types as we do with the Props.
To make it work, the custom types are already writte in the Props.h file, therefore the State.h file must import that other file to have access to the required types.
This diff adds and updates the tests for the State.
## Changelog
[General][Added] - Generate custom Native State
Reviewed By: cortinico
Differential Revision: D39816763
fbshipit-source-id: 42d1aa9a6df23145f4a46ae8ccfb43d81fa651fb
Summary:
This PR is a task from https://github.com/facebook/react-native/issues/34872
_Extract the modules/utils.js from the flow and typescript folders in a shared parsers-commons.js file. Then, have the two parsers use the same wrapModuleSchema function for modules._
(`wrapModuleSchema` is a copy-paste mistake, in this case it is `wrapNullable` and `unwrapNullable`)
## 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] - Extracts Codegen's modules/utils.js files from the flow and typescript folders in parsers-commons
Pull Request resolved: https://github.com/facebook/react-native/pull/34898
Test Plan:
I ran `yarn jest react-native-codegen`:
<img width="775" alt="Capture d’écran 2022-10-07 à 21 29 48" src="https://user-images.githubusercontent.com/17070498/194639515-a446c2cf-daf3-43a1-9833-cd546ca5865e.png">
Reviewed By: cipolleschi
Differential Revision: D40193740
Pulled By: cipolleschi
fbshipit-source-id: 02cbacc215fe5dd9bdd0839d8796587ab2821906
Summary:
A few fixings to make `yarn jest react-native-doegen` works on Windows:
- ~~Add a `.gitignore` file to tell git not to track generated/temporary files.~~
- There is no `rm` on Windows, change it to `rimraf`.
I have been using it in the last 3 months and it works perfectly on Windows, otherwise I could not even build the code in my laptop.
## Changelog
[General] [Changed] - Make `yarn` and `yarn jest react-native-codegen` works on Windows with git
Pull Request resolved: https://github.com/facebook/react-native/pull/34854
Test Plan: `yarn jest react-native-codegen` passed
Reviewed By: cortinico
Differential Revision: D40059524
Pulled By: cortinico
fbshipit-source-id: e3cde2506c7d18c2b580099257637b90f4cb328c
Summary:
This diff helps the library maintainer to keep their spec file platform specific if some specs make no sense in one platform or in the other.
We are filtering the spec files when we need to create the Schema.
The diff modifies also the call sites in the `scripts` (for iOS) and in the `gradle-plugin` (for Android).
It also adds tests for the new functions in the CLI.
The change is completely additive and it should not change any pre-existing behaviour.
## Changelog
[General][Added] - Add support for platform-specific specs
Reviewed By: cortinico
Differential Revision: D40008581
fbshipit-source-id: b7fcf6d38f85fe10e4e00002d3c6f2910abdbe35
Summary:
`TSParenthesizedType`, `TSUnionType`, `TSNullKeyword`, `TSUndefinedKeyword`, `TSVoidKeyword` etc are repeatly processed in so many places. In this change I put them in a new file `parseTopLevelType.js`, and everyone call that file, all repeat implementation are deleted.
The `parseTopLevelType` function will look into a type consisted by the above types in any possible combination (but still very easy to do), and tell you if a type is nullable, or if there is a default value, and what is the real type with all noises removed.
Array types and union types are processed in component twice, for property of array, and property of nested arrays (`componentsUtils.js`). They are extracted into single functions.
## Changelog
[General] [Changed] - Refactor in turbo module TypeScript codegen: process `(T)`, `T|U`, `T|undefined` and related stuff in a central place
Pull Request resolved: https://github.com/facebook/react-native/pull/34814
Test Plan: `yarn jest react-native-codegen` passed
Reviewed By: yungsters, sammy-SC
Differential Revision: D40094373
fbshipit-source-id: f28e145bc4e7734be9036815ea425d820eadb8f0
Summary:
This change will expose the rrc_image module to be consumed via Prefab by user projects. This is needed as part of a change of the codegen to support custom states.
Changelog:
[Internal] [Changed] - Expose rrc_image to consuming build
Reviewed By: cipolleschi
Differential Revision: D40098541
fbshipit-source-id: 2e07d8b19f53ba93920009603c95a43b01f131eb
Summary:
This change will expose the `react_render_imagemanager` module to be consumed via Prefab by user projects. This is needed as part of a change of the codegen to support custom states.
Changelog:
[Internal] [Changed] - Expose react_render_imagemanager to consuming build
Reviewed By: cipolleschi
Differential Revision: D40022929
fbshipit-source-id: ece47503b11a641067ae6db82f1d69b244c9c5be
Summary:
Currently, it is not clear how to run codegen tests in the OSS.
## Changelog
[General][Added] - Add instructions about how to add tests in the OSS
Reviewed By: cortinico
Differential Revision: D40020907
fbshipit-source-id: 073ec8bf08d2a9e8b26287763dd881371ecf561d
Summary:
When using flow syntax like this, the parser would throw an error.
```
const foo = new ClassInstance();
export type {foo};
```
Changelog: [Internal]
Reviewed By: christophpurrer
Differential Revision: D39850098
fbshipit-source-id: 28a7748892559c2c4a0fb8afa8612cbeb5568058
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/34800
The TypeScript parser was not handling negative default values properly. The reason why is because the AST for those values is structurally different and wraps them in a `UnaryExpression` with the `-` operator.
This Diff adds the support for those default values and it also add some tests in both Flow and TS.
## Changelog
[General][Fixed] - Properly parse negative values
Reviewed By: cortinico
Differential Revision: D39847784
fbshipit-source-id: 95fc5768987477c540a54a7c4e4ff785d7a1e5d7
Summary:
Interface was supported in component, but it only allows interfaces in limited cases.
In this change, I extended interface support to all places where object literal type is supported.
I also refactor the code so that properties and events are able to share the same implementation.
In order not to mess up the diff, I noticed that implementations are repeated in processing array properties and non-array properties. But I leave it without refactoring. I will do it in future PRs.
I also commented potential problems I found in the code.
## Changelog
[General] [Changed] - Fix interface support in turbo module TypeScript codegen (component only)
Pull Request resolved: https://github.com/facebook/react-native/pull/34778
Test Plan: `yarn jest react-native-codegen` passed
Reviewed By: cortinico
Differential Revision: D39809230
Pulled By: cipolleschi
fbshipit-source-id: cfb51ce915249b5abceafee1c08b7e5762d03519
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/34786
This diff is the TS equivalent of D39686251.
It introduces the possibility to parse a custom Native State in Typescript.
The parsing follows the exact same rules as props, as initial heuristic. This should allow enough customization for the developers who needs a custom state.
Currently, we only support using `interface` for the state and the interface must contain the `NativeState` string in its name.
This diff introduces also tests for the TypeScript parser and it aligns the tests between Flow and TS.
## Changelog
[General][Added] - Implement custom Native State parsing in TypeScript
Reviewed By: cortinico
Differential Revision: D39811476
fbshipit-source-id: 1e1b86b50b9632c13157ff6c8115f5ebcbada643
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/34753
This Diff introduce a the capability to parse custom NativeStates in Flow. To achieve this I also had to define the CodegenSchema.
The parsing follows the exact same rules as props, as initial heuristic. This should allow enough customization for the developers who needs a custom state.
There is only a case I was not able to make it work that is STATE_ALIASED_LOCALLY, from the fixtures. I don't know how diffuse it is and I think we can live with some workarounds for the time being.
This diff also adds tests for the custom Native State Flow Parser.
## Changelog
[General][Added] - Implement custom Native State parsing in Flow
Reviewed By: cortinico
Differential Revision: D39686251
fbshipit-source-id: 446997a39b33b7e9351d5ba12cecaeff33df4d16
Summary:
In `buildEventSchema` and `buildPropSchema`, they check into property types to see if the given property could be converted into an event schema or a property schema. The original implementation only handles limited cases, I refactor them and make them easier to maintain.
In `getPropertyType` in `events.js`, it handles `(T)` at a wrong place, fixed.
In `getPropertyType` in `props.js`, it doesn't handle `(T)`, fixed.
And I also fixed some other issues to make the codegen reports error better.
There are many duplicated test cases that cover every piece of the code, I changed some of them so that it tests both original cases and new cases.
## Changelog
[General] [Changed] - Handle (T) and undefined properly in turbo module component codegen
Pull Request resolved: https://github.com/facebook/react-native/pull/34693
Test Plan: `yarn jest` passed in `packages/react-native-codegen`
Reviewed By: NickGerleman
Differential Revision: D39647075
Pulled By: cipolleschi
fbshipit-source-id: 8e1df2b54aab37b7151d0bf74260e2eba0602777
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/34754
This Diff is the second step of enabling the CodeGen to parse and generate a NativeState for the components.
The feature has been largely requested by the OSS community but it could be also helpful for people in Meta.
## Changelog
[General][Added] - Always generate an empty NativeState for Fabric Components
Reviewed By: cortinico
Differential Revision: D39696435
fbshipit-source-id: e24768af78f59696c0b4db009e8065bb5c89316b
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/34750
This Diff is the first step of enabling the CodeGen to parse and generate a NativeState for the components.
The feature has been largely requested by the OSS community but it could be also helpful for people in Meta.
To allow the generation of custom `NativeState`, we first have to always generate a `ViewEventEmitter`: that's because the `ConcreteShadowNode` template lists the Generics with this order: `Name`, `Props`, `EventEmitter`, Others...
If we skip the `EventEmitters` and we put the `State`, React Native would think that the State is actually an `EventEmitter` and the build step will fail.
## Changelog
[General][Added] - Always generate a ViewEventEmitter for Fabric Components
Reviewed By: cortinico
Differential Revision: D39509869
fbshipit-source-id: 390cc146ef013baf1ed09d55a0182a5aeb5b9d9e
Summary:
When `GenerateRCTThirdPartyFabricComponentsProviderCpp.js` generates `RCTThirdPartyFabricComponentsProvider.mm` an edge case happens in the following situation:
- The same library exports multiple modules with one component each (i.e. one component per file);
- The **first component** is excluded for iOS via the `excludedPlatforms` property in *codegenNativeComponent*.
A "loose" comma appears in the generated template, breaking the code.
```c++
Class<RCTComponentViewProtocol> RCTThirdPartyFabricComponentsProvider(const char *name) {
static std::unordered_map<std::string, Class (*)(void)> sFabricComponentsClassMap = {
, // <-- the offending comma
{"NativeComponent2", NativeComponent2Cls}, // rnmylibrary
};
}
```
At some point, `GenerateRCTThirdPartyFabricComponentsProviderCpp.js` does not properly filter out empty arrays resulting from excluded components. This does not seem to be a problem when the excluded component is not the first being processed, as the comma gets added at the end of the previous line, after the comment with the name of the library.
## 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
-->
[iOS] [Fixed] - Fix error in the Codegen template for ThirdPartyFabricComponentsProvider
Pull Request resolved: https://github.com/facebook/react-native/pull/34738
Test Plan:
<!-- Demonstrate the code is solid. Example: The exact commands you ran and their output, screenshots / videos if the pull request changes the user interface. -->
This is the schema that leads to the bug. Notice that the first component was excluded for iOS.
```json
{
"modules": {
"ComponentFile1": {
"type": "Component",
"components": {
"NativeComponent1": {
"excludedPlatforms": ["iOS"]
"extendsProps": [
{
"type": "ReactNativeBuiltInType",
"knownTypeName": "ReactNativeCoreViewProps"
}
],
"events": [],
"props": [],
"commands": []
}
}
},
"ComponentFile2": {
"type": "Component",
"components": {
"NativeComponent2": {
"extendsProps": [
{
"type": "ReactNativeBuiltInType",
"knownTypeName": "ReactNativeCoreViewProps"
}
],
"events": [],
"props": [],
"commands": []
}
}
}
}
```
`GenerateRCTThirdPartyFabricComponentsProviderCpp.js` should generate a template without the comma in the wrong position (before NativeComponent2).
I also added an additional test case to cover this problem. All the other tests passed.
Reviewed By: sammy-SC
Differential Revision: D39686573
Pulled By: cipolleschi
fbshipit-source-id: 6054464d024218eb0b2e02974aa5cc7c8aebbbc9
Summary:
1. In some situation (I don't know exactly how it is triggered but I found that during porting it to `react-native-windows`), `ExportNamedDeclaration.exportKind` is missing. Just skip the checking to `exportKind` because the rest of the checking is sufficient without reading this field.
2. Add `TSParenthesizedType` to module codegen in TypeScript, so that type `(T)` is treated like `T`.
## Changelog
[General] [Changed] - codegen: support TypeScript type `(T)` for turbo module codegen (module only)
Pull Request resolved: https://github.com/facebook/react-native/pull/34621
Test Plan: `yarn jest` passed in `packages/react-native-codegen`
Reviewed By: RSNara
Differential Revision: D39322001
Pulled By: cipolleschi
fbshipit-source-id: 1855711da7062a065c05a10f275e26baa88cf75f
Summary:
Pull request https://github.com/facebook/react-native/issues/34251 only partially worked because I didn't notice that there is ` 1` after the snapshot name. In this change I fixed the issue and find out there is one case that Flow and TypeScript parser generate different result.
It is expected since the test inputs are different. TypeScript removes one member because there is no `{...X, ...}` type in TypeScript. We could make the codegen support intersection type and rewrite it to `X & {...}`, but this will not be included here anyway.
## Changelog
[General] [Changed] - codegen: ensure equivalent Flow and TypeScript TM definition generates the same output
Pull Request resolved: https://github.com/facebook/react-native/pull/34620
Test Plan: `yarn jest` passed in `packages/react-native-codegen`
Reviewed By: NickGerleman
Differential Revision: D39321965
Pulled By: cipolleschi
fbshipit-source-id: aec60f5c9c18323cbd833bf5705af544d7db2e73
Summary:
Changelog: [internal]
This replaces all direct references to the `ReactNative` module (which is the Paper renderer) to `RendererProxy` which would select between Paper and Fabric correctly.
The implementation of these functions is exactly the same right now.
As per the removal of the fix for T55744311 in `ScrollView`, I verified this doesn't cause any issues in the screen where it failed before.
Reviewed By: javache
Differential Revision: D39270691
fbshipit-source-id: 03882748fe4b754b9a2c5e9d4c4f003b94ed49ef
Summary:
During the CoreContributor summit, we discovered that the `react-native-codegen` tests cannot be executed in the OSS due to this [issue with Jest](https://github.com/facebook/jest/issues/2567).
This PR moves the required variables inside the proper closure so that we can run tests in the OSS.
## Changelog
[General] [Fixed] - Enable the `react-native-codegen` tests in the OSS.
Pull Request resolved: https://github.com/facebook/react-native/pull/34594
Test Plan:
1. Run `yarn install` in the `react-native-codegen` folder.
2. Run `yarn jest`, see the test fail.
3. Apply the changes in this diff.
4. Run `yarn jest`, see the test pass.
Reviewed By: cortinico
Differential Revision: D39259164
Pulled By: cipolleschi
fbshipit-source-id: 7c4c0a7baa3c9b5e90a7ef75a37a0ec9d1b89db0
Summary:
There are cases where we want to pass an enum type into a TurboModule method which is handy to restrict certain arguments to a restricted set of values, e.g. restricting quality to ```enum Quality { SD, HD, }```
Approach:
- We are not generating an ```enum``` type in C++ but rather just cast type safe to the corresponding member type.
- We don't support mixed enum types at this time, e.g. ```export enum StringOption { One = 'one', Two = 2, Three = 'three', };``` will not work. See: https://www.typescriptlang.org/docs/handbook/enums.html#heterogeneous-enums
- We only support untyped (default to String), String, and Number enum properties
This is for C++ only, Java and ObjC are not supported atm.
Changelog:
[General][Added] - react-native-code-gen Add Enum Type support for C++ TurboModules
Reviewed By: RSNara
Differential Revision: D38880963
fbshipit-source-id: f2399b29948306bc555429b6f96c43ea4c39c46e
Summary:
There are cases where we want to pass a union type into a TurboModule method which is handy to restrict certain arguments to a restricted set of values, e.g. restricting quality to ```'low'```, ```high``` or resolution to ```720```, ```1080```.
- We are not generating an ```union``` type in C++ but rather just cast type safe to the corresponding member type.
- We don't support mixed primitive union types at this time, e.g. ```export type ChooseSomething = 'One' | 'Two' | 3 | false;``` does not work.
- We can support mixed object union types such as ... ```export type ChooseObject = {} | {low: string};``` - which need special logic in the C++ TM to correctly parse the resulting jsi::Object
This is for C++ only, Java and ObjC are not supported atm.
Changelog:
[General][Added] - react-native-code-gen Add Union Type support for C++ TurboModules
Reviewed By: javache
Differential Revision: D38919688
fbshipit-source-id: 0fd37545b32b4f2059a8babda62dab4a85de37a9
Summary:
Updating flow-parser to a more recent version which also supports parsing enums
Changelog: [Internal]
Reviewed By: yungsters
Differential Revision: D38832380
fbshipit-source-id: e3ef477abf85939ce3c9d92844e28dc59d8231c1
Summary:
With react-native 0.70-rc.3 and new arch, codegen may fail if it encounters `.d.ts` files because specs may appear to be unused.
## Changelog
[General] [Fixed] - Codegen should ignore `.d.ts` files
Pull Request resolved: https://github.com/facebook/react-native/pull/34439
Test Plan:
See repro in https://github.com/microsoft/react-native-test-app/pull/1052. The build will fail without manually patching this in.
If you prefer to use your own test app, try adding [react-native-safe-area-context](https://github.com/th3rdwave/react-native-safe-area-context) as a dependency.
Reviewed By: cipolleschi
Differential Revision: D38826388
Pulled By: cortinico
fbshipit-source-id: eb7c9be2d49286bae86b2428862fbf20f6f32ca5
Summary:
Sometime over the past few months (and with changes such as migrating to the `hermes-eslint` parser), a bunch of lint warnings crept into the codebase.
This does a pass to clean them all up, ignore generated files, and refactor some code to be... better.
There should be no observable behavior changes as a result of this.
Changelog:
[Internal]
Reviewed By: NickGerleman
Differential Revision: D38646643
fbshipit-source-id: a7b55d1e4cd5700340cc5c21f928baf3ea1d5a58
Summary:
Folly's molly target combines a number of targets that are supposed to be useable on mobile. Since we're trying to move away from folly, we should instead list explicitly which parts of folly we're using so we can remove them over time, and track which targets no longer have any folly dependencies.
Changelog: [Internal]
Reviewed By: NickGerleman
Differential Revision: D38352060
fbshipit-source-id: 09d0d84793692f97f4d49390c99c38b23441df54
Summary:
Flow and TypeScript are two very similar programming languages. They are both recognizable as inputs for turbo module codegen. It is reasonable to require equivalent Flow and TypeScript turbo module definition generates the same output.
I add some test cases to ensure this. Glad that no functional inconsistency is found here.
## Changelog
[General] [Changed] - codegen: ensure equivalent Flow and TypeScript TM definition generates the same output
Pull Request resolved: https://github.com/facebook/react-native/pull/34251
Test Plan: `yarn jest` passed in `packages/react-native-codegen`
Reviewed By: dmitryrykun
Differential Revision: D38116756
Pulled By: cipolleschi
fbshipit-source-id: 476adbd171a35813923f2020c826d21b1fc2eeed
Summary:
In D37873933, we update the buildifier binaries, which apply some minor changes to existing lint. Specifically, entries are now properly sorted.
Update the files in xplat such that updating buildifier does not cause lint changes in users diffs.
drop-conflicts
allow_many_files
#nocancel
#forcetdhashing
(Note: this ignores all push blocking failures!)
Reviewed By: d16r
Differential Revision: D37873936
fbshipit-source-id: f31d9c50d64ae99f99298977b471bf13e7ed5262
Summary:
Turbo module codegen for component parse `readonly T[]` and `readonly Object[][]` incorrectly. In this change, it is fixed, with necessary test cases added.
## Changelog
[General] [Added] - Support TypeScript array types for turbo module (component only)
Pull Request resolved: https://github.com/facebook/react-native/pull/34216
Test Plan: `yarn jest` passed in `packages/react-native-codegen`
Reviewed By: sammy-SC
Differential Revision: D37953004
Pulled By: cipolleschi
fbshipit-source-id: b71ea35eb9c4bd5dc41130f1dcf9201704ec298e
Summary:
According Flow's document, a maybe type of T means `T | null | undefined`, instead of `T | null | void`.
I think keeping TypeScript and Flow being consistent to each other is better.
## Changelog
<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->
[General] [Changed] - Accept TypeScript type `T | null | undefined` as a maybe type of T in turbo module.
Pull Request resolved: https://github.com/facebook/react-native/pull/34158
Test Plan: `yarn jest` passed in `packages/react-native-codegen`
Reviewed By: yungsters
Differential Revision: D37731169
Pulled By: philIip
fbshipit-source-id: b6d9b7e8991f60e12c1004bed5b937b34fb02c47
Summary:
Turbo module codegen supports arrays in both Flow and TypeScript, but it only recognize `Array<T>` and `ReadonlyArray<T>` in TypeScript.
In this change, `T[]` and `readonly T[]` are made recognizable in codegen.
## Changelog
[General] [Added] - Support TypeScript array types for turbo module (module only)
Pull Request resolved: https://github.com/facebook/react-native/pull/34183
Test Plan: `yarn jest` passed in `packages/react-native-codegen`
Reviewed By: lunaleaps
Differential Revision: D37812638
Pulled By: cipolleschi
fbshipit-source-id: d63b0585497a43c274d50e1877baab5d1cc3f8fa
Summary:
If you don't list `Cxx` as a supported platform, we will use Android builds, even when using `buck run` for local execution.
Changelog: [Internal]
Reviewed By: derolf
Differential Revision: D37600464
fbshipit-source-id: 6ba8566cde4180524351c9d8c647ce1d4ac5279d
Summary: Add annotations to function parameters required for Flow's Local Type Inference project. This codemod prepares the codebase to match Flow's new typechecking algorithm. The new algorithm will make Flow more reliable and predictable.
Reviewed By: bradzacher
Differential Revision: D37388949
fbshipit-source-id: cdcbc98035ce9b6994842005ea46df42de54f9b8
Summary: Add annotations to function parameters required for Flow's Local Type Inference project. This codemod prepares the codebase to match Flow's new typechecking algorithm. The new algorithm will make Flow more reliable and predicatable.
Reviewed By: bradzacher
Differential Revision: D37363351
fbshipit-source-id: a9d3df7db6f9d094ac2ce81aae1f3ab4f62b243a
Summary: Add annotations to function parameters required for Flow's Local Type Inference project. This codemod prepares the codebase to match Flow's new typechecking algorithm. The new algorithm will make Flow more reliable and predicatable.
Reviewed By: evanyeung
Differential Revision: D37353648
fbshipit-source-id: e5a0c685ced85a8ff353d578b373f836b376bb28