esprima AST

是一個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"
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容