CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-react-native--codegen

Code generation tools for parsing React Native component definitions and generating native platform code.

Pending
Overview
Eval results
Files

schema-management.mddocs/

Schema Management

Combine, validate, and manage React Native component schemas with utilities for merging multiple schema files and ensuring correctness.

Capabilities

Schema Validation

Validate React Native schemas to ensure they follow the correct format and don't contain conflicts.

/**
 * Validate a schema and throw an error if invalid
 * @param schema - Schema object to validate
 * @throws Error with detailed validation failure information
 */
function validate(schema: SchemaType): void;

/**
 * Get validation errors without throwing
 * @param schema - Schema object to validate
 * @returns Array of error messages, empty if valid
 */
function getErrors(schema: SchemaType): Array<string>;

Usage Example:

const SchemaValidator = require('@react-native/codegen/lib/SchemaValidator');

try {
  SchemaValidator.validate(schema);
  console.log('Schema is valid');
} catch (error) {
  console.error('Schema validation failed:', error.message);
}

// Or check errors without throwing
const errors = SchemaValidator.getErrors(schema);
if (errors.length > 0) {
  console.error('Validation errors:', errors);
}

Schema Combination

Combine multiple JavaScript/TypeScript files into a single schema file.

/**
 * Combine schemas from multiple files and write to output file
 * @param fileList - Array of file paths to combine
 * @param platform - Platform filter (ios/android) or null for all platforms
 * @param outfile - Path where combined schema should be written
 * @param excludeRegExp - Regular expression to exclude files, or null
 * @param libraryName - Name for the combined library, or null
 */
function combineSchemasInFileListAndWriteToFile(
  fileList: Array<string>,
  platform: string | null,
  outfile: string,
  excludeRegExp: RegExp | null,
  libraryName: string | null
): void;

/**
 * Combine schemas from multiple files into a single schema object
 * @param files - Array of file paths to combine
 * @param libraryName - Name for the combined library, or null
 * @returns Combined schema object
 */
function combineSchemas(
  files: Array<string>, 
  libraryName: string | null
): SchemaType;

Usage Example:

const { combineSchemasInFileListAndWriteToFile } = require('@react-native/codegen/lib/cli/combine/combine-js-to-schema');

// Combine multiple component files into one schema
combineSchemasInFileListAndWriteToFile(
  ['./Component1.js', './Component2.tsx', './Module.ts'],
  'ios', // Platform filter
  './combined-schema.json',
  /test/, // Exclude test files
  'MyLibrary'
);

File Filtering Utilities

Utilities for filtering and processing files during schema combination.

/**
 * Filter function to identify JavaScript/TypeScript files suitable for parsing
 * @param filename - File path to check
 * @returns true if file should be included in schema generation
 */
function filterJSFile(filename: string): boolean;

Usage Example:

const { filterJSFile } = require('@react-native/codegen/lib/cli/combine/combine-utils');
const glob = require('glob');

// Find all parseable files in a directory
const allFiles = glob.sync('src/**/*');
const jsFiles = allFiles.filter(filterJSFile);
console.log('Files to parse:', jsFiles);

Schema Validation Rules

The validation system checks for several types of errors:

Component Name Conflicts

// This would cause a validation error:
// File1.js exports component "Button"
// File2.js also exports component "Button"

Components with the same name across different modules are not allowed, as this creates ambiguity in the generated code.

Platform Exclusions

Schemas can specify platform exclusions to prevent generation for specific platforms:

interface ComponentSchema {
  type: 'Component';
  components: { [componentName: string]: ComponentInfo };
  /** Platforms where this component should not be generated */
  excludedPlatforms?: Array<PlatformType>;
}

interface NativeModuleSchema {
  type: 'NativeModule';
  aliasMap: NativeModuleAliasMap;
  enumMap: NativeModuleEnumMap;
  spec: NativeModuleSpec;
  /** Platforms where this module should not be generated */  
  excludedPlatforms?: Array<PlatformType>;
}

type PlatformType = 'iOS' | 'android';

Schema Structure Requirements

Valid schemas must have:

  • At least one module definition
  • Proper component or module type specification
  • Valid type annotations for all properties
  • Consistent naming conventions

Advanced Schema Operations

Platform-Specific Filtering

When combining schemas, you can filter by platform to create platform-specific builds:

// Generate iOS-only schema
combineSchemasInFileListAndWriteToFile(
  files,
  'ios', // Only include iOS-compatible components
  'ios-schema.json',
  null,
  'MyLibraryIOS'
);

// Generate Android-only schema  
combineSchemasInFileListAndWriteToFile(
  files,
  'android', // Only include Android-compatible components
  'android-schema.json', 
  null,
  'MyLibraryAndroid'
);

Selective File Inclusion

Use regular expressions to exclude specific file patterns:

const testFileRegex = /__tests__|\.test\.|\.spec\./;

combineSchemasInFileListAndWriteToFile(
  files,
  null,
  'production-schema.json',
  testFileRegex, // Exclude test files
  'MyLibrary'
);

Schema Merging Logic

When combining schemas, the system:

  1. Parses each file using the appropriate parser (Flow/TypeScript)
  2. Filters by platform if specified
  3. Merges module definitions from all files
  4. Validates uniqueness of component/module names
  5. Applies exclusion rules based on regex patterns
  6. Outputs combined schema in standardized format

Conflicting definitions with the same name cause validation errors and must be resolved by renaming one of the conflicting items.

Install with Tessl CLI

npx tessl i tessl/npm-react-native--codegen

docs

cli-tools.md

code-generation.md

file-parsing.md

index.md

schema-management.md

tile.json