是一個JS parser Uglify 用到了它
var esprima = require('esprima');
var program = 'const answer = 42';
esprima.tokenize(program)
//we get this
[ { type: 'Keyword', value: 'const' },
{ type: 'Identifier', value: 'answer' },
{ type: 'Punctuator', value: '=' },
{ type: 'Numeric', value: '42' } ]
parse 將得到抽象語法樹AST
esprima.parse(program)
因?yàn)橹挥?const answer = 42 這么一句
所以語法樹是一個長度為1的數(shù)組
這句話是一個變量聲明 variable declaration
轉(zhuǎn)為 AST 得到下面信息

image.png
如果有兩句話呢 比如這樣
program = 'var a = 1; if(a == 1){console.log(1)}else{console.log(\'not 1\')}';
ast = esprima.parse(program);
第一句是變量聲明 第二句是if條件判斷語句

image.png
條件判斷語句又包括三個部分 判斷條件和兩個 語句塊

image.png
在一個語句塊中又只有一句話(body:array[1])
這句話里
又只有一個表達(dá)式 (array[0]是ExpressionStatement)

image.png
這個表達(dá)式console.log(1)的參數(shù)是一個數(shù)字1
expression 再展開

image.png
//then we got
{
"type": "Program",
"body": [
{
"type": "VariableDeclaration",
"declarations": [
{
"type": "VariableDeclarator",
"id": {
"type": "Identifier",
"name": "a"
},
"init": {
"type": "Literal",
"value": 1,
"raw": "1"
}
}
],
"kind": "var"
},
{
"type": "IfStatement",
"test": {
"type": "BinaryExpression",
"operator": "==",
"left": {
"type": "Identifier",
"name": "a"
},
"right": {
"type": "Literal",
"value": 1,
"raw": "1"
}
},
"consequent": {
"type": "BlockStatement",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "CallExpression",
"callee": {
"type": "MemberExpression",
"computed": false,
"object": {
"type": "Identifier",
"name": "console"
},
"property": {
"type": "Identifier",
"name": "log"
}
},
"arguments": [
{
"type": "Literal",
"value": 1,
"raw": "1"
}
]
}
}
]
},
"alternate": {
"type": "BlockStatement",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "CallExpression",
"callee": {
"type": "MemberExpression",
"computed": false,
"object": {
"type": "Identifier",
"name": "console"
},
"property": {
"type": "Identifier",
"name": "log"
}
},
"arguments": [
{
"type": "Literal",
"value": "not 1",
"raw": "'not 1'"
}
]
}
}
]
}
}
],
"sourceType": "script"
}