Граф коммитов

462 Коммитов

Автор SHA1 Сообщение Дата
Nick Gerleman 4d247fe058 Avoid Repo Structure Assumptions in RNTester Imports (#30141)
Summary:
RNTester has some imports left over that operate on directory traversal, assuming it is contained within react-native sources. This change makes these imports relative to react-native, enabling usage outside of the RN repo.

Relates to https://github.com/microsoft/react-native-windows/issues/6210

## Changelog

[Internal] [Fixed] - Avoid File Structure Assumptions in RNTester Imports

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

Test Plan: Validated we can bundle and flow-check both iOS + Android

Reviewed By: cpojer

Differential Revision: D24259628

Pulled By: appden

fbshipit-source-id: 0c21b5d354b01785d8402599da3b0a5be81b4c6d
2020-10-12 19:56:09 -07:00
simek b7167c23fc PlatformColors: add missing clearColor for iOS (#30054)
Summary:
This small PR adds missing [`clearColor`](https://developer.apple.com/documentation/uikit/uicolor/1621945-clearcolor?language=objc) to the avaiable PlatformColors on iOS.

**Please let me know** if you would like to see ["Fixed colors"](https://developer.apple.com/documentation/uikit/uicolor/standard_colors?language=objc) added to the `PlatformColors`. I can address this within this PR or create a separate one.

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

[iOS] [Added] - PlatformColors: add missing `clearColor`

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

Test Plan:
[(I had to disable the Dark Mode to fix the RNTester readability problems)](https://user-images.githubusercontent.com/719641/94453196-b35cb000-01b0-11eb-8d7d-73d48ceecf53.PNG)

Transparent/clear color has been added to the RNTester PlatformColors API example:
![IMG_6782](https://user-images.githubusercontent.com/719641/94453172-ae97fc00-01b0-11eb-9bef-1a1a387ac009.PNG)

Reviewed By: shergin

Differential Revision: D24241160

Pulled By: sammy-SC

fbshipit-source-id: 41fb51677329cc3b3f915d5d08694c07b4ef2cf3
2020-10-12 02:14:42 -07:00
Janic Duplessis 6685aba462 Update template to xcode 12 (#30150)
Summary:
This makes a few change to the template xcodeproj

- Update to recommended settings for Xcode 12
- Remove the main.jsbundle file that doesn't exists, this file is included automatically in the app bundle and doesn't need to be in xcode (it won't even be at that path).
- Add -e to the bundle images script. It seems like Xcode doesn't fail the build on script errors anymore so this makes it so the build stop. If it doesn't stop the app will launch and crash because the bundle doesn't exist (note this is only if running the app in release mode since in debug we don't generate the bundle).

## Changelog

[iOS] [Added] - Update template to xcode 12

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

Test Plan: Made the same changes in an app and made sure it ran well.

Reviewed By: cpojer

Differential Revision: D24237481

Pulled By: sammy-SC

fbshipit-source-id: 2b06a24c510c423eb45a1a840ea365b64506321b
2020-10-12 01:28:32 -07:00
Ramanpreet Nara 8e943d8f5a Remove reliance on util.inspect in snapshot tests
Summary:
According to Node's documentation: https://nodejs.org/api/util.html#util_util_inspect_object_showhidden_depth_colors

> The util.inspect() method returns a string representation of object that is intended for debugging. **The output of util.inspect may change at any time and should not be depended upon programmatically.**

Therefore, this diff switches over our RN Codegen snapshot tests to use a `JSON.stringify` call, followed by a replace of `"` with `'`. This gets the job done without compromising readability.

**Question:** Why do we not use `"`? A: Jest escapes all `"` in the snapshots, which makes reading the snapshots/using them in the console harder.

Changelog: [Internal]

Reviewed By: hramos

Differential Revision: D24157056

fbshipit-source-id: 2f1aa2df28ac3ed4aa17bcdbcd23846ddbf804cf
2020-10-07 14:28:20 -07:00
Héctor Ramos 4eb8bd5084 trivial: Remove whitespace in generated output
Summary:
Remove extraneous newlines before and after structs, before copyright headers, and other locations.

Changelog: [Internal]

Reviewed By: fkgozali

Differential Revision: D24117137

fbshipit-source-id: 194996019b4cadef9239a78334f31c0bc89e3901
2020-10-06 01:44:20 -07:00
Héctor Ramos d4937b925b Use correct var name for element when handling arrays of type aliases
Summary:
Fixes an issue where, if a spec uses an array of elements where the element is a type alias, the generated code would use the wrong variable name.

An example of such a spec can be found in `NativeExceptionsManager.js`:
```
  +reportSoftException: (
    message: string,
    stack: Array<StackFrame>,
    exceptionId: number,
  ) => void;
```

The fix ensures the local variable name is passed through, ensuring that either p or itemValue_N is used when appropriate.

Changelog: [Internal]

Reviewed By: RSNara

Differential Revision: D24116429

fbshipit-source-id: f39b75adb604c751d70a284a11a7fa6649b1344d
2020-10-05 12:57:34 -07:00
Christoph Nakazawa 43624dd193 Update Babel to 7.11.6
Summary: Changelog: [Internal]

Reviewed By: GijsWeterings

Differential Revision: D24039996

fbshipit-source-id: 26ec2a988faadf24ea76d9124052d9397682787b
2020-10-05 05:46:31 -07:00
Ramanpreet Nara 3b6b039e85 Make JavaPoet JavaSpec Generator handle NullableTypeAnnotation
Summary: Changelog: [Internal]

Reviewed By: fkgozali

Differential Revision: D24030728

fbshipit-source-id: b79f7dfa7304bfddc6333c641eadcaa55c0cb7a0
2020-10-01 19:30:08 -07:00
Ramanpreet Nara 5122e33f4e Make ObjCPP Generator handle NullableTypeAnnotation
Summary:
See title.
Changelog: [Internal]

Reviewed By: hramos

Differential Revision: D24027243

fbshipit-source-id: 4e03a14fd23c1f5f3d282fc14a760d7549cdd6b9
2020-10-01 19:30:08 -07:00
Ramanpreet Nara 6d7e763e85 Make JniCpp Generator handle NullableTypeAnnotation
Summary: Changelog: [Internal]

Reviewed By: hramos

Differential Revision: D24027245

fbshipit-source-id: 73f9e7ec7ccb7627d2df63798a4269c860388dde
2020-10-01 19:30:08 -07:00
Ramanpreet Nara 0a2228ae3f Make JavaSpec Generator handle NullableTypeAnnotation
Summary: Changelog: [Internal]

Reviewed By: hramos

Differential Revision: D24027248

fbshipit-source-id: 7ca0e04e8e2f19cffc9aea3d6db51f86703c06f7
2020-10-01 19:30:08 -07:00
Ramanpreet Nara d9dc9d5d0a Make H Generator handle NullableTypeAnnotation
Summary: Changelog: [Internal]

Reviewed By: hramos

Differential Revision: D24027247

fbshipit-source-id: cc326f5db919de1f6c6b56603f420c87272da918
2020-10-01 19:30:08 -07:00
Ramanpreet Nara 1a90e1b471 Make Cpp Generator handle NullableTypeAnnotation
Summary:
See title.
Changelog: [Internal]

Reviewed By: hramos

Differential Revision: D24027244

fbshipit-source-id: 4f967a7a72afd6aa7a886c16eeb683c52da2dc9c
2020-10-01 19:30:07 -07:00
Ramanpreet Nara 6fe06ca160 Update generator fixtures to use NullableTypeAnnotation
Summary:
This will make sure that the snapshot tests for the generators work with NullableTypeAnnotation.
Changelog: [Internal]

Reviewed By: hramos

Differential Revision: D24027246

fbshipit-source-id: cee93e40be7585ec527087d114f8326c2ecb9ddd
2020-10-01 19:30:07 -07:00
Ramanpreet Nara 8d807dfbc3 Introduce NullableTypeAnnotation for Flow Module Parser
Summary:
Previously, all our type annotations contained a `nullable` property. This diff removes that property from all our NativeModule type annotations, and instead introduces a `NullableTypeAnnotation`.

**Some Benefits:**
- In all our serialization functions, we use Flow exhaustive checking to ensure that all type-annotations can be serialized. Since nullability is now recorded as a type annotation, Flow will ensure we always explicitly handle nullability. Previously, with nullability as a property, we could ignore it without any feedback from flow.
- This aligns the NativeModule schema with the ESTree spec.
- After this diff, we're one step closer to sharing type annotations with Codegen's schema. Many NativeModule type annotations now have the same shape as their Codegen counterparts. They will be merged in a subsequent diff.

**Downsides:**
- If you want to check whether a type annotation is of type `T`, you have to remember to unwrap the type annotation *yourself*. Flow won't warn you if you forget to unwrap the type, which can lead to incomplete handling to nullable types in our generators.
- When you're creating type annotations in code, previously, you *had* to specify nullability, since it was a property on all type annotation objects. Now, it's very possible for you to forget to wrap the type annotation, which will just lead to nullability bugs.

**Notes:**
- In the scheam, exported type annotations are *always* required. They can be made nullable using the new `Nullable` genric type.

Changelog: [Internal]

Reviewed By: fkgozali

Differential Revision: D24026887

fbshipit-source-id: 9e71e2c6102dc506824403dbb712488ca8507d08
2020-10-01 19:30:07 -07:00
Ramanpreet Nara 0a2e0f777b Make Flow Parser snapshots more readable
Summary:
The Flow Parser's snapshots, which are serializations of the CodegenSchema object, are extremely difficult to read. In this diff, I explicitly serialized the schema objects using Node's `util.inspect`.

**Benefits:**
- The snapshots are more readable now.
- You can copy-paste the objects from the snapshot files directly into the Chrome console, or into other JavaScript files. This is a very useful feature, when we're testing the generators, or other infra that depends on the codegen.

Changelog: [Internal]

Reviewed By: hramos

Differential Revision: D24063112

fbshipit-source-id: 2c6ec3424aac8bab2688dc6ae286b73f90e4bef1
2020-10-01 19:30:07 -07:00
Frieder Bluemle 553fb8b28d Update Android Gradle plugin to 4.0.1 (#29013)
Summary:
This is a major version update that needs to be tested thoroughly.

Android Studio 4.0.1 is now available in the stable channel

https://androidstudio.googleblog.com/2020/05/android-studio-40-available-in-stable.html
https://developer.android.com/studio/releases/gradle-plugin#4.0.1

## Changelog

[Android] [Changed] - Update Android Gradle plugin to 4.0.1

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

Test Plan:
Build project

Closes https://github.com/facebook/react-native/issues/29044

Reviewed By: shergin

Differential Revision: D24041233

Pulled By: fkgozali

fbshipit-source-id: 68ef0f313aa773866e65796e323ed0f19f41f834
2020-09-30 22:16:54 -07:00
Kevin Gozali 1d89ef87b6 TurboModule: exclude NativeSampleTurboModule in the schema
Summary:
The sample module is meant for demo only, but it lives alongside other core modules in react-native repo.

For now, exclude it in the Flow-type parsing, just like `NativeUIManager.js`

Changelog: [Internal]

Reviewed By: hramos

Differential Revision: D24005108

fbshipit-source-id: 9ef524bfe2778dd983c94d1701f9ce49da5e0a68
2020-09-29 18:41:14 -07:00
Kevin Gozali 94b198cfd5 TurboModule Android: install SampleTurboModule and the playground to RNTester
Summary:
This compiles SampleTurboModule into RNTester Android. It also adds the NativeModule playground to show case TurboModule system to RNTester examples, just like in iOS.

{F337854369}

Changelog: [Android][TurboModule] Added TurboModule example to RNTester when `USE_CODEGEN` is set

Reviewed By: hramos

Differential Revision: D24004711

fbshipit-source-id: b682dd51fa998ee2e60f8d6ffd8c39220d13a7fe
2020-09-29 18:41:14 -07:00
Kevin Gozali b9a1ea9e9e TurboModule Android: move SampleTurboModule impl and spec to OSS
Summary:
This is the Java/JNI impl of the NativeSampleTurboModule.js, just like on iOS. The files here are supposed to be generated by the react-native-codegen, but they are checked in to the repo for easier build integration with RNTester.

Changelog: [Internal]

Reviewed By: hramos

Differential Revision: D23985746

fbshipit-source-id: 46340d778f3d964efe5b538d15ebe0f2cab04862
2020-09-29 18:41:14 -07:00
Kevin Gozali b931bd33fe TurboModule Android: properly set up RNTester ndkBuild and cleanup dependencies
Summary:
Before RNTester compilation starts, it needs to wait for :ReactAndroid NDK build to finish, so that it knows where to find the exported .so files. This tells the `preBuild` task to depends on `:ReactAndroid:prepareReactNdkLibs` task. The .so files are now copied over to the local project build dir, instead of depending on :ReactAndroid's build dir.

For cleanup, the reverse ordering is needed: before `clean` removed our temp dir to store the copied .so files, make sure the ndkBuild cleanup tasks execute beforehand.

Changelog: [Internal]

Reviewed By: hramos

Differential Revision: D23982989

fbshipit-source-id: 955d7c9bccb5855b6b066fca89764df2ede89f63
2020-09-29 18:41:14 -07:00
Ramanpreet Nara 6e6443afd0 Remove header_namespace from module codegen target
Summary:
Making this change because I see this error when compiling Internationalization
```
➜  fbsource buck build //xplat/js/RKJSModules/Libraries/Internationalization:generated_objcpp_modules-InternationalizationApple
buck-out/gen/33fbdb84/xplat/js/RKJSModules/Libraries/Internationalization/generate_module_mm-Internationalization/FBReactNativeInternationalizationSpec-generated.mm:15:9: fatal error: 'FBReactNativeInternationalizationSpec.h' file not found
#import "FBReactNativeInternationalizationSpec.h"
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
Command failed with exit code 1.

command: [/Applications/Xcode_11.6.0_fb.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++, @/Users/ramanpreet/fbsource/buck-out/bin/33fbdb84/xplat/js/RKJSModules/Libraries/Internationalization/generated_objcpp_modules-InternationalizationApple#compile-FBReactNativeInternationalizationSpec-generated.mm.o...
```

Since the header namespace is "FBReactNativeInternationalizationSpec", we can only import the header file via "FBReactNativeInternationalizationSpec/FBReactNativeInternationalizationSpec.h", according to this buck documentation: https://buck.build/rule/cxx_library.html#headers. Not entirely sure how this target compiled before.

The legacy codegen buck target also set the header namespace to "": https://fburl.com/diffusion/3p85qhf9.

Changelog: [Internal]

Reviewed By: hramos

Differential Revision: D23978436

fbshipit-source-id: c9cd7c710edf94df6df10778f8603870f92275a7
2020-09-29 14:39:42 -07:00
Ramanpreet Nara 6d6e04619f Fix ObjC++ structs and method mapping
Summary:
Adjust generated ObjC++ code to resolve a few build time and run time errors:

* Suppress CONSTANTS struct implementations
* Use type alias name as struct name when serializing arguments that involve a type alias
* Use actual number of arguments for a method when generating method map.

With these changes in place, RNTester can be built and run using the code that is generated by the new codegen.

Changelog: [Internal]

Reviewed By: hramos

Differential Revision: D23926500

fbshipit-source-id: 88fcbb795fd71dc8155eb26348db943975e13e84
2020-09-29 14:39:41 -07:00
Ramanpreet Nara 97d3e85c29 Fix ObjC++ module generator output
Summary:
* Removed extraneous closing brace.
* Fixed static method signature, replacing double colon with an underscore (`static facebook::jsi::Value __hostFunction_Native${moduleName}SpecJSI::${methodName}()` -> `static facebook::jsi::Value __hostFunction_Native${moduleName}SpecJSI_${methodName}()`).
* Wrap `getConstants` selector name with `selector()`.
* Pass through `getConstants` and `constantsToExport` to allow de-duping of `getConstants` method in generator output.

Note that the FBReactNativeSpec that is output by the generator still has some issues that need to be addressed before it can be used.

Changelog: [Internal]

Reviewed By: fkgozali

Differential Revision: D23910505

fbshipit-source-id: 37d884885b8878f38d40637377c2a74a728c3a13
2020-09-29 14:39:41 -07:00
Ramanpreet Nara 1b3dc1d9e3 Fix GenerateModuleJniCpp
Summary:
Just updated the generator to work with the new RN Codegen Flow Parser types.

Changelog: [Internal]

Reviewed By: fkgozali

Differential Revision: D23667253

fbshipit-source-id: ef94e75287d37dfd7b80f61455a1bfa34bddeb28
2020-09-29 14:39:41 -07:00
Ramanpreet Nara cedd628fc3 Fix GenerateModuleJniH
Summary:
Just updated the generator to work with the new RN Codegen Flow Parser types.

Changelog: [Internal]

Reviewed By: PeteTheHeat

Differential Revision: D23667250

fbshipit-source-id: f36b5418101c40331964d1f9ede7c6bd7924383d
2020-09-29 14:39:41 -07:00
Ramanpreet Nara 560ac1a9fa Fix GenerateModuleJavaSpec
Summary:
Just updated the generator to work with the new RN Codegen Flow Parser types.

Changelog: [Internal]

Reviewed By: hramos

Differential Revision: D23667255

fbshipit-source-id: 40b7747aad89f6d5bbb9f42d59a4df9633060c66
2020-09-29 14:39:41 -07:00
Ramanpreet Nara 4ab7cc236a Fix GenerateModuleH
Summary:
Just updated the generator to work with the new RN Codegen Flow Parser types.

Changelog: [Internal]

Reviewed By: PeteTheHeat

Differential Revision: D23667252

fbshipit-source-id: 34404a478ddd67446d82b5f98e1051300064e95c
2020-09-29 14:39:41 -07:00
Ramanpreet Nara c0408b56fc Fix GenerateModuleCpp
Summary:
Just updating this generator to understand the new RN Codegen Module parser flow types.

Changelog: [Internal]

Reviewed By: PeteTheHeat

Differential Revision: D23667254

fbshipit-source-id: 558dd7ac5b4541edf40248b8ab8bb50d31fa8624
2020-09-29 14:39:40 -07:00
Ramanpreet Nara 1ac1255d63 E2E snapshot test ObjC++ generator
Summary:
NativeModule specs exist under `react-native-github/packages/react-native-codegen/src/__tests__/modules/fixtures`. `GenerateModuleObjCpp-test.js` runs the RN Codegen on those NativeModule specs, and saves the output inside a snapshot. For convenience, the folowing command runs the legacy codegen on the fixtures:

```
buck build fbsource//xplat/js/react-native-github/packages/react-native-codegen/src/__tests__/modules:RNCodegenModuleFixtures-flow-types-ios --show-output
```

Changelog: [Internal]

Reviewed By: hramos

Differential Revision: D23637708

fbshipit-source-id: 3319f319515eca42b4499682313fea6e0bdc2a06
2020-09-29 14:39:40 -07:00
Ramanpreet Nara c267b8de72 Rewrite ObjC++ module generator
Summary:
## Misc. Improvements

* We now have 95%+ flow coverage in all generator files. Henceforth, we can make changes to these files with more confidence, and trust flow to catch more errors. This should also improve the DevX of working on these files.
* Better templates: Instead of doing string replace with RegExps, we instead use functions and leverage JS template literals to generate our code. A few benefits: (1) data dependencies of templates are clearly visible, and statically checked by flow, (2) the templates are more readable in VSCode.
* Merged the GenerateModuleHObjCpp.js and GenerateModuleMm.js generators. They can share a lot of logic, so it's not a good idea to keep them separate.
* The ObjC++ module generator no longer generates “dead” structs (i.e structs that aren’t used by type-safety infra). In fact, it explicitly only supports the types in our Wiki. (I know this wasn’t the case with the legacy codegen, because we were generating native code for enums in the legacy codegen). This is a mixed bag. The test to verify correctness will be more difficult to write. However, keeping structs in the codegen needlessly complicates the parsers + generators, and creates technical debt for us to clean up later.

## Abstractions
- **StructCollector:** As we serialize NativeModule methods, when we detect an ObjectTypeAnnotation in the return type of `getConstants()` or inside a method param, we must create a Struct JS object for it. When we detect a type-alias (also in the same locations), we must look up that type-alias and create a Struct from its RHS. A Struct is basically an ObjectTypeAnnotation with a context (i.e: used in getConstants() vs as a method param), that cannot contain other ObjectTypeAnnotations.
- **serializeMethod.js** Given a NativeModule method type annotation, output the protocol method, JS return type, selector, a record of which params were structs, and which structs. Basically, this is all the information necessary to generate the declaration and implementation codegen for a partiular NativeModule method.
- **serializeStruct/*.js**: After creating all these Structs, we need to loop over all of them, and tranform them into ObjC++ code.
  - **serializeStruct.js**: Depending on the struct context, calls either `serializeRegularStruct.js` or `serializeConstantsStruct.js`. Both of these files have the same layout/abstractions. They look very similar.
- **serializeModule.js:** Outputs RCTCxxConvert categories for transforming `NSDictionary *` into C++ structs. Outputs ObjCTurboModule subclass.

## Algorithm
```
for spec in NativeModuleSpecs
  structCollector = new StructCollector
  resolveAlias = (aliasName) => nullthrows(spec.aliases[aliasName])

  methodDatas = []
  for method in methods(spec)
    methodData.push(serializeMethod(method, structCollector, resolveAlias))
  end

  structs = structCollector.getStructs()

  output generateImplCodegen(methodDatas, structs)
  output generateHeaderCodegen(methodDatas, structs)
end
```

Changelog: [Internal]

Reviewed By: hramos

Differential Revision: D23633940

fbshipit-source-id: 7c29f458b65434f4865ef1993061b0f0dc7d04ce
2020-09-29 14:39:40 -07:00
Ramanpreet Nara c300193853 Create utilities for module generators
Summary:
There are two operations we do in every NativeModule generator:
- We convert the `SchemaType` into a map of NativeModule schemas
- If the type-annotation is a TypeAliasTypeAnnotation, we resolve it by doing a lookup on the NativeModuleAliasMap. This is usually followed by an invariant to assert that the lookup didn't fail.

Both procedures have been translated into utilities for use across our generators. I also deleted `getTypeAliasTypeAnnotation` which will no longer be used.

Changelog: [Internal]

Reviewed By: hramos

Differential Revision: D23667249

fbshipit-source-id: 4e34078980e2caa4daed77c38b1168bfc161c31c
2020-09-29 14:39:40 -07:00
Ramanpreet Nara 5f5ccfcb2c Update generator fixtures to comply with Schema flow types
Summary:
In diffs below, we made several changes to the NativeModule schema. This diff just ensures that the input to our generator snapshot tests, which are module schemas, are valid.

Changelog: [Internal]

Reviewed By: TheSavior

Differential Revision: D23633942

fbshipit-source-id: 444ccd60f6ec76e348a8e54b946260464ff3aeee
2020-09-29 14:39:40 -07:00
Ramanpreet Nara 91a7da087b Refactor: Make NativeModuleArrayTypeAnnotation elementType customizable
Summary:
In the future, we'll use `NativeModuleArrayTypeAnnotation` inside JS struct objects. Structs cannot contain `ObjectTypeAnnotations` anywhere. Therefore, we need the elementType of `NativeModuuleArrayTypeAnnotation`'s elementType customizable.

Changelog: [Internal]

Reviewed By: hramos

Differential Revision: D23645210

fbshipit-source-id: 97abb993d59536ebd68ec08b18ce7f2801c68a2d
2020-09-29 14:39:39 -07:00
Ramanpreet Nara 4927de6011 Rename GenericPromiseTypeAnnotation to PromiseTypeAnnotation
Summary:
We have first class support for Promises in our codegen. So, it's more appropriate to just call this PromiseTypeAnnotation, as opposed to GenericPromiseTypeAnnotation.

Changelog: [Internal]

Reviewed By: hramos

Differential Revision: D23645209

fbshipit-source-id: bfc0b909750e221e18be33acf197f342a2918aa9
2020-09-29 14:39:39 -07:00
Ramanpreet Nara 92a6722bf2 Refactor: Make NativeModuleAliasMap $ReadOnly
Summary:
We were using `$ReadOnly<NativeModuleAliasMap>` everywhere, so I figured we'd just make the type itself `$ReadOnly`.

Changelog: [Internal]

Reviewed By: PeteTheHeat

Differential Revision: D23645207

fbshipit-source-id: 4e018d5768f4fcfd00492def7d840a5054cb2b73
2020-09-29 14:39:39 -07:00
Ramanpreet Nara 2e0fb69365 Refactor: Introduce Required<T> generic type
Summary:
This diff introduces a `Required<T>` generic type in CodegenSchema. Why? NativeModule aliase RHSs are basically ObjectTypeAnnotations but they have `nullable` fixed to `false`. This generic type reduces duplication in the schema flow types.

Changelog: [Internal]

Reviewed By: fkgozali

Differential Revision: D23645208

fbshipit-source-id: da984f64fa17d8533a3ea74b132ce10aae9aa7ed
2020-09-29 14:39:39 -07:00
Ramanpreet Nara 1194a36efa Export module type annotations from CodegenSchema
Summary:
This diff has a few changes:
- All type annotations are now declared top-level, and exported from `CodegenSchema.js`.

Changelog: [Internal]

Reviewed By: hramos

Differential Revision: D23616473

fbshipit-source-id: 1509c304305e56674bd76c44bc49f755dfeaa332
2020-09-29 14:39:39 -07:00
Ramanpreet Nara 572e1da889 Fix type alias nullability resolution in module parser
Summary:
Consider this case:

```
type Animal = ?{|
  name: string,
|};

type B = Animal

export interface Spec extends TurboModule {
  +greet: (animal: B) => void;
}
```

The generated output for this spec is:

```
namespace JS {
  namespace NativeSampleTurboModule {
    struct Animal {
      NSString *name() const;

      Animal(NSDictionary *const v) : _v(v) {}
    private:
      NSDictionary *_v;
    };
  }
}

protocol NativeSampleTurboModuleSpec <RCTBridgeModule, RCTTurboModule>
- (void)greet:(JS::NativeSampleTurboModule::Animal &)animal;
end
```

Observations:
  1. The codegen looks as though we wrote `+greet: (animal: ?Animal) => void;` as opposed to `+greet: (animal: B) => void;`
  2. The generated struct is called `Animal`, not `B`.

## After this diff
Whenever we detect a usage of a type alias, we recursively resolve it, keeping a track of whether the resolution will be nullable. In this example, we follow B to Animal, and then Animal to ?{|name: string|}.

Then, we:
  1. Replace the `B` in `+greet: (animal: B) => void;` with `?Animal`,
  2. Pretend that `Animal = {|name: string|}`.

Why do we make all type alias RHSs required?
 2. This design is simpler than managing nullability in both the type alias usage, and the type alias RHS.
 3. What does it mean for a C++ struct, which is what this type alias RHS will generate, to be nullable? ¯\_(ツ)_/¯. Nullability is a concept that only makes sense when talking about instances (i.e: usages) of the C++ structs. Hence, it's better to manage nullability within the actual TypeAliasTypeAnnotation nodes, and not the associated ObjectTypeAnnotations.

## Other Changes
- Whenever we use the `Animal` type-alias, the e2e jest tests validate that the type alias exists in the module schema.

Changelog: [Internal]

Reviewed By: PeteTheHeat

Differential Revision: D23225934

fbshipit-source-id: 8316dea2ec6e2d50cad90e178963c6264044f7b7
2020-09-29 14:39:39 -07:00
Ramanpreet Nara aba4e87873 Add unit tests to validate module parser
Summary:
## Test Structure
- Parameter Parsing
  - For type in [optional, nullable, optional and nullable, required]:
    - Can parse Primitives
    - Can parse Object
    - Can parse Arrays
      - Can parse primitive element types
      - Can parse Object element types
      - Can parse Array element types
      - Can parse Reserved Type element types
      - Can parse Type alias element types
      - Can parse Object Literals
    - Can parse Function
    - Can parse Reserved Types (e.g: RootTag)
    - Can parse Type alias
    - Can parse Object Literals
      - For prop type in [optional, nullable, optional and nullable, required]:
        - Can parse primitive prop types
        - Can parse Object prop types
        - Can parse Array prop types
        - Can parse Reserved Type prop types
        - Can parse Type alias prop types
        - Can parse Object Literal prop types
- Return Parsing
  - For type in [nullable, required]:
    - Can parse Promises
    - Can parse Primitives
    - Can parse Object
    - Can parse Arrays
      - Can parse primitive element types
      - Can parse Object element types
      - Can parse Array element types
      - Can parse Reserved Type element types
      - Can parse Type alias element types
      - Can parse Object Literals
    - Can parse Function
    - Can parse Reserved Types (e.g: RootTag)
    - Can parse Type aliases
    - Can parse Object Literals
      - For prop type in [optional, nullable, optional and nullable, required]:
        - Can parse primitive prop types
        - Can parse Object prop types
        - Can parse Array prop types
        - Can parse Reserved Type prop types
        - Can parse Type alias prop types
        - Can parse Object Literal prop types

Changelog: [Internal]

Reviewed By: PeteTheHeat

Differential Revision: D23089925

fbshipit-source-id: 73c3b1ef33b402265c14f0ac9e364414a5d54dca
2020-09-29 14:39:38 -07:00
Ramanpreet Nara 2d19037041 Rewrite Flow module parser
Summary:
This diff:
1. Simplifies the NativeModule schema Flow types.
2. Simplifies the NativeModule Flow parser, to accomodate the modified schema, and to reduce code duplication.

**Notes:**
- If the parser detects an unrecognized type within the context of parsing an Array's element type, it'll omit the `elementType` property of the `ArrayTypeAnnotation`. Details: T72031674. **Rationale:** Basically, when an array element type is supported, we use it in our generators. When it's unsupported, we ignore it. In the unsupported case, there's no point in trying to parse the Array element type, which is why I decided to omit the `elementType` property. Ideally, our NativeModule specs would never use unsupported types, in any context. This would allow us to always parse and use the elementType. However, that seems like a it could be a hefty migration: we have > 400 specs. Since, this isn't a battle we need to fight right now, I left a TODO at the relevant lines instead.
- The legacy codegen would generate structs for each object literal type in the file. In this re-implementation of the parser, I only insert into the aliases array when we detect a usage of a type-alias to an ObjectLiteral type annotation. With this decision, we won't be able to generate these unnecessary structs. This is good because we get rid of dead code. It's bad because it might make our migration to this codegen bit more difficult.

[WARNING] This diff produces flow failures that will be addressed in subsequent diffs.

Changelog: [Internal]

Reviewed By: fkgozali

Differential Revision: D23201387

fbshipit-source-id: 55ce0df925a8bae0e7d5bb2a9b63167607eba461
2020-09-29 14:39:38 -07:00
Ramanpreet Nara f9ea52574e Cleanup buildMethodSchema and introduce nullable methods
Summary:
## Changes
- Started doing cleanup under `/parsers/flow/modules/methods.js`
- Rename `NativeModuleMethodTypeShape` -> `NativeModulePropertyShape`
- Moved optional property from `FunctionTypeAnnotation` to the `NativeModulePropertyShape`
- Introduced a nullable property inside what was once `FunctionTypeAnnotation`.

Changelog: [Internal]

Reviewed By: PeteTheHeat

Differential Revision: D23146050

fbshipit-source-id: 2fe97bb9c0736242682133e4923131a54bbea54b
2020-09-29 14:39:38 -07:00
Ramanpreet Nara 747f493feb Colocate alias/method generation logic
Summary:
## Changes
- Generating the aliases was split over `parsers/flow/modules/index.js`, and `parsers/flow/modules/aliases.js`. I moved everything to the latter file.
- Generating methods was split over `parsers/flow/modules/index.js` and `parsers/flow/modules/methods.js`. I moved everything to the latter file.
- More type-safety

Changelog: [Internal]

Reviewed By: PeteTheHeat

Differential Revision: D23143382

fbshipit-source-id: e11b76bee7917a7db37ae7f1af64da5f046c5d1e
2020-09-29 14:39:38 -07:00
Ramanpreet Nara 78057729ce Clean up parsers/flow/modules/index.js
Summary:
Changes:
- Adding type annotations, where possible
- Delete unnecessary functions

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D23138227

fbshipit-source-id: b4b47492ddef49f496ed9fcb61ef1c000e3e8f18
2020-09-29 14:39:38 -07:00
Ramanpreet Nara 8c138baf4e Fix parser's buildSchema's return type
Summary:
`buildSchema` delegates to two other functions: `buildComponentSchema`, or `buildComponentSchema`. Both return have a return type of *required* `SchemaType`. Therefore, `buildSchema` can have a return type of `SchemaType`.

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D23135499

fbshipit-source-id: f13db17549c93b965f372d9b68d06efc2a40c6cc
2020-09-29 14:39:37 -07:00
Ramanpreet Nara 22d6e04da1 Clean up Module/Component detection logic in Flow parser
Summary:
Just broke down getConfigType into two separate functions: `isModule` and `isComponent`.
- Cleaned up `isComponent`, to check for the the AST node types.
- Re-implemented `isModule`
- Improved error messages.

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D23121896

fbshipit-source-id: 3df2b2e334c4cea8eabe2e73ecb9f1f1217e7be4
2020-09-29 14:39:37 -07:00
Ramanpreet Nara 1435d654d7 Restructure getTypes function
Summary:
There are two types of types we care about:
- Type aliases
- Interface Declarations

These types can be exported.

I think we should build the types dictionary from only those types. Everything else should be ignored.

Changelog: [Internal]

Reviewed By: fkgozali

Differential Revision: D23120241

fbshipit-source-id: 9f023081d0f9c85b45407b180ae7c3e7391eb725
2020-09-29 14:39:37 -07:00
Ramanpreet Nara 5142e99437 Delete NativeModuleSchemaBuilderConfig type
Summary:
Unecessary, since `NativeModuleShape` is the exact same thing.

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D23119784

fbshipit-source-id: 8c4aded88a97a80aa977c13cc27e32fbe68150aa
2020-09-29 14:39:37 -07:00
Ramanpreet Nara 341e05ff62 Allow NativeModule method return types to use type aliases
Summary:
We already support type-aliases in our NativeModule method params. This diff extends the support to NativeModule method returns.

**Note:** I need to see if I need to update the generators to handle this case. Will do that in this diff, after working through other higher priority stuff.

Changelog: [Internal]

Reviewed By: PeteTheHeat

Differential Revision: D22828839

fbshipit-source-id: cf5c756d3cacf067df217cdb6212946320a2d4be
2020-09-29 14:39:37 -07:00
simek 29f5dc2ae0 chore: deduplicate lock, update packages repository fields (#30044)
Summary:
This small PR includes the following changes:
* deduplicate yarn lock file using [`yarn-deduplicate`](https://github.com/atlassian/yarn-deduplicate) package
  * deduplicate script has been added as `update-lock`, let me know if you would like also to see this in [`postinstall`](https://docs.npmjs.com/misc/scripts) (to automatically optimize lock on every dependency change)
* according to the [npm docs](https://docs.npmjs.com/files/package.json#repository):
  * main `package.json` repository field has been replaced with shorthand
  * monorepo packages repository field has been extended by `directory`

The main goal of introducing deduplication script was to optimize the dependencies footprint while developing and speed up the initial installation process. Running `yarn-deduplicate` also increase the security in some way, because it enforces usage only of the latest version of the package. You can read more about the benefits in the deduplicate script [repository](https://github.com/atlassian/yarn-deduplicate#duplicated-packages).

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

[Internal] [Added] - add yarn lock deduplication script

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

Test Plan: `yarn install` was successful, this also should not affect yarn workspaces in any way.

Reviewed By: GijsWeterings

Differential Revision: D23959812

Pulled By: cpojer

fbshipit-source-id: e2455e3718378e1ce6206e79463d4083f8fe5d47
2020-09-28 22:41:50 -07:00