简体   繁体   中英

What is an AST transformation?

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 (ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM