Table of Contents:
  1. \@babel|types使用教程
    1. 资源
      1. 说明
        1. 常用的API
          1. t.valueToNode
        2. 常用的节点类型
          1. numericLiteral(value)
          2. identifier(name)
          3. ifStatement(test, consequent, alternate)
          4. blockStatement(body, directives)
          5. switchStatement(discriminant, cases)
          6. switchCase(test, consequent)
          7. conditionalExpression(test, consequent, alternate)
          8. 声明语句节点
          9. 表达式节点

      \@babel|types使用教程

      Reading Time:The full text has 961 words, estimated reading time: 5 minutes
      Creation Date:2022-08-10
      Article Tags:
      Previous Article:\@babel|traverse使用教程
       
      BEGIN

      资源

      官方文档:https://babeljs.io/docs/babel-types 🔗

      说明

      @babel/types这个包对于搞TypeScript肯定熟悉,写ts肯定会引入一个types的包,用到的类型定义都是从这个包导出来,@babel/types除了提供类型外还提供了对应的检测方法与构造方法。

      包引入:const t = require("@babel/types");

      在使用babel做逆向代码分析时,这个包也是非常重要的,但是官方文档会让人很懵逼,比如:

      anyTypeAnnotation
      
      t.anyTypeAnnotation();
      
      See also t.isAnyTypeAnnotation(node, opts) and t.assertAnyTypeAnnotation(node, opts).
      Aliases: Flow, FlowType, FlowBaseAnnotation

      这是官方文档的第一个api,我们需要关注三个单词:

      1. anyTypeAnnotation: 类型构造方法
      2. isAnyTypeAnnotation: 类型判断方法,通过对比type属性值,如node.type === 'AnyTypeAnnotation'
      3. assertAnyTypeAnnotation: 激进的类型判断方法

      还有一个没写的该类型关键词制药把构造方法首字母大写即可,即:AnyTypeAnnotation

      其中t.is...(node, opts)t.assert...(node, opts)中入参opts可以不用管,如果传入opts则表示对比opts的属性键与node的同名键的值是否一致,全一致才会返回true,因此基本上不会用到

      常用的API

      t.valueToNode

      该方法传入基本数据类型的值并返回对应的node节点,如:

      > types.valueToNode(1)
      { type: 'NumericLiteral', value: 1 }
      > types.valueToNode('1')
      { type: 'StringLiteral', value: '1' }
      > types.valueToNode(['1'])
      {
        type: 'ArrayExpression',
        elements: [ { type: 'StringLiteral', value: '1' } ]
      }

      常用的节点类型

      首字母小写是构造方法,首字母大写是数据类型

      numericLiteral(value)

      构建数字类型字面量节点

      • value: 数字字面量

      identifier(name)

      构建变量名标识符或函数名标识符节点

      • name: 类型string,标识符字面量(字符串)

      ifStatement(test, consequent, alternate)

      构建if声明语句节点

      • test: 表示条件表达式的 AST 节点。
      • consequent: 表示条件为真时执行的语句块的 AST 节点(如果是单个语句,可以是一个表达式语句)。
      • alternate: 表示条件为假时执行的语句块的 AST 节点(如果没有,可以为 null)

      blockStatement(body, directives)

      构建代码块节点,用大括号包起来的部分

      • body: 类型Array<Statement>,代码体节点数组
      • directives: 类型Array<Directive | DirectiveLiteral>,代码头部的指令集合,如:use strict

      switchStatement(discriminant, cases)

      构建switch语句的ast节点

      • discriminant: 类型Expression,表示 switch 语句中的判别表达式的 AST 节点。
      • cases: 类型Array<SwitchCase>,表示 switch 语句中的 case 语句的数组。每个 case 是一个 SwitchCase 节点,其中包含了 test 表达式和 consequent 语句块。

      switchCase(test, consequent)

      构建switch的case语句的ast节点

      • test: 表示 case 的测试表达式的 AST 节点,当判别表达式等于这个表达式时,执行相应的语句块。如果为 null,则表示这是一个默认的 case, 即default。
      • consequent: 表示与该 case 关联的语句块的数组。

      conditionalExpression(test, consequent, alternate)

      构建三元运算符的ast节点

      • test: 表示条件表达式的 AST 节点。
      • consequent: 表示条件为真时执行的语句块的 AST 节点(如果是单个语句,可以是一个表达式语句)。
      • alternate: 表示条件为假时执行的语句块的 AST 节点(如果没有,可以为 null)

      声明语句节点

      breakStatement(label)

      构建break语句的ast节点

      • label: 类型Identifier

      expressionStatement(expression)

      构建可执行语句的ast节点

      • expression: 类型Expression,可执行表达式节点

      returnStatement(argument)

      构建return语句的ast节点

      • argument: 要返回的表达式节点

      表达式节点

      binaryExpression(operator, left, right)

      构建二进制表达式节点

      • operator: 操作运算符符
      • left: 左节点
      • right: 右节点

      assignmentExpression(operator, left, right)

      构建赋值表达式节点

      • operator: 表示赋值操作符的字符串,例如 =、+=、-= 等。
      • left: 表示被赋值的目标的 AST 节点,可以是标识符、成员表达式等。
      • right: 表示赋给目标的值的 AST 节点。

      sequenceExpression(expressions)

      构建逗号表达式节点

      • expressions: 其它可执行表达式节点的集合
      FINISH
      Previous Article:\@babel|traverse使用教程

      Random Articles
      Life Countdown
      default