Writing code that writes code has proven to be valuable for keeping software scalable and maintainable. Moreover, code generation techniques significantly increase the profitability of software in many areas. Template engines have been available for decades for classical programming languages as well as web-based environments. DSL development tools like Eclipse Xtext do a great job on assisting us in developing solutions on the Java platform, for example.
The journey goes on, and it leads to the cloud. The new open source project Langium enables DSL development for cloud IDEs, and the ability for generating target code is a key feature. Writing code generators in JavaScript or TypeScript is possible, and tagged templates are a valuable feature. However, problems remain, like the dilemma of properly formatted templates vs. properly formatted generated code.
In this talk I will showcase the development of a code generator in TypeScript being executed on Node.js. My presentation includes solutions for:
- the whitespace problem
- unfolding definitions into different generated artifacts
- mastering complexity by introducing intermediate representations
- testing the generator