作家
登录

JavaScript语法树与代码转化实践

作者: 来源: 2017-07-27 09:55:49 阅读 我要评论

return
  •  
  • 我们也可以指定忽视遍历某个子路径:

    1. outerPath.traverse({ 
    2.   Function(innerPath) { 
    3.     innerPath.skip(); // if checking the children is irrelevant 
    4.   }, 
    5.   ReferencedIdentifier(innerPath, state) { 
    6.     state.iife = true
    7.     innerPath.stop(); // if you want to save some state and then stop traversal, or deopt 
    8.   } 
    9. });  

    操作

    • 调换节点
    1. BinaryExpression(path) { 
    2.   if (t.isIdentifier(path.node.left, { name"n" })) { 
    3.     // ... 
    4.   } 
    5.  
    6. // 等价于 
    7. BinaryExpression(path) { 
    8.   if ( 
    9.     path.node.left != null && 
    10.     path.node.left.type === "Identifier" && 
    11.     path.node.left.name === "n" 
    12.   ) { 
    13.     // ... 
    14.   } 
    15.  
    • 断定某个路径对应的节点是否为指定类型
    1. // 插件定义 
    2. BinaryExpression(path) { 
    3.   path.WordStrWith( 
    4.     t.binaryExpression("**", path.node.left, t.numberLiteral(2)) 
    5.   ); 
    6.  
    7. // 代码结不雅 
    8.   function square(n) { 
    9. -   return n * n; 
    10. +   return n ** 2; 
    11.   }  
    • 将某个节点调换为多个节点
    1. // 插件定义 
    2. ReturnStatement(path) { 
    3.   path.WordStrWithMultiple([ 
    4.     t.expressionStatement(t.stringLiteral("Is this the real life?")), 
    5.     t.expressionStatement(t.stringLiteral("Is this just fantasy?")), 
    6.     t.expressionStatement(t.stringLiteral("(Enjoy singing the rest of the song in your head)"

        推荐阅读

        海南常见罪名量刑细则实施 人工智能助力司法改革

      2009年6月,我省开端量刑规范化改革,是全国第二批试点省份。据介绍,海南最新版的量刑细则涵盖了23个常见着绫躯和罚金刑、缓刑实用,所涉的刑事犯法案件占到我省刑事案件的95%以上。此次>>>详细阅读


      本文标题:JavaScript语法树与代码转化实践

      地址:http://www.17bianji.com/lsqh/36429.html

    关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)