1 unstable release
| 0.1.16 | Sep 17, 2025 |
|---|
#490 in Development tools
660KB
16K
SLoC
Kotoba2TSX
Complete toolchain for converting Kotoba configuration files to React TypeScript components. Transforms Jsonnet-based UI declarations into production-ready TSX code with full type safety and modern React patterns.
๐ฏ Overview
Kotoba2TSX bridges the gap between declarative UI configuration and React development. It parses Kotoba files (Jsonnet format) and generates TypeScript React components with proper typing, state management, and event handling.
๐๏ธ Architecture
Core Pipeline
Kotoba File (.kotoba) โ Parser โ AST โ Generator โ TSX Component (.tsx)
โ โ โ โ
Jsonnet/JSON Validation TypeScript React + Hooks
Evaluation & Transform Generation Component Code
Key Components
Parser (parser.rs)
// Jsonnet-enhanced JSON parsing with validation
pub struct KotobaParser;
impl KotobaParser {
pub fn parse_file(&self, path: &str) -> Result<KotobaConfig>;
pub fn parse_content(&self, content: &str) -> Result<KotobaConfig>;
}
Generator (generator.rs)
// TypeScript + React code generation
pub struct TsxGenerator;
impl TsxGenerator {
pub fn generate_tsx(&self, config: &KotobaConfig) -> Result<String>;
pub fn generate_file(&self, config: &KotobaConfig, path: &str) -> Result<()>;
}
SWC Integration (swc_integration.rs)
// Advanced code formatting and optimization
pub struct SwcCodeGenerator;
impl SwcCodeGenerator {
pub fn format_code(&self, code: &str) -> Result<String>;
pub fn create_react_import(&self) -> String;
}
๐ Quality Metrics
| Metric | Status |
|---|---|
| Compilation | โ Clean (with warnings to fix) |
| Tests | โ Comprehensive test suite (61 tests) |
| Documentation | โ Complete API docs |
| Performance | โ Efficient parsing and generation |
| TSX Output | โ Production-ready React code |
| Type Safety | โ Full TypeScript integration |
๐ง Usage
Basic Conversion
use kotoba2tsx::prelude::*;
// Convert content string to TSX
let kotoba_content = r#"{
"name": "MyApp",
"version": "1.0.0",
"theme": "light",
"components": {
"Button": {
"type": "component",
"name": "Button",
"component_type": "button",
"props": {"children": "Click me"}
}
},
"handlers": {},
"states": {},
"config": {}
}"#;
let tsx_code = kotoba2tsx::convert_content(kotoba_content)?;
println!("{}", tsx_code);
File-based Conversion
use kotoba2tsx::convert_file;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Convert .kotoba file to .tsx file
convert_file("app.kotoba", "App.tsx").await?;
Ok(())
}
Advanced Generation
use kotoba2tsx::{KotobaParser, TsxGenerator};
// Custom configuration
let parser = KotobaParser::new();
let config = parser.parse_file("complex_app.kotoba").await?;
let generator = TsxGenerator::new();
let tsx_code = generator.generate_tsx(&config)?;
๐ Ecosystem Integration
Kotoba2TSX is part of the complete Kotoba toolchain:
| Crate | Purpose | Integration |
|---|---|---|
kotoba-jsonnet |
Required | Jsonnet evaluation for configuration files |
kotoba-core |
Required | Base types and IR definitions |
kotoba-server |
Optional | REST API for configuration serving |
swc |
Required | TypeScript/JavaScript processing |
๐งช Testing
cargo test -p kotoba2tsx
Test Coverage:
- โ JSON/Jsonnet parsing and validation
- โ TSX code generation for all component types
- โ TypeScript interface generation
- โ React hooks and state management
- โ Event handler integration
- โ CSS-in-JS styled components
- โ SWC code formatting
- โ File I/O operations
- โ Error handling and edge cases
๐ Performance
- Fast Parsing: Efficient Jsonnet evaluation and AST construction
- Optimized Generation: Template-based TSX code generation
- SWC Integration: Lightning-fast code formatting and optimization
- Streaming Output: Memory-efficient large file processing
- Parallel Processing: Concurrent file conversion support
๐ Security
- Input Validation: Comprehensive Jsonnet/JSON syntax validation
- Code Injection Prevention: Safe code generation without eval()
- Type Safety: Full TypeScript type checking
- Sanitized Output: XSS-safe React component generation
๐ API Reference
Core Types
KotobaConfig- Main configuration structureKotobaComponent- Individual component definitionComponentType- Component classification enumTsxGenerator- Main code generation engineKotobaParser- Configuration parsing engine
Generation Options
TsxGenerationOptions- Code generation configurationCssInJsLibrary- CSS-in-JS framework selectionComponentStyle- Styling configuration
Utilities
- [
convert_content()] - Convert string content to TSX - [
convert_file()] - Convert file to file (async) SwcCodeGenerator- Advanced code formatting
๐ค Contributing
See the main Kotoba repository for contribution guidelines.
๐ License
Licensed under MIT OR Apache-2.0. See LICENSE for details.
Dependencies
~22โ40MB
~587K SLoC