大蟒蛇python教程共享Babel 插件开发&访问节点实例详解

访问节点

获取子节点的path:

我们在处理节点的属性之前必须要拿到节点对象才能进行操作,我们使用path.node.property来访问属性~

binaryexpression(path) {    path.node.left;    path.node.right;    path.node.operator;  }  

我们还可以使用 path 内置的 get 函数来指定属性名获取属性值~

binaryexpression(path) {    path.get('left');  }  program(path) {    path.get('body.0');  }  

检查节点的类型:

检查节点的类型我们可以使用内置的工具类函数isxxx()~

binaryexpression(path) {    if (t.isidentifier(path.node.left)) {      // ...    }  }  

我们在检查类型的时候还可以顺便检查其中的某些属性是否达到预期~

binaryexpression(path) {    if (t.isidentifier(path.node.left, { name: "n" })) {      // ...    }  }  // 简化前的代码  binaryexpression(path) {    if (      path.node.left != null &&      path.node.left.type === "identifier" &&      path.node.left.name === "n"    ) {      // ...    }  }  

检查路径(path)类型:

路径具有相同的方法检查节点的类型~

binaryexpression(path) {    if (path.get('left').isidentifier({ name: "n" })) {      // ...    }  }  // 等价于  binaryexpression(path) {    if (t.isidentifier(path.node.left, { name: "n" })) {      // ...    }  }  

检查标识符(identifier)是否被引用:

identifier(path) {    if (path.isreferencedidentifier()) {      // ...    }  }  // 或者  identifier(path) {    if (t.isreferenced(path.node, path.parent)) {      // ...    }  }  

找到特定的父路径:

向上查找特定节点可以使用~

path.findparent((path) => path.isobjectexpression());  

如果也需要遍历当前节点~

path.find((path) => path.isobjectexpression());  

查找最接近的父函数或程序~

path.getfunctionparent();  

向上遍历语法树,直到找到在列表中的父节点路径~

path.getstatementparent();  

获取同级路径:

如果一个路径是在一个 functionprogram中的列表里面,它就有同级节点。

  • 使用path.inlist来判断路径是否有同级节点,
  • 使用path.getsibling(index)来获得同级路径,
  • 使用 path.key获取路径所在容器的索引,
  • 使用 path.container获取路径的容器(包含所有同级节点的数组)
  • 使用 path.listkey获取容器的key

这些api用于 babel-minify 中使用的 transform-merge-sibling-variables 插件.

var a = 1; // patha, path.key = 0  var b = 2; // pathb, path.key = 1  var c = 3; // pathc, path.key = 2  
export default function({ types: t }) {    return {      visitor: {        variabledeclaration(path) {          // if the current path is patha          path.inlist // true          path.listkey // "body"          path.key // 0          path.getsibling(0) // patha          path.getsibling(path.key + 1) // pathb          path.container // [patha, pathb, pathc]        }      }    };  }  

停止遍历:

当我们遍历完成目的后应该尽早结束而不是继续遍历下去~

binaryexpression(path) {    if (path.node.operator !== '**') return;  }  

如果您在顶级路径中进行子遍历,则可以使用2个提供的api方法~

path.skip()跳过遍历当前路径的子路径~

path.stop()完全停止遍历~

outerpath.traverse({    function(innerpath) {      innerpath.skip(); // if checking the children is irrelevant    },    referencedidentifier(innerpath, state) {      state.iife = true;      innerpath.stop(); // if you want to save some state and then stop traversal, or deopt    }  });  

以上就是babel 插件开发&amp;访问节点实例详解的详细内容,更多关于babel 插件开发访问节点的资料请关注<计算机技术网(www.ctvol.com)!!>其它相关文章!

需要了解更多python教程分享Babel 插件开发&访问节点实例详解,都可以关注python教程分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/pythontutorial/1235115.html

(0)
上一篇 2022年9月1日
下一篇 2022年9月1日

精彩推荐