How xlboy/type-zen works
Main Components
• Core Engine (packages/core)
• Contains the compiler, parser, AST, grammar (Nearley grammar files, moo lexer, etc.), and related utilities.
• Divided into submodules such as:
▫ AST – building and traversing type representations.
▫ Compiler – converting sugar syntax to valid TypeScript.
▫ Grammar – nearley definitions and lexer implementations.
• CLI Interface (packages/cli)
• Command-line executable that exposes the core functionality.
• Wraps the core engine features by parsing user input, invoking the compiler, and outputting TypeScript code.
• TypeScript Plugin (packages/ts-plugin)
• Integrates with the TypeScript compiler.
• Offers runtime transformation or real-time diagnostics integration; it uses logging, snapshotting, and other helper modules.
• VSCode Extension (packages/vscode)
• Provides language support, editor integrations, and commands (like “compile-current-file”) within the Visual Studio Code ecosystem.
• Relies on configurations, language syntax highlighting files, and underlying core functionalities (possibly reusing the TS Plugin or direct calls to the core engine).
• Global Preset Types (packages/preset-type)
• Supplies global type declarations (like TZ_URS and others) required by the compiled output.
• Imported automatically via tsconfig for TypeScript.
• Playground Website (website/playground)
• A front-end built (using React as suggested by TSX files) that lets users experiment with TypeZen interactively.
• Acts as a client that calls the underlying transformation, probably via a web assembly variant or by bundling the core’s transformation logic.
Relationships and Data Flow
– The Core Engine is clearly the heart of the system. Both CLI, TS Plugin, and even parts of the VSCode extension depend on it.
– The CLI takes user commands and feeds code into the Core Engine; its output is then either presented on the console or used to generate files.
– The TS Plugin integrates with the TypeScript toolchain and may intercept source files or serve as a transformer that dispatches code to the Core Engine.
– The VSCode Extension acts as an orchestration layer for a better developer experience, providing commands, configuration, and possibly real-time analysis by interfacing with the TS Plugin or directly the Core Engine.
– The Playground website offers a UI front-end to demonstrate the language’s capabilities, sending code to be compiled and then showing the transformation results.
Architectural Patterns and Design Principles
– Modular design: Each major functionality (CLI, core engine, plugin, extension, playground) lives within its own package, easing maintenance and testing.
– Clear separation of concerns: The core language processing is separated from the different interfaces that leverage it. This promotes reusability (the same core engine is used by CLI, plugin, and extension).
– Standard dependency integration: Through configuration files (pnpm-workspace, tsconfig.json, etc.), each package declares its dependencies with clear interfaces.
– Extensibility: The modular approach (including plans for an “Unplugin” integration supporting multiple bundlers such as Webpack/Vite/Rollup) hints at design to allow future integration.
– Emphasis on testing: The large number of test directories across packages indicates thorough testing as a design principle.
Final Reminders
– Clearly label every component and sub-component.
– Capture as much detail as possible without clutter—explain the architectural “heart” (packages/core) and then show how each supporting package ties into the overall system.
– As extra references, include any external integrations (like how the Playground and VSCode Extension relate to community usage and developer tools).