运算结果

 

https://www.jianshu.com/p/a3857fa5c899

 

 

const fs = require('fs');
const { parse } = require("@babel/parser");
const traverse = require("@babel/traverse").default;
const t = require("@babel/types");
const generator = require("@babel/generator").default;

let jscode = `

var a = "Hello,AST"["charCodeAt"](5);

`;

let ast = parse(jscode);



const visitor = {
    "MemberExpression"(path)
    {
        let property = path.get('property');
        if (property.isStringLiteral())
        {
            let value = property.node.value;
            path.node.computed = false;
            property.replaceWith(t.Identifier(value));
        }
    },
    "CallExpression":{
        exit:function(path)
        {
            const {confident,value} = path.evaluate();
            confident && path.replaceInline(t.valueToNode(value));
        }
    },
}




traverse(ast, visitor);
let { code } = generator(ast);
console.log(code);

 

posted @ 2022-01-07 20:33  AngDH  阅读(58)  评论(0编辑  收藏  举报