Codeverter: The open source code translation tool from SOUTHWORKS

Introducing Codeverter

Authored by Agustin Seifert, Principle Software Engineer at SOUTHWORKS

The problem:

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 all superheroes wear capes

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?

·      Linting

·      Code static analysis

·      Code transformation/transpilation

 

For example, Typescript code is transpiled toJavaScript. How? The source code is parsed into an AST that is converted into the JavaScript equivalent one. So, if we can generate JavaScript, what about another language?

 

Get down to work

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)

Let's pack it into a tool, Codeverter

Codeverter

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.

Summary

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!

  

Check out SOUTHWORKS Codeverter here:

Public repository: github.com/southworks/codeverter

Playground: southworks.github.io/codeverter/

NPM package: npmjs.com/package/@southworks/codeverter

--

Special thanks to SOUTHIES Agustin Seifert, Micaela Ramirez, Jesus Caceres and Santiago Calle for their awesome work on this project.

SOUTHWORKS Dev Team

Software developers, engineers, coders and self-confessed tech nerds

SOUTHWORKS software engineers from across the globe share their latest musings.

Latest posts