Code generation tools for parsing React Native component definitions and generating native platform code.
—
Parse Flow and TypeScript files containing React Native component definitions and convert them to standardized schemas.
Parse Flow-annotated JavaScript files containing React Native component definitions.
/**
* Parser for Flow-annotated React Native component files
*/
class FlowParser {
/**
* Parse a Flow file and extract React Native component/module schema
* @param filename - Path to the Flow file to parse
* @returns Schema object describing the components and modules
*/
parseFile(filename: string): SchemaType;
}Usage Example:
const { FlowParser } = require('@react-native/codegen/lib/parsers/flow/parser');
const flowParser = new FlowParser();
const schema = flowParser.parseFile('./MyComponent.js');
console.log('Parsed modules:', Object.keys(schema.modules));Parse TypeScript files containing React Native component definitions.
/**
* Parser for TypeScript React Native component files
*/
class TypeScriptParser {
/**
* Parse a TypeScript file and extract React Native component/module schema
* @param filename - Path to the TypeScript file to parse
* @returns Schema object describing the components and modules
*/
parseFile(filename: string): SchemaType;
}Usage Example:
const { TypeScriptParser } = require('@react-native/codegen/lib/parsers/typescript/parser');
const typescriptParser = new TypeScriptParser();
const schema = typescriptParser.parseFile('./MyComponent.tsx');
console.log('Parsed modules:', Object.keys(schema.modules));Parse multiple files and output their schemas to console (CLI utility).
/**
* Parse multiple files and output JSON schemas to console
* @param files - Array of file paths to parse (supports both .js/.ts/.tsx extensions)
*/
function parseFiles(files: Array<string>): void;Usage Example:
const parseFiles = require('@react-native/codegen/lib/cli/parser/parser');
// Parse multiple files - automatically detects Flow vs TypeScript
parseFiles(['./Component1.js', './Component2.tsx', './Module.ts']);
// Outputs JSON schema for each file to consoleThe parsers automatically detect file types based on extensions:
.js files with Flow annotations (// @flow).ts and .tsx filescodegenNativeComponent callsextends TurboModule definitionsAll parsers produce schemas in the same standardized format:
interface SchemaType {
/** Optional library name for the schema */
libraryName?: string;
/** Map of module names to their definitions */
modules: {
[hasteModuleName: string]: ComponentSchema | NativeModuleSchema;
};
}
interface ComponentSchema {
type: 'Component';
/** Map of component names to their configurations */
components: {
[componentName: string]: ComponentInfo;
};
/** Platforms where this component should not be generated */
excludedPlatforms?: Array<PlatformType>;
}
interface NativeModuleSchema {
type: 'NativeModule';
/** Type aliases used in the module */
aliasMap: NativeModuleAliasMap;
/** Enums defined in the module */
enumMap: NativeModuleEnumMap;
/** Module specification with methods and properties */
spec: NativeModuleSpec;
/** Platforms where this module should not be generated */
excludedPlatforms?: Array<PlatformType>;
}
type PlatformType = 'iOS' | 'android';The parsers recognize these patterns in source files:
// Flow
export default codegenNativeComponent<Props>('MyComponent');
// TypeScript
export default codegenNativeComponent<Props>('MyComponent');// Flow
export interface Spec extends TurboModule {
+getConstants: () => {||};
+getString: (arg: string) => string;
}
// TypeScript
export interface Spec extends TurboModule {
readonly getConstants: () => {};
getString(arg: string): string;
}Parsing errors are reported through the error handling system:
/** Error classes for parsing failures */
class UnsupportedObjectPropertyTypeAnnotationParserError extends Error {
constructor(message: string, hasteModuleName: string, propertyName: string);
}
/** Utility functions for error handling and reporting */
function getErrorMessage(error: ParserError): string;When parsing fails, the parsers throw descriptive errors indicating:
Both parsers use the same underlying architecture:
Install with Tessl CLI
npx tessl i tessl/npm-react-native--codegen