Add GeneratePropsJavaInterface

Summary: Adds GeneratePropsJavaInterface to the codegen to init open sourcing the Java codegen

Reviewed By: mdvacca, makovkastar

Differential Revision: D16280966

fbshipit-source-id: e3428285562329a22c1710cc7347c31f7c01d9c0
This commit is contained in:
Rick Hanlon authored and Facebook Github Bot committed 2019-07-19 15:04:05 -07:00
1 parent b1bec856c3
commit 9830d0d7b7
4 files changed
+540

No files matched your search

@@ -0,0 +1,77 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
*/
'use strict';
import type {ComponentShape} from '../../CodegenSchema';
function upperCaseFirst(inString: string): string {
return inString[0].toUpperCase() + inString.slice(1);
}
export function toSafeJavaString(input: string): string {
return input
.split('-')
.map(upperCaseFirst)
.join('');
}
export function getImports(component: ComponentShape): Set<string> {
const imports: Set<string> = new Set();
component.extendsProps.forEach(extendProps => {
switch (extendProps.type) {
case 'ReactNativeBuiltInType':
switch (extendProps.knownTypeName) {
case 'ReactNativeCoreViewProps':
imports.add('import android.view.View;');
return;
default:
(extendProps.knownTypeName: empty);
throw new Error('Invalid knownTypeName');
}
default:
(extendProps.type: empty);
throw new Error('Invalid extended type');
}
});
function addImportsForNativeName(name) {
switch (name) {
case 'ColorPrimitive':
return;
case 'ImageSourcePrimitive':
imports.add('import com.facebook.react.bridge.ReadableMap;');
return;
case 'PointPrimitive':
imports.add('import com.facebook.react.bridge.ReadableMap;');
return;
default:
(name: empty);
throw new Error(
`Invalid NativePrimitiveTypeAnnotation name, got ${name}`,
);
}
}
component.props.forEach(prop => {
const typeAnnotation = prop.typeAnnotation;
if (typeAnnotation.type === 'NativePrimitiveTypeAnnotation') {
addImportsForNativeName(typeAnnotation.name);
}
if (typeAnnotation.type === 'ArrayTypeAnnotation') {
imports.add('import com.facebook.react.bridge.ReadableArray;');
}
});
return imports;
}