PHP 7.4.33
Preview: GeneratePropsJavaInterface.js Size: 7.35 KB
/var/www/uibuilder.cmshelp.dk/httpdocs/node_modules/@react-native/codegen/lib/generators/components/GeneratePropsJavaInterface.js
/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 *
 * @format
 */

'use strict';

const _require = require('./JavaHelpers'),
  getImports = _require.getImports,
  getInterfaceJavaClassName = _require.getInterfaceJavaClassName,
  toSafeJavaString = _require.toSafeJavaString;

// File path -> contents

const FileTemplate = ({
  packageName,
  imports,
  className,
  extendClasses,
  methods,
}) => `/**
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
*
* Do not edit this file as changes may cause incorrect behavior and will be lost
* once the code is regenerated.
*
* ${'@'}generated by codegen project: GeneratePropsJavaInterface.js
*/

package ${packageName};

${imports}

public interface ${className}<T extends ${extendClasses}> extends ViewManagerWithGeneratedInterface {
  ${methods}
}
`;
function addNullable(imports) {
  imports.add('import androidx.annotation.Nullable;');
}
function getJavaValueForProp(prop, imports) {
  const typeAnnotation = prop.typeAnnotation;
  switch (typeAnnotation.type) {
    case 'BooleanTypeAnnotation':
      if (typeAnnotation.default === null) {
        addNullable(imports);
        return '@Nullable Boolean value';
      } else {
        return 'boolean value';
      }
    case 'StringTypeAnnotation':
      addNullable(imports);
      return '@Nullable String value';
    case 'Int32TypeAnnotation':
      return 'int value';
    case 'DoubleTypeAnnotation':
      return 'double value';
    case 'FloatTypeAnnotation':
      if (typeAnnotation.default === null) {
        addNullable(imports);
        return '@Nullable Float value';
      } else {
        return 'float value';
      }
    case 'ReservedPropTypeAnnotation':
      switch (typeAnnotation.name) {
        case 'ColorPrimitive':
          addNullable(imports);
          return '@Nullable Integer value';
        case 'ImageSourcePrimitive':
          addNullable(imports);
          return '@Nullable ReadableMap value';
        case 'ImageRequestPrimitive':
          addNullable(imports);
          return '@Nullable ReadableMap value';
        case 'PointPrimitive':
          addNullable(imports);
          return '@Nullable ReadableMap value';
        case 'EdgeInsetsPrimitive':
          addNullable(imports);
          return '@Nullable ReadableMap value';
        case 'DimensionPrimitive':
          addNullable(imports);
          return '@Nullable YogaValue value';
        default:
          typeAnnotation.name;
          throw new Error('Received unknown ReservedPropTypeAnnotation');
      }
    case 'ArrayTypeAnnotation': {
      addNullable(imports);
      return '@Nullable ReadableArray value';
    }
    case 'ObjectTypeAnnotation': {
      addNullable(imports);
      return '@Nullable ReadableMap value';
    }
    case 'StringEnumTypeAnnotation':
      addNullable(imports);
      return '@Nullable String value';
    case 'Int32EnumTypeAnnotation':
      addNullable(imports);
      return '@Nullable Integer value';
    case 'MixedTypeAnnotation':
      return 'Dynamic value';
    default:
      typeAnnotation;
      throw new Error('Received invalid typeAnnotation');
  }
}
function generatePropsString(component, imports) {
  if (component.props.length === 0) {
    return '// No props';
  }
  return component.props
    .map(prop => {
      return `void set${toSafeJavaString(
        prop.name,
      )}(T view, ${getJavaValueForProp(prop, imports)});`;
    })
    .join('\n' + '  ');
}
function getCommandArgJavaType(param) {
  const typeAnnotation = param.typeAnnotation;
  switch (typeAnnotation.type) {
    case 'ReservedTypeAnnotation':
      switch (typeAnnotation.name) {
        case 'RootTag':
          return 'double';
        default:
          typeAnnotation.name;
          throw new Error(`Receieved invalid type: ${typeAnnotation.name}`);
      }
    case 'BooleanTypeAnnotation':
      return 'boolean';
    case 'DoubleTypeAnnotation':
      return 'double';
    case 'FloatTypeAnnotation':
      return 'float';
    case 'Int32TypeAnnotation':
      return 'int';
    case 'StringTypeAnnotation':
      return 'String';
    case 'ArrayTypeAnnotation':
      return 'ReadableArray';
    default:
      typeAnnotation.type;
      throw new Error('Receieved invalid typeAnnotation');
  }
}
function getCommandArguments(command, componentName) {
  return [
    'T view',
    ...command.typeAnnotation.params.map(param => {
      const commandArgJavaType = getCommandArgJavaType(param);
      return `${commandArgJavaType} ${param.name}`;
    }),
  ].join(', ');
}
function generateCommandsString(component, componentName) {
  return component.commands
    .map(command => {
      const safeJavaName = toSafeJavaString(command.name, false);
      return `void ${safeJavaName}(${getCommandArguments(
        command,
        componentName,
      )});`;
    })
    .join('\n' + '  ');
}
function getClassExtendString(component) {
  const extendString = component.extendsProps
    .map(extendProps => {
      switch (extendProps.type) {
        case 'ReactNativeBuiltInType':
          switch (extendProps.knownTypeName) {
            case 'ReactNativeCoreViewProps':
              return 'View';
            default:
              extendProps.knownTypeName;
              throw new Error('Invalid knownTypeName');
          }
        default:
          extendProps.type;
          throw new Error('Invalid extended type');
      }
    })
    .join('');
  return extendString;
}
module.exports = {
  generate(
    libraryName,
    schema,
    packageName,
    assumeNonnull = false,
    headerPrefix,
  ) {
    // TODO: This doesn't support custom package name yet.
    const normalizedPackageName = 'com.facebook.react.viewmanagers';
    const outputDir = `java/${normalizedPackageName.replace(/\./g, '/')}`;
    const files = new Map();
    Object.keys(schema.modules).forEach(moduleName => {
      const module = schema.modules[moduleName];
      if (module.type !== 'Component') {
        return;
      }
      const components = module.components;

      // No components in this module
      if (components == null) {
        return;
      }
      return Object.keys(components)
        .filter(componentName => {
          const component = components[componentName];
          return !(
            component.excludedPlatforms &&
            component.excludedPlatforms.includes('android')
          );
        })
        .forEach(componentName => {
          const component = components[componentName];
          const className = getInterfaceJavaClassName(componentName);
          const imports = getImports(component, 'interface');
          const propsString = generatePropsString(component, imports);
          const commandsString = generateCommandsString(
            component,
            componentName,
          );
          const extendString = getClassExtendString(component);
          const replacedTemplate = FileTemplate({
            imports: Array.from(imports).sort().join('\n'),
            packageName: normalizedPackageName,
            className,
            extendClasses: extendString,
            methods: [propsString, commandsString]
              .join('\n' + '  ')
              .trimRight(),
          });
          files.set(`${outputDir}/${className}.java`, replacedTemplate);
        });
    });
    return files;
  },
};

Directory Contents

Dirs: 2 × Files: 42
Name Size Perms Modified Actions
- drwxr-xr-x 2025-03-28 11:04:37
Edit Download
- drwxr-xr-x 2025-03-28 11:04:37
Edit Download
8.09 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
9.54 KB lrw-r--r-- 2025-03-28 11:04:35
Edit Download
1.46 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
1.53 KB lrw-r--r-- 2025-03-28 11:04:35
Edit Download
8.16 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
9.20 KB lrw-r--r-- 2025-03-28 11:04:35
Edit Download
2.44 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
2.59 KB lrw-r--r-- 2025-03-28 11:04:36
Edit Download
2.43 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
2.58 KB lrw-r--r-- 2025-03-28 11:04:36
Edit Download
9.34 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
10.35 KB lrw-r--r-- 2025-03-28 11:04:36
Edit Download
10.78 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
12.08 KB lrw-r--r-- 2025-03-28 11:04:36
Edit Download
7.99 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
8.90 KB lrw-r--r-- 2025-03-28 11:04:36
Edit Download
4.33 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
4.54 KB lrw-r--r-- 2025-03-28 11:04:36
Edit Download
18.22 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
19.69 KB lrw-r--r-- 2025-03-28 11:04:36
Edit Download
9.54 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
10.24 KB lrw-r--r-- 2025-03-28 11:04:36
Edit Download
7.35 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
8.00 KB lrw-r--r-- 2025-03-28 11:04:36
Edit Download
2.09 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
2.23 KB lrw-r--r-- 2025-03-28 11:04:36
Edit Download
2.67 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
2.84 KB lrw-r--r-- 2025-03-28 11:04:36
Edit Download
2.01 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
2.15 KB lrw-r--r-- 2025-03-28 11:04:36
Edit Download
2.07 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
2.28 KB lrw-r--r-- 2025-03-28 11:04:36
Edit Download
5.27 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
5.88 KB lrw-r--r-- 2025-03-28 11:04:36
Edit Download
2.99 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
3.10 KB lrw-r--r-- 2025-03-28 11:04:36
Edit Download
3.23 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
3.33 KB lrw-r--r-- 2025-03-28 11:04:36
Edit Download
13.37 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
13.95 KB lrw-r--r-- 2025-03-28 11:04:36
Edit Download
3.58 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
3.99 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
If ZipArchive is unavailable, a .tar will be created (no compression).