SOUTHWORKS Dev Team
October 26, 2022
Authored by Agustin Seifert, Principle Software Engineer at SOUTHWORKS
You have an idea, open your IDE, prepare your favorite development stack and you start writing lines of code at the speed of light - everything flows. At the end of the adventure, you realize you have a react web application that consumes different services made in C#, GO and NodeJS. A big success!
So romantic, isn't it? ...But not everything in the garden is rosy. Let's think about it one second, you have a react application which consumes Rest services... DTOs, a lot of DTOs to map the received data. Now, what's happening with our services written in C# and GO? These cannot use the same classes and need to be rewritten.
We will finish writing our code N times to get it into the equivalent ones. This means time, maintenance, and possible errors. Is this unusual? Not at all, it is more common than you think.
Not everything is lost, there is something called AST. Basically, a tree representation of the source code which is easily navigated by accessing nodes.
What can we do with an AST? / What is an AST good for?
· Code static analysis
· Code transformation/transpilation
Using the TypeScript compiler is possible to get the corresponding AST:
Now we need to look into for the desired nodes and generate the new source code. If we analyze this file index.ts :
The AST will be:
We can easily walk through the nodes with a recursive function ( node.forEachChild)
This is a tool developed in TypeScript and NodeJS that brings the possibility to convert source code written in TypeScript into different languages (currentlyC# and Go). It can migrate either simple complex structures. Supporting from a basic enumerate or constant to a class with inheritance and interfaces In a close future support to transform simple logics within methods will be added. By the mean time only supports the method signature.
The AST is a powerful tool to help in code transformation - and using Codeverter (or another similar tool) you can save significant amounts of time and increase your productivity by avoiding the rewrite process for each class or interface N times.
For example, assuming that to re-write a simple DTO (having in mind code guidelines and so on) takes about a minute, and let’s say you have 20 DTOs in a project - You have saved 20 minutes! Enough to make a coffee!
Public repository: github.com/southworks/codeverter
NPM package: npmjs.com/package/@southworks/codeverter