What is recast?
The recast npm package is a JavaScript library for parsing, transforming, and printing JavaScript code. It allows developers to manipulate the syntax tree of JavaScript code, enabling tasks such as code refactoring, code generation, and more. Recast preserves the original formatting of the code as much as possible, which is useful when modifying existing code.
What are recast's main functionalities?
Parsing JavaScript code into an Abstract Syntax Tree (AST)
This feature allows you to parse a string of JavaScript code into an AST, which can then be manipulated or analyzed.
const recast = require('recast');
const code = 'let x = 42;';
const ast = recast.parse(code);
Transforming the AST
This feature enables you to traverse and modify the AST. In this example, all 'let' declarations are changed to 'var'.
const recast = require('recast');
const ast = recast.parse('let x = 42;');
recast.types.visit(ast, {
visitVariableDeclaration(path) {
path.node.kind = 'var';
return false;
}
});
const transformedCode = recast.print(ast).code;
Printing the modified AST back to JavaScript code
After modifying the AST, this feature allows you to print it back into a formatted JavaScript code string.
const recast = require('recast');
const ast = recast.parse('let x = 42;');
// ... modify the AST ...
const modifiedCode = recast.print(ast).code;
Other packages similar to recast
babel
Babel is a widely-used JavaScript compiler that allows you to transform your JavaScript code using various plugins. It can parse and transform modern JavaScript features into a format compatible with older browsers. Babel's plugin system is more extensive than recast, and it is often used for compiling next-gen JavaScript features down to current standards.
esprima
Esprima is a JavaScript parser that produces an AST for JavaScript code. It is similar to recast in that it can be used for static code analysis and manipulation. However, unlike recast, Esprima does not focus on preserving the original code formatting.
jscodeshift
jscodeshift is a toolkit for running codemods over multiple JavaScript or TypeScript files. It uses recast and babel under the hood for parsing and printing, but provides a higher-level API for transforming code, making it more accessible for writing complex codemods.
recast, v.
- to give (a metal object) a different form by melting it down and reshaping it.
- to form, fashion, or arrange again.
- to remodel or reconstruct (a literary work, document, sentence, etc.).
- to supply (a theater or opera work) with a new cast.
The more code you have, the harder it becomes to make big, sweeping changes quickly and confidently. Even if you trust yourself not to make too many mistakes, and no matter how proficient you are with your text editor, changing tens of thousands of lines of code takes precious, non-refundable time.
Is there a better way? Not always! When a task requires you to alter the semantics of many different pieces of code in subtly different ways, your brain inevitably becomes the bottleneck, and there is little hope of completely automating the process. Your best bet is to plan carefully, buckle down, and get it right the first time. Love it or loathe it, that's the way programming goes sometimes.
What I hope to eliminate are the brain-wasting tasks, the tasks that are bottlenecked by keystrokes, the tasks that can be expressed as operations on the syntactic structure of your code. Specifically, my goal is to make it possible for you to run your code through a parser, manipulate the abstract syntax tree directly, subject only to the constraints of your imagination, and then automatically translate those modifications back into source code, without upsetting the formatting of unmodified code.
And here's the best part: when you're done running a Recast script, if you're not completely satisfied with the results, blow them away with git reset --hard
, tweak the script, and just run it again. Change your mind as many times as you like. Instead of typing yourself into a nasty case of RSI, gaze upon your new wells of free time and ask yourself: what now?
Though still under active development, Recast is ready for use with JavaScript today. There's nothing particularly JavaScript-specific about the ideas involved, but I write a lot of JS, and a good parser was readily available. If these ideas gain sufficient traction in the JavaScript community, more languages will undoubtedly follow.