其他分享
首页 > 其他分享> > 逗号表达式

逗号表达式

作者:互联网

 

 

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

let jscode = `

function test2(a,b,c,d,e,f){
    return f = (e = (d = (c = (b = (a = 1000,a+50,b+60,c+70,a+2000), d+ 80,b+3000),e+90,c+4000),f+100,d+5000),e+6000);

}
console.log(test2());
`;

let ast = parse(jscode);


const visitor =
{
    SequenceExpression: {
        exit(path) {
            let expressions = path.node.expressions;
            let finalExpression = expressions.pop();
            let statement = path.getStatementParent();
            expressions.map(function (v) {
                statement.insertBefore(types.ExpressionStatement(v));
            });
            path.replaceInline(finalExpression);
        },
    }

}



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

 

标签:const,babel,require,逗号,let,path,expressions,表达式
来源: https://www.cnblogs.com/angdh/p/15780774.html