小编Aft*_*han的帖子

Babel 插件(访客模式)——它是如何工作的

我想在我的 babel 插件中做两个替换。第二次更换只能在第一次更换完成后进行。

module.exports = function(babel) {
    const t = babel.types;
    return {
        visitor: {
            FunctionExpression: function(path) {
                //Conversion to arrow functions
                path.replaceWith(t.arrowFunctionExpression(path.node.params, path.node.body, false));
            },
            ThisExpression: function(path) {
                //Converting all this expressions to identifiers so that it won't get translated differently
                path.replaceWith(t.identifier("this"));
            }
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

在我的“FunctionExpression”的 AST 树中,“ThisExpression”存在于树下的某个位置。我希望仅在第二次转换完成后才进行第一次转换。我该如何实现这一点?

javascript visitor-pattern babeljs

4
推荐指数
1
解决办法
4839
查看次数

标签 统计

babeljs ×1

javascript ×1

visitor-pattern ×1