ast binding

 

https://evilrecluse.top/post/7389a59f/

 

 

 

 

 

Binding 对象用于存储 绑定 的信息
这个对象会作为Scope对象的一个属性存在
同一个作用域可以包含多个 Binding

你可以在 @babel/traverse/lib/scope/binding.js 中查看到它的定义

const parser = require("@babel/parser");
const traverse = require("@babel/traverse").default;

const jscode = `
function a(){
    var a = 1;
    a = a + 1;
    return a;
}
function b(){
    var b = 1;
    var c = 2;
    b = b - c;
    return b;
}

`;
let ast = parser.parse(jscode);
const visitor = {
    BlockStatement(path){
        console.log("\n此块节点源码:\n", path.toString())
        console.log('----------------------------------------')
        var bindings = path.scope.bindings
        console.log('作用域内 被绑定量 数量:', Object.keys(bindings).length)

        for(var binding_ in bindings){
            console.log('名字:', binding_)
            binding_ = bindings[binding_];
            console.log('类型:', binding_.kind)
            console.log('定义:', binding_.identifier)
            console.log('是否会被修改:', binding_.constant)
            console.log('被修改信息信息记录', binding_.constantViolations)
            console.log('是否会被引用:', binding_.referenced)
            console.log('被引用次数', binding_.references)
            console.log('被引用信息NodePath记录', binding_.referencePaths)
        }
    }
}

traverse(ast, visitor);
// let { code } = generator(ast);

// console.log(code);

 

posted @ 2021-12-28 21:57  AngDH  阅读(126)  评论(0编辑  收藏  举报