What is an AST transformation in general? I came across these words when reading Groovy blog's. But what it is in general?
AST means Abstract Syntax Tree, which is basically an abstract representation of code / any syntactic structure. A transformation is an action modifying this tree (i.e. transforming the existing AST to a new AST). For more information have a look here: http://en.wikipedia.org/wiki/Abstract_syntax_tree
In addition to what have been mentioned already, you might also be interested in a broader and more fundamental concept of Term rewriting.
The simple answer is any function that converts one AST, into another AST.
A more sophisticated view can be found in my SO answer on Model-driven development: What is a transform?
AST is a tree representation of the abstract syntactic structure of source code written in a programming language.
When there is a need to transform code changing it parts usually transformer
operates with tree representation of source code to find the node which requires changes using Visitor Pattern and to apply this changes.
For example putout code transformer for JavaScript supports direct manipulation with AST
tree this way:
const putout = require('putout');
const removeDebugger = {
report: () => 'debugger should not be used',
fix: (path) => {
path.remove();
},
traverse: ({push}) = ({
'DebuggerStatement': (path) => {
push(path);
}
}),
};
putout('const a = 5; debugger', {
fix: true,
plugins: [
['remove-debugger', removeDebugger]
]
});
// returns
({
code: 'const a = 5;',
places: [],
});
Anyways there is much simpler way to manipulate with AST used in @putout/plugin-remove-debugger:
const removeDebugger = {
report: () => 'debugger should not be used',
replace: () = ({
'debugger': ''
}),
};
In this example one expression replaced with another using templates language of @putout/engine-runner that helps to write simple code transformation without touching AST
at all.
Worth mention that inside of replace
transformation anyways used AST
because it's the most powerful way of manipulation with source code.
© 2022 - 2024 — McMap. All rights reserved.