AI Generated Video Summary
Today, we are going to be talking about compilation, or I should say transpilation. In fact, when talking about compilation, we refer to that process that takes a source code as an input and produces binary code or bytecode as an output. The whole compilation phase takes place in three different steps. Parsing, code transformation, and code generation.
2. Understanding Abstract Syntax Tree Traversal
If we were writing like foo equals true, we would have wrote down the boolean literal instead of the numerical one. When reading an abstract syntax tree, we call this process traversing the tree. With Babel, we can transform variable declarations from var to let. The same concept applies to other tools like Prettier, ESLint, Babel, GIS, CodeShift. With ESLint, we can report and fix errors related to template literals.
If we were writing like foo equals true, we would have wrote down the boolean literal instead of the numerical one, or strings or whatever, just for you to understand. While it's quite easy to understand this tree, I guess, this one, it's a bit more challenging to deal with.
When reading an abstract syntax tree, we call this process traversing the tree. It's quite complex on, of course, complex programs. But luckily, many tools such as Babel, ESLint, Pretier, provides us a nice interface for traversing the tree which is called the visitor pattern.
In that case, with Babel, we're saying, okay, dear Babel, please give me all the variable declarations or the identifiers, all the numerical literals, and we just print them to the console. Once we get, for example, all the variable declarations, we can start transforming them. So that's the case. We say, okay, please give me all the variable declarations inside of our code. And if the kind of the variable is a var, just transform it into a let, which is not wise. I'm not suggesting you're doing that, but it's a very straightforward example. So that makes things easier for us. So that said, on the left, we have our input var foo equals 10. And on the right, we have our output let foo equals 10. As you can see, on line two, we have constant and we leave it untouched because it's not a variable, it's a, it's a const. So as you can see on the code, we are not saying if it's constant, please change it to let. So we are not doing that. As I said, the same concept applies to other tools, such as Prettier, ESLint, Babel, GIS, CodeShift. And we're gonna see them in details right now.
With ESLint, we have a similar approach. We say, okay, please, ESLint, give me all the template literals you can find in my code. And if I find one, I can report to ESLint an error, such as please do not use template literals. And I can also provide a fix for it. But in that case, if I have an expression inside my template literals, I can say, okay, I'm too lazy for that. So I'm just returning, I'm not fixing this. But if I don't have an expression, I can say, okay, so I don't have expression. I don't have any expression. So no dynamic data inside my template literals. So just change the literals into double quotes. So that's what we are doing now.
3. Code Transformation Tools and Techniques
JS CodeShift is a powerful library built and maintained by Facebook for large-scale refactors. It allows you to find and replace identifiers, making it easier to refactor code. Another useful tool is the T-TypeScript plugin, which enables transformations like performing operations at compile time instead of runtime. By understanding abstract syntax trees and implementing the visitor pattern, you can achieve powerful code transformations. Tools like Babel, TSLint, YASLint, Recast, Pretier, and the website astexplorer.net can help you in this process. Thank you all for being here and feel free to reach out to me for further discussions on code refactoring and transformations.
And on the left, the input on the right, the output. JS CodeShift, it's another awesome example. It's a library built by Facebook and maintained by Facebook itself. And it's meant for big refactors, large scale refactors. And the concept is quite similar.
Here, we are saying, okay, please JS CodeShift, find all the identifiers. And if the identifier names contains the old name string, replace it with new name string. This is the case again for large-scale refactors. So we have like old name factory, which get refactored into new name factory.
And the same again applies with Babel, TSLint, YASLint, Recast, Pretier, JS CodeShift, and there's also an amazing website, which is called astexplorer.net that can really help you debugging your formulas and try to learn more about how to traverse a tree. So I really wanna thank you all for being here. It has been a real pleasure to talk at Node Congress, and these are my social media handlers, so if you want to reach out, I'd be glad to talk with you again about how to refactor code bases and making code transformations. That's what I love to do and to talk about. So thank you again.