逗号表达式

 

 

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);

 

posted @ 2022-01-09 13:21  AngDH  阅读(68)  评论(0编辑  收藏  举报