mirror of
https://github.com/react/react-native.git
synced 2026-10-05 19:31:56 +08:00
Summary: This gets us on the latest Prettier 2.x: https://prettier.io/blog/2020/03/21/2.0.0.html Notably, this adds support for TypeScript 3.8, which introduces new syntax, such as `import type`. Reviewed By: zertosh Differential Revision: D20636268 fbshipit-source-id: fca5833d003804333a05ba16325bbbe0e06d6c8a
112 lines
2.9 KiB
JavaScript
112 lines
2.9 KiB
JavaScript
/**
|
|
* 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);
|
|
}
|
|
|
|
function getInterfaceJavaClassName(componentName: string): string {
|
|
return `${componentName.replace(/^RCT/, '')}ManagerInterface`;
|
|
}
|
|
|
|
function getDelegateJavaClassName(componentName: string): string {
|
|
return `${componentName.replace(/^RCT/, '')}ManagerDelegate`;
|
|
}
|
|
|
|
function toSafeJavaString(
|
|
input: string,
|
|
shouldUpperCaseFirst?: boolean,
|
|
): string {
|
|
const parts = input.split('-');
|
|
|
|
if (shouldUpperCaseFirst === false) {
|
|
return parts.join('');
|
|
}
|
|
|
|
return parts.map(upperCaseFirst).join('');
|
|
}
|
|
|
|
function getImports(
|
|
component: ComponentShape,
|
|
type: 'interface' | 'delegate',
|
|
): 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':
|
|
if (type === 'delegate') {
|
|
imports.add('import com.facebook.react.bridge.ColorPropConverter;');
|
|
}
|
|
return;
|
|
case 'ImageSourcePrimitive':
|
|
imports.add('import com.facebook.react.bridge.ReadableMap;');
|
|
return;
|
|
case 'PointPrimitive':
|
|
imports.add('import com.facebook.react.bridge.ReadableMap;');
|
|
return;
|
|
case 'EdgeInsetsPrimitive':
|
|
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;');
|
|
}
|
|
|
|
if (typeAnnotation.type === 'ObjectTypeAnnotation') {
|
|
imports.add('import com.facebook.react.bridge.ReadableMap;');
|
|
}
|
|
});
|
|
|
|
return imports;
|
|
}
|
|
|
|
module.exports = {
|
|
getInterfaceJavaClassName,
|
|
getDelegateJavaClassName,
|
|
toSafeJavaString,
|
|
getImports,
|
|
};
|