当前位置:   article > 正文

超级详细AST抽象语法树Javascript_switchstatement ast

switchstatement ast

AST 抽象语法树

Program 程序(开始)
interface Program <: Node {
    type: "Program";
    body: [ Statement ];
}
  • 1
  • 2
  • 3
  • 4
1. statement ——语句
1)Statement —— 表示任意语句
interface Statement <: Node { }
  • 1
2)EmptyStatement ——表示空语句,即分号;
interface EmptyStatement <: Statement {
    type: "EmptyStatement";
}
  • 1
  • 2
  • 3
3)BlockStatement ——表示块语句,由{}包围的语句
interface BlockStatement <: Statement {
    type: "BlockStatement";
    body: [ Statement ];
}
  • 1
  • 2
  • 3
  • 4
4)ExpressionStatement —— 表示表达式语句,由单个表达式组成的语句
interface ExpressionStatement <: Statement {
    type: "ExpressionStatement";
    expression: Expression;
}
  • 1
  • 2
  • 3
  • 4

例如:

alert(1)
// 转成AST
{
  "type": "Program",
  "start": 0,
  "end": 8,
  "body": [
    {
      "type": "ExpressionStatement",
      "start": 0,
      "end": 8,
      "expression": {
        "type": "CallExpression",
        "start": 0,
        "end": 8,
        "callee": {
          "type": "Identifier",
          "start": 0,
          "end": 5,
          "name": "alert"
        },
        "arguments": [
          {
            "type": "Literal",
            "start": 6,
            "end": 7,
            "value": 1,
            "raw": "1"
          }
        ],
        "optional": false
      }
    }
  ],
  "sourceType": "module"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
5)IfStatement —— 表示If语句
interface IfStatement <: Statement {
    type: "IfStatement";
    test: Expression;
    consequent: Statement;
    alternate: Statement | null;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

例如:

if (a || b) {
	alert(1);
} else {
	alert(2);
}
/**
* if  IfStatement
* a || b  test  LogicalExpression
* {}  consequent  BlockStatement
* else alternate  BlockStatement
*/
// 转成AST如下
{
  "type": "Program",
  "start": 0,
  "end": 48,
  "body": [
    {
      "type": "IfStatement",
      "start": 1,
      "end": 47,
      "test": {
        "type": "LogicalExpression",
        "start": 5,
        "end": 11,
        "left": {
          "type": "Identifier",
          "start": 5,
          "end": 6,
          "name": "a"
        },
        "operator": "||",
        "right": {
          "type": "Identifier",
          "start": 10,
          "end": 11,
          "name": "b"
        }
      },
      "consequent": {
        "type": "BlockStatement",
        "start": 13,
        "end": 27,
        "body": [
          {
            "type": "ExpressionStatement",
            "start": 16,
            "end": 25,
            "expression": {
              "type": "CallExpression",
              "start": 16,
              "end": 24,
              "callee": {
                "type": "Identifier",
                "start": 16,
                "end": 21,
                "name": "alert"
              },
              "arguments": [
                {
                  "type": "Literal",
                  "start": 22,
                  "end": 23,
                  "value": 1,
                  "raw": "1"
                }
              ],
              "optional": false
            }
          }
        ]
      },
      "alternate": {
        "type": "BlockStatement",
        "start": 33,
        "end": 47,
        "body": [
          {
            "type": "ExpressionStatement",
            "start": 36,
            "end": 45,
            "expression": {
              "type": "CallExpression",
              "start": 36,
              "end": 44,
              "callee": {
                "type": "Identifier",
                "start": 36,
                "end": 41,
                "name": "alert"
              },
              "arguments": [
                {
                  "type": "Literal",
                  "start": 42,
                  "end": 43,
                  "value": 2,
                  "raw": "2"
                }
              ],
              "optional": false
            }
          }
        ]
      }
    }
  ],
  "sourceType": "module"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
6)LabeledStatement —— 表示标签语句,以break/continue开头的标签语句
interface LabeledStatement <: Statement {
    type: "LabeledStatement";
    label: Identifier;
    body: Statement;
}
  • 1
  • 2
  • 3
  • 4
  • 5
7)BreakStatement —— 表示一个break语句
interface BreakStatement <: Statement {
    type: "BreakStatement";
    label: Identifier | null;
}
  • 1
  • 2
  • 3
  • 4
8)ContinueStatement —— 表示一个continue语句
interface ContinueStatement <: Statement {
    type: "ContinueStatement";
    label: Identifier | null;
}
  • 1
  • 2
  • 3
  • 4
9)WithStatement —— 表示一个 with语句
interface WithStatement <: Statement {
    type: "WithStatement";
    object: Expression;
    body: Statement;
}
  • 1
  • 2
  • 3
  • 4
  • 5
10)SwitchStatement —— 表示一个switch语句
interface SwitchStatement <: Statement {
    type: "SwitchStatement";
    discriminant: Expression;
    cases: [ SwitchCase ];
    lexical: boolean;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
11)ReturnStatement —— 表示一个return语句
interface ReturnStatement <: Statement {
    type: "ReturnStatement";
    argument: Expression | null;
}
  • 1
  • 2
  • 3
  • 4
12)ThrowStatement —— 表示一个throw语句
interface ThrowStatement <: Statement {
    type: "ThrowStatement";
    argument: Expression;
}
  • 1
  • 2
  • 3
  • 4
13)TryStatement —— 表示一个try语句
interface TryStatement <: Statement {
    type: "TryStatement";
    block: BlockStatement;
    handler: CatchClause | null;
    guardedHandlers: [ CatchClause ];
    finalizer: BlockStatement | null;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
14)WhileStatement —— 表示一个while语句
interface WhileStatement <: Statement {
    type: "WhileStatement";
    test: Expression;
    body: Statement;
}
  • 1
  • 2
  • 3
  • 4
  • 5
15)DoWhileStatement —— 表示一个do/while语句
interface DoWhileStatement <: Statement {
    type: "DoWhileStatement";
    body: Statement;
    test: Expression;
}
  • 1
  • 2
  • 3
  • 4
  • 5
16)ForStatement —— 表示一个for语句
interface ForStatement <: Statement {
    type: "ForStatement";
    init: VariableDeclaration | Expression | null;
    test: Expression | null;
    update: Expression | null;
    body: Statement;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

例如:

for (var i = 0; i < 10; i++) {
	console.log(i);
}
// 转成AST
{
  "type": "Program",
  "start": 0,
  "end": 49,
  "body": [
    {
      "type": "ForStatement",
      "start": 0,
      "end": 49,
      "init": {
        "type": "VariableDeclaration",
        "start": 5,
        "end": 14,
        "declarations": [
          {
            "type": "VariableDeclarator",
            "start": 9,
            "end": 14,
            "id": {
              "type": "Identifier",
              "start": 9,
              "end": 10,
              "name": "i"
            },
            "init": {
              "type": "Literal",
              "start": 13,
              "end": 14,
              "value": 0,
              "raw": "0"
            }
          }
        ],
        "kind": "var"
      },
      "test": {
        "type": "BinaryExpression",
        "start": 16,
        "end": 22,
        "left": {
          "type": "Identifier",
          "start": 16,
          "end": 17,
          "name": "i"
        },
        "operator": "<",
        "right": {
          "type": "Literal",
          "start": 20,
          "end": 22,
          "value": 10,
          "raw": "10"
        }
      },
      "update": {
        "type": "UpdateExpression",
        "start": 24,
        "end": 27,
        "operator": "++",
        "prefix": false,
        "argument": {
          "type": "Identifier",
          "start": 24,
          "end": 25,
          "name": "i"
        }
      },
      "body": {
        "type": "BlockStatement",
        "start": 29,
        "end": 49,
        "body": [
          {
            "type": "ExpressionStatement",
            "start": 32,
            "end": 47,
            "expression": {
              "type": "CallExpression",
              "start": 32,
              "end": 46,
              "callee": {
                "type": "MemberExpression",
                "start": 32,
                "end": 43,
                "object": {
                  "type": "Identifier",
                  "start": 32,
                  "end": 39,
                  "name": "console"
                },
                "property": {
                  "type": "Identifier",
                  "start": 40,
                  "end": 43,
                  "name": "log"
                },
                "computed": false,
                "optional": false
              },
              "arguments": [
                {
                  "type": "Identifier",
                  "start": 44,
                  "end": 45,
                  "name": "i"
                }
              ],
              "optional": false
            }
          }
        ]
      }
    }
  ],
  "sourceType": "module"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
17)ForInStatement —— 表示一个for/in语句
interface ForInStatement <: Statement {
    type: "ForInStatement";
    left: VariableDeclaration |  Expression;
    right: Expression;
    body: Statement;
    each: boolean;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
18)ForOfStatement —— 表示一个for/of语句
interface ForOfStatement <: Statement {
    type: "ForOfStatement";
    left: VariableDeclaration |  Expression;
    right: Expression;
    body: Statement;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

例如:

const iterable = ['mini', 'mani', 'mo'];
for (const value of iterable) {
  console.log(value);
}

// 转成AST
{
  "type": "Program",
  "start": 0,
  "end": 98,
  "body": [
    {
      "type": "VariableDeclaration",
      "start": 0,
      "end": 40,
      "declarations": [
        {
          "type": "VariableDeclarator",
          "start": 6,
          "end": 39,
          "id": {
            "type": "Identifier",
            "start": 6,
            "end": 14,
            "name": "iterable"
          },
          "init": {
            "type": "ArrayExpression",
            "start": 17,
            "end": 39,
            "elements": [
              {
                "type": "Literal",
                "start": 18,
                "end": 24,
                "value": "mini",
                "raw": "'mini'"
              },
              {
                "type": "Literal",
                "start": 26,
                "end": 32,
                "value": "mani",
                "raw": "'mani'"
              },
              {
                "type": "Literal",
                "start": 34,
                "end": 38,
                "value": "mo",
                "raw": "'mo'"
              }
            ]
          }
        }
      ],
      "kind": "const"
    },
    {
      "type": "ForOfStatement",
      "start": 43,
      "end": 98,
      "await": false,
      "left": {
        "type": "VariableDeclaration",
        "start": 48,
        "end": 59,
        "declarations": [
          {
            "type": "VariableDeclarator",
            "start": 54,
            "end": 59,
            "id": {
              "type": "Identifier",
              "start": 54,
              "end": 59,
              "name": "value"
            },
            "init": null
          }
        ],
        "kind": "const"
      },
      "right": {
        "type": "Identifier",
        "start": 63,
        "end": 71,
        "name": "iterable"
      },
      "body": {
        "type": "BlockStatement",
        "start": 73,
        "end": 98,
        "body": [
          {
            "type": "ExpressionStatement",
            "start": 77,
            "end": 96,
            "expression": {
              "type": "CallExpression",
              "start": 77,
              "end": 95,
              "callee": {
                "type": "MemberExpression",
                "start": 77,
                "end": 88,
                "object": {
                  "type": "Identifier",
                  "start": 77,
                  "end": 84,
                  "name": "console"
                },
                "property": {
                  "type": "Identifier",
                  "start": 85,
                  "end": 88,
                  "name": "log"
                },
                "computed": false,
                "optional": false
              },
              "arguments": [
                {
                  "type": "Identifier",
                  "start": 89,
                  "end": 94,
                  "name": "value"
                }
              ],
              "optional": false
            }
          }
        ]
      }
    }
  ],
  "sourceType": "module"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
19)LetStatement —— 表示let语句
interface LetStatement <: Statement {
    type: "LetStatement";
    head: [ VariableDeclarator ];
    body: Statement;
}
  • 1
  • 2
  • 3
  • 4
  • 5
20)DebuggerStatement —— 表示一个debugger语句
interface DebuggerStatement <: Statement {
    type: "DebuggerStatement";
}
  • 1
  • 2
  • 3
Declarations 声明
1)Declaration —— 表示任何一条声明
interface Declaration <: Statement { }
  • 1
2)FunctionDeclaration —— 表示一个函数声明 id字段不为null
interface FunctionDeclaration <: Function, Declaration {
    type: "FunctionDeclaration";
    id: Identifier;
    params: [ Pattern ];
    defaults: [ Expression ];
    rest: Identifier | null;
    body: BlockStatement | Expression;
    generator: boolean;
    expression: boolean;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
3)VariableDeclaration —— 表示变量声明,let var const
interface VariableDeclaration <: Declaration {
    type: "VariableDeclaration";
    declarations: [ VariableDeclarator ];
    kind: "var" | "let" | "const";
}
  • 1
  • 2
  • 3
  • 4
  • 5

id字段不能为null.
let和const是SpiderMonkey特有的.

6)VariableDeclarator —— 表示变量声明符
interface VariableDeclarator <: Node {
    type: "VariableDeclarator";
    id: Pattern;
    init: Expression | null;
}
  • 1
  • 2
  • 3
  • 4
  • 5
Expression 表达式
interface Expression <: Node, Pattern { }
  • 1

任意表达式

1)ThisExpression —— 表示一个this表达式
interface ThisExpression <: Expression {
    type: "ThisExpression";
}
  • 1
  • 2
  • 3
2)ArrayExpression —— 表示一个数组表达式
interface ArrayExpression <: Expression {
    type: "ArrayExpression";
    elements: [ Expression | null ];
}
  • 1
  • 2
  • 3
  • 4

例如:

var arr = [1, 2];
// 转成AST
{
  "type": "Program",
  "start": 0,
  "end": 20,
  "body": [
    {
      "type": "VariableDeclaration",
      "start": 2,
      "end": 19,
      "declarations": [
        {
          "type": "VariableDeclarator",
          "start": 6,
          "end": 18,
          "id": {
            "type": "Identifier",
            "start": 6,
            "end": 9,
            "name": "arr"
          },
          "init": {
            "type": "ArrayExpression",
            "start": 12,
            "end": 18,
            "elements": [
              {
                "type": "Literal",
                "start": 13,
                "end": 14,
                "value": 1,
                "raw": "1"
              },
              {
                "type": "Literal",
                "start": 16,
                "end": 17,
                "value": 2,
                "raw": "2"
              }
            ]
          }
        }
      ],
      "kind": "var"
    }
  ],
  "sourceType": "module"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
3)ObjectExpression —— 表示一个对象表达式
interface ObjectExpression <: Expression {
    type: "ObjectExpression";
    properties: [ { key: Literal | Identifier,
                    value: Expression,
                    kind: "init" | "get" | "set" } ];
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
var foo = { a: 1 };

类似结构:

{
  "type": "ObjectExpression",
  "properties": [
    {
      "type": "ObjectProperty",
      "key": {
        "type": "Identifier",
        "name": "a"
      },
      "value": {
        "type": "NumericLiteral",
        "value": 1
      }
    }
  ]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
4)FunctionExpression —— 表示一个函数表达式
interface FunctionExpression <: Function, Expression {
    type: "FunctionExpression";
    id: Identifier | null;
    params: [ Pattern ];
    defaults: [ Expression ];
    rest: Identifier | null;
    body: BlockStatement | Expression;
    generator: boolean;
    expression: boolean;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

例如

var show= function () {
	return 1;
}
// 转成AST
{
  "type": "Program",
  "start": 0,
  "end": 37,
  "body": [
    {
      "type": "VariableDeclaration",
      "start": 0,
      "end": 36,
      "declarations": [
        {
          "type": "VariableDeclarator",
          "start": 4,
          "end": 36,
          "id": {
            "type": "Identifier",
            "start": 4,
            "end": 8,
            "name": "show"
          },
          "init": {
            "type": "FunctionExpression",
            "start": 10,
            "end": 36,
            "id": null,
            "expression": false,
            "generator": false,
            "async": false,
            "params": [],
            "body": {
              "type": "BlockStatement",
              "start": 22,
              "end": 36,
              "body": [
                {
                  "type": "ReturnStatement",
                  "start": 25,
                  "end": 34,
                  "argument": {
                    "type": "Literal",
                    "start": 32,
                    "end": 33,
                    "value": 1,
                    "raw": "1"
                  }
                }
              ]
            }
          }
        }
      ],
      "kind": "var"
    }
  ],
  "sourceType": "module"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
5)SequenceExpression —— 表示序列表达式,由逗号分隔
interface SequenceExpression <: Expression {
    type: "SequenceExpression";
    expressions: [ Expression ];
}
  • 1
  • 2
  • 3
  • 4

例如

function show(a = 0, b = 0) {
	return a, b;
}

// 转成AST
{
  "type": "Program",
  "start": 0,
  "end": 46,
  "body": [
    {
      "type": "FunctionDeclaration",
      "start": 0,
      "end": 45,
      "id": {
        "type": "Identifier",
        "start": 9,
        "end": 13,
        "name": "show"
      },
      "expression": false,
      "generator": false,
      "async": false,
      "params": [
        {
          "type": "AssignmentPattern",
          "start": 14,
          "end": 19,
          "left": {
            "type": "Identifier",
            "start": 14,
            "end": 15,
            "name": "a"
          },
          "right": {
            "type": "Literal",
            "start": 18,
            "end": 19,
            "value": 0,
            "raw": "0"
          }
        },
        {
          "type": "AssignmentPattern",
          "start": 21,
          "end": 26,
          "left": {
            "type": "Identifier",
            "start": 21,
            "end": 22,
            "name": "b"
          },
          "right": {
            "type": "Literal",
            "start": 25,
            "end": 26,
            "value": 0,
            "raw": "0"
          }
        }
      ],
      "body": {
        "type": "BlockStatement",
        "start": 28,
        "end": 45,
        "body": [
          {
            "type": "ReturnStatement",
            "start": 31,
            "end": 43,
            "argument": {
              "type": "SequenceExpression",
              "start": 38,
              "end": 42,
              "expressions": [
                {
                  "type": "Identifier",
                  "start": 38,
                  "end": 39,
                  "name": "a"
                },
                {
                  "type": "Identifier",
                  "start": 41,
                  "end": 42,
                  "name": "b"
                }
              ]
            }
          }
        ]
      }
    }
  ],
  "sourceType": "module"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
6)UnaryExpression
interface UnaryExpression <: Expression {
    type: "UnaryExpression";
    operator: UnaryOperator;
    prefix: boolean;
    argument: Expression;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
7)BinaryExpression —— 表示一个二元运算符表达式
interface BinaryExpression <: Expression {
    type: "BinaryExpression";
    operator: BinaryOperator;
    left: Expression;
    right: Expression;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

例如

a >= 0

// 转成AST
{
  "type": "Program",
  "start": 0,
  "end": 7,
  "body": [
    {
      "type": "ExpressionStatement",
      "start": 0,
      "end": 6,
      "expression": {
        "type": "BinaryExpression",
        "start": 0,
        "end": 6,
        "left": {
          "type": "Identifier",
          "start": 0,
          "end": 1,
          "name": "a"
        },
        "operator": ">=",
        "right": {
          "type": "Literal",
          "start": 5,
          "end": 6,
          "value": 0,
          "raw": "0"
        }
      }
    }
  ],
  "sourceType": "module"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
8)AssignmentExpression
interface AssignmentExpression <: Expression {
    type: "AssignmentExpression";
    operator: AssignmentOperator;
    left: Expression;
    right: Expression;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

例如:

this.a = 0;

// 转成AST
{
  "type": "Program",
  "start": 0,
  "end": 12,
  "body": [
    {
      "type": "ExpressionStatement",
      "start": 0,
      "end": 11,
      "expression": {
        "type": "AssignmentExpression",
        "start": 0,
        "end": 10,
        "operator": "=",
        "left": {
          "type": "MemberExpression",
          "start": 0,
          "end": 6,
          "object": {
            "type": "ThisExpression",
            "start": 0,
            "end": 4
          },
          "property": {
            "type": "Identifier",
            "start": 5,
            "end": 6,
            "name": "a"
          },
          "computed": false,
          "optional": false
        },
        "right": {
          "type": "Literal",
          "start": 9,
          "end": 10,
          "value": 0,
          "raw": "0"
        }
      }
    }
  ],
  "sourceType": "module"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
9)UpdateExpression
interface UpdateExpression <: Expression {
    type: "UpdateExpression";
    operator: UpdateOperator;
    argument: Expression;
    prefix: boolean;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
10)LogicalExpression —— 表示逻辑运算符(&&,||,!)
interface LogicalExpression <: Expression {
    type: "LogicalExpression";
    operator: LogicalOperator;
    left: Expression;
    right: Expression;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
11)ConditionalExpression —— 表示条件运算符(三元)
interface ConditionalExpression <: Expression {
    type: "ConditionalExpression";
    test: Expression;
    alternate: Expression;
    consequent: Expression;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
12)NewExpression —— 表示new运算符
new constructor[([arguments])]
  • 1
interface NewExpression <: Expression {
    type: "NewExpression";
    callee: Expression;
    arguments: [ Expression ] | null;
}
  • 1
  • 2
  • 3
  • 4
  • 5

例如:

new Date()

// 转成AST
{
  "type": "Program",
  "start": 0,
  "end": 12,
  "body": [
    {
      "type": "ExpressionStatement",
      "start": 1,
      "end": 11,
      "expression": {
        "type": "NewExpression",
        "start": 1,
        "end": 11,
        "callee": {
          "type": "Identifier",
          "start": 5,
          "end": 9,
          "name": "Date"
        },
        "arguments": []
      }
    }
  ],
  "sourceType": "module"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
13)CallExpression —— 表示函数或方法的call表达式
interface CallExpression <: Expression {
    type: "CallExpression";
    callee: Expression;
    arguments: [ Expression ];
}
  • 1
  • 2
  • 3
  • 4
  • 5
14) MemberExpression —— 表示member表达式
interface MemberExpression <: Expression {
    type: "MemberExpression";
    object: Expression;
    property: Identifier | Expression;
    computed : boolean;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

例如:

console.log(4);

// 转成AST
{
  "type": "Program",
  "start": 0,
  "end": 17,
  "body": [
    {
      "type": "ExpressionStatement",
      "start": 2,
      "end": 17,
      "expression": {
        "type": "CallExpression",
        "start": 2,
        "end": 16,
        "callee": {
          "type": "MemberExpression",
          "start": 2,
          "end": 13,
          "object": {
            "type": "Identifier",
            "start": 2,
            "end": 9,
            "name": "console"
          },
          "property": {
            "type": "Identifier",
            "start": 10,
            "end": 13,
            "name": "log"
          },
          "computed": false,
          "optional": false
        },
        "arguments": [
          {
            "type": "Literal",
            "start": 14,
            "end": 15,
            "value": 4,
            "raw": "4"
          }
        ],
        "optional": false
      }
    }
  ],
  "sourceType": "module"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
15)YieldExpression —— 表示yield表达式
interface YieldExpression <: Expression {
    argument: Expression | null;
}
  • 1
  • 2
  • 3

注: yield expressions 是SpiderMonkey特有的.

16)ComprehensionExpression —— 表示
interface ComprehensionExpression <: Expression {
    body: Expression;
    blocks: [ ComprehensionBlock ];
    filter: Expression | null;
}
  • 1
  • 2
  • 3
  • 4
  • 5

数组推导式:

// 公式:
[ expression for (variable in object) if (condition) ]

// 例如:
var numbers = [1, 2, 3, 4];
var doubled = [i * 2 for (i of numbers)];
console.log(doubled); 
// logs 2,4,6,8
// 等价于
var doubled = numbers.map(function(i){return i * 2;});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

注: Array comprehensions 是SpiderMonkey特有的.

17)GeneratorExpression —— 表示generator表达式
interface GeneratorExpression <: Expression {
    body: Expression;
    blocks: [ ComprehensionBlock ];
    filter: Expression | null;
}
  • 1
  • 2
  • 3
  • 4
  • 5

注: Generator expressions 是SpiderMonkey特有的.

18)GraphExpression —— 表示graph表达式
interface GraphExpression <: Expression {
    index: uint32;
    expression: Literal;
}
  • 1
  • 2
  • 3
  • 4
// 类似这样的
#1={ self: #1# }.
  • 1
  • 2

注: Graph expressions 是SpiderMonkey特有的.

19)GraphIndexExpression —— 表示graph索引表达式
interface GraphIndexExpression <: Expression {
    index: uint32;
}

// 类似这样
#1#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Graph索引表达式是SpiderMonkey特有的.

20)LetExpression —— 表示一个let表达式
interface LetExpression <: Expression {
    type: "LetExpression";
    head: [ { id: Pattern, init: Expression | null } ];
    body: Expression;
}
  • 1
  • 2
  • 3
  • 4
  • 5

注: let表达式是SpiderMonkey特有的.

Pattern 模式(解构模式)
interface Pattern <: Node { }
  • 1
1)ObjectPattern —— 表示对象解构模式
interface ObjectPattern <: Pattern {
    type: "ObjectPattern";
    properties: [ { key: Literal | Identifier, value: Pattern } ];
}
  • 1
  • 2
  • 3
  • 4

例如:

let {a, b} = obj;
// 转成AST
{
  "type": "Program",
  "start": 0,
  "end": 18,
  "body": [
    {
      "type": "VariableDeclaration",
      "start": 0,
      "end": 17,
      "declarations": [
        {
          "type": "VariableDeclarator",
          "start": 4,
          "end": 16,
          "id": {
            "type": "ObjectPattern",
            "start": 4,
            "end": 10,
            "properties": [
              {
                "type": "Property",
                "start": 5,
                "end": 6,
                "method": false,
                "shorthand": true,
                "computed": false,
                "key": {
                  "type": "Identifier",
                  "start": 5,
                  "end": 6,
                  "name": "a"
                },
                "kind": "init",
                "value": {
                  "type": "Identifier",
                  "start": 5,
                  "end": 6,
                  "name": "a"
                }
              },
              {
                "type": "Property",
                "start": 8,
                "end": 9,
                "method": false,
                "shorthand": true,
                "computed": false,
                "key": {
                  "type": "Identifier",
                  "start": 8,
                  "end": 9,
                  "name": "b"
                },
                "kind": "init",
                "value": {
                  "type": "Identifier",
                  "start": 8,
                  "end": 9,
                  "name": "b"
                }
              }
            ]
          },
          "init": {
            "type": "Identifier",
            "start": 13,
            "end": 16,
            "name": "obj"
          }
        }
      ],
      "kind": "let"
    }
  ],
  "sourceType": "module"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
2)ArrayPattern —— 表示数组解构模式
interface ArrayPattern <: Pattern {
    type: "ArrayPattern";
    elements: [ Pattern | null ];
}
  • 1
  • 2
  • 3
  • 4

例如:

let [a, b, c] = [1, 2, 3];
// 转成AST
{
  "type": "Program",
  "start": 0,
  "end": 27,
  "body": [
    {
      "type": "VariableDeclaration",
      "start": 0,
      "end": 26,
      "declarations": [
        {
          "type": "VariableDeclarator",
          "start": 4,
          "end": 25,
          "id": {
            "type": "ArrayPattern",
            "start": 4,
            "end": 13,
            "elements": [
              {
                "type": "Identifier",
                "start": 5,
                "end": 6,
                "name": "a"
              },
              {
                "type": "Identifier",
                "start": 8,
                "end": 9,
                "name": "b"
              },
              {
                "type": "Identifier",
                "start": 11,
                "end": 12,
                "name": "c"
              }
            ]
          },
          "init": {
            "type": "ArrayExpression",
            "start": 16,
            "end": 25,
            "elements": [
              {
                "type": "Literal",
                "start": 17,
                "end": 18,
                "value": 1,
                "raw": "1"
              },
              {
                "type": "Literal",
                "start": 20,
                "end": 21,
                "value": 2,
                "raw": "2"
              },
              {
                "type": "Literal",
                "start": 23,
                "end": 24,
                "value": 3,
                "raw": "3"
              }
            ]
          }
        }
      ],
      "kind": "let"
    }
  ],
  "sourceType": "module"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
子句
1) SwitchCase —— 表示switch中case语句
interface SwitchCase <: Node {
    type: "SwitchCase";
    test: Expression | null;
    consequent: [ Statement ];
}
  • 1
  • 2
  • 3
  • 4
  • 5

例如:

switch (a) {
  case '1': 
    console.log('1');
    break;
  case '2':
    console.log('2');
    break;
  default:
    console.log('3');
}
// 转成AST
{
  "type": "Program",
  "start": 0,
  "end": 138,
  "body": [
    {
      "type": "SwitchStatement",
      "start": 0,
      "end": 138,
      "discriminant": {
        "type": "Identifier",
        "start": 8,
        "end": 9,
        "name": "a"
      },
      "cases": [
        {
          "type": "SwitchCase",
          "start": 15,
          "end": 58,
          "consequent": [
            {
              "type": "ExpressionStatement",
              "start": 30,
              "end": 47,
              "expression": {
                "type": "CallExpression",
                "start": 30,
                "end": 46,
                "callee": {
                  "type": "MemberExpression",
                  "start": 30,
                  "end": 41,
                  "object": {
                    "type": "Identifier",
                    "start": 30,
                    "end": 37,
                    "name": "console"
                  },
                  "property": {
                    "type": "Identifier",
                    "start": 38,
                    "end": 41,
                    "name": "log"
                  },
                  "computed": false,
                  "optional": false
                },
                "arguments": [
                  {
                    "type": "Literal",
                    "start": 42,
                    "end": 45,
                    "value": "1",
                    "raw": "'1'"
                  }
                ],
                "optional": false
              }
            },
            {
              "type": "BreakStatement",
              "start": 52,
              "end": 58,
              "label": null
            }
          ],
          "test": {
            "type": "Literal",
            "start": 20,
            "end": 23,
            "value": "1",
            "raw": "'1'"
          }
        },
        {
          "type": "SwitchCase",
          "start": 61,
          "end": 103,
          "consequent": [
            {
              "type": "ExpressionStatement",
              "start": 75,
              "end": 92,
              "expression": {
                "type": "CallExpression",
                "start": 75,
                "end": 91,
                "callee": {
                  "type": "MemberExpression",
                  "start": 75,
                  "end": 86,
                  "object": {
                    "type": "Identifier",
                    "start": 75,
                    "end": 82,
                    "name": "console"
                  },
                  "property": {
                    "type": "Identifier",
                    "start": 83,
                    "end": 86,
                    "name": "log"
                  },
                  "computed": false,
                  "optional": false
                },
                "arguments": [
                  {
                    "type": "Literal",
                    "start": 87,
                    "end": 90,
                    "value": "2",
                    "raw": "'2'"
                  }
                ],
                "optional": false
              }
            },
            {
              "type": "BreakStatement",
              "start": 97,
              "end": 103,
              "label": null
            }
          ],
          "test": {
            "type": "Literal",
            "start": 66,
            "end": 69,
            "value": "2",
            "raw": "'2'"
          }
        },
        {
          "type": "SwitchCase",
          "start": 106,
          "end": 136,
          "consequent": [
            {
              "type": "ExpressionStatement",
              "start": 119,
              "end": 136,
              "expression": {
                "type": "CallExpression",
                "start": 119,
                "end": 135,
                "callee": {
                  "type": "MemberExpression",
                  "start": 119,
                  "end": 130,
                  "object": {
                    "type": "Identifier",
                    "start": 119,
                    "end": 126,
                    "name": "console"
                  },
                  "property": {
                    "type": "Identifier",
                    "start": 127,
                    "end": 130,
                    "name": "log"
                  },
                  "computed": false,
                  "optional": false
                },
                "arguments": [
                  {
                    "type": "Literal",
                    "start": 131,
                    "end": 134,
                    "value": "3",
                    "raw": "'3'"
                  }
                ],
                "optional": false
              }
            }
          ],
          "test": null
        }
      ]
    }
  ],
  "sourceType": "module"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
2)CatchClause —— 表示catch语句
interface CatchClause <: Node {
    type: "CatchClause";
    param: Pattern;
    guard: Expression | null;
    body: BlockStatement;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
3)ComprehensionBlock —— 表示数组推导式中for或for each语句
interface ComprehensionBlock <: Node {
    left: Pattern;
    right: Expression;
    each: boolean;
}
  • 1
  • 2
  • 3
  • 4
  • 5
杂项
1)Identifier —— 标识符
interface Identifier <: Node, Expression, Pattern {
    type: "Identifier";
    name: string;
}
  • 1
  • 2
  • 3
  • 4

注意,标识符可以是表达式或解构模式

2)Literal ——
interface Literal <: Node, Expression {
    type: "Literal";
    value: string | boolean | null | number | RegExp;
}
  • 1
  • 2
  • 3
  • 4
AST 转化网址参考:https://astexplorer.net/
var a = 1;

// 转化成AST
{
  "type": "Program",
  "start": 0,
  "end": 10,
  "body": [
    {
      "type": "VariableDeclaration",
      "start": 0,
      "end": 10,
      "declarations": [
        {
          "type": "VariableDeclarator",
          "start": 4,
          "end": 9,
          "id": {
            "type": "Identifier",
            "start": 4,
            "end": 5,
            "name": "a"
          },
          "init": {
            "type": "Literal",
            "start": 8,
            "end": 9,
            "value": 1,
            "raw": "1"
          }
        }
      ],
      "kind": "var"
    }
  ],
  "sourceType": "module"
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/597751
推荐阅读
相关标签
  

闽ICP备14008679号