0.0.61 • Published 1 month ago

unstable-parser v0.0.61

Weekly downloads
-
License
ISC
Repository
-
Last release
1 month ago

unstable-parser

一个 Javascript 解析器,可以将js代码解析为AST(抽象语法树)
该解析器目前处于实验阶段,请勿在生产环境中使用
目前该解析器已经可以解析大多数JS语法,只有极少数语法不支持
解析出的AST已经基本符合 ESTree规范

使用方法

在线使用

在线解析
以一个基本的防抖函数为例
进入上述网站,在左侧粘贴下列代码,并点击上方解析按钮

export function debounce(fn, delay, self = null) {
	let timer = null;
	return function(...args) {
		if(timer) {clearTimeout(timer)}
		timer = setTimeout(()=> {
			self ? fn.apply(self, args) : fn.apply(window, args);
		}, delay);
	}
}

会输出以下AST

{
  "type": "Program",
  "body": [
    {
      "type": "ExportDeclaration",
      "declaration": {
        "type": "FunctionDeclaration",
        "body": {
          "type": "BlockStatement",
          "body": [
            {
              "type": "VariableDeclaration",
              "kind": "let",
              "declarations": [
                {
                  "type": "VariableDeclarator",
                  "id": {
                    "type": "Identifier",
                    "value": "timer",
                    "name": "timer"
                  },
                  "init": {
                    "type": "Literal",
                    "value": null,
                    "raw": "null"
                  }
                }
              ]
            },
            {
              "type": "ReturnStatement",
              "argument": {
                "type": "FunctionExpression",
                "body": {
                  "type": "BlockStatement",
                  "body": [
                    {
                      "type": "IfStatement",
                      "test": {
                        "type": "Identifier",
                        "value": "timer",
                        "name": "timer"
                      },
                      "consequent": {
                        "type": "BlockStatement",
                        "body": [
                          {
                            "type": "ExpressionStatement",
                            "expression": {
                              "type": "CallExpression",
                              "callee": {
                                "type": "Identifier",
                                "value": "clearTimeout",
                                "name": "clearTimeout"
                              },
                              "arguments": [
                                {
                                  "type": "Identifier",
                                  "value": "timer",
                                  "name": "timer"
                                }
                              ],
                              "optional": false
                            }
                          }
                        ]
                      }
                    },
                    {
                      "type": "ExpressionStatement",
                      "expression": {
                        "type": "AssignmentExpression",
                        "operator": "=",
                        "left": {
                          "type": "Identifier",
                          "value": "timer",
                          "name": "timer"
                        },
                        "right": {
                          "type": "CallExpression",
                          "callee": {
                            "type": "Identifier",
                            "value": "setTimeout",
                            "name": "setTimeout"
                          },
                          "arguments": [
                            {
                              "type": "ArrowFunctionExpression",
                              "params": [],
                              "body": {
                                "type": "BlockStatement",
                                "body": [
                                  {
                                    "type": "ExpressionStatement",
                                    "expression": {
                                      "test": {
                                        "type": "Identifier",
                                        "value": "self",
                                        "name": "self"
                                      },
                                      "type": "ConditionalExpression",
                                      "consequent": {
                                        "type": "CallExpression",
                                        "callee": {
                                          "type": "MemberExpression",
                                          "object": {
                                            "type": "Identifier",
                                            "value": "fn",
                                            "name": "fn"
                                          },
                                          "property": {
                                            "type": "Identifier",
                                            "value": "apply",
                                            "name": "apply"
                                          },
                                          "optional": false
                                        },
                                        "arguments": [
                                          {
                                            "type": "Identifier",
                                            "value": "self",
                                            "name": "self"
                                          },
                                          {
                                            "type": "Identifier",
                                            "value": "args",
                                            "name": "args"
                                          }
                                        ],
                                        "optional": false
                                      },
                                      "alternate": {
                                        "type": "CallExpression",
                                        "callee": {
                                          "type": "MemberExpression",
                                          "object": {
                                            "type": "Identifier",
                                            "value": "fn",
                                            "name": "fn"
                                          },
                                          "property": {
                                            "type": "Identifier",
                                            "value": "apply",
                                            "name": "apply"
                                          },
                                          "optional": false
                                        },
                                        "arguments": [
                                          {
                                            "type": "Identifier",
                                            "value": "window",
                                            "name": "window"
                                          },
                                          {
                                            "type": "Identifier",
                                            "value": "args",
                                            "name": "args"
                                          }
                                        ],
                                        "optional": false
                                      }
                                    }
                                  }
                                ]
                              }
                            },
                            {
                              "type": "Identifier",
                              "value": "delay",
                              "name": "delay"
                            }
                          ],
                          "optional": false
                        }
                      }
                    }
                  ]
                },
                "id": null,
                "generator": false,
                "params": [
                  {
                    "type": "RestElement",
                    "name": {
                      "type": "Identifier",
                      "value": "args",
                      "name": "args"
                    }
                  }
                ]
              }
            }
          ]
        },
        "id": {
          "type": "Identifier",
          "value": "debounce",
          "name": "debounce"
        },
        "generator": false,
        "params": [
          {
            "type": "Identifier",
            "value": "fn",
            "name": "fn"
          },
          {
            "type": "Identifier",
            "value": "delay",
            "name": "delay"
          },
          {
            "type": "AssignmentPattern",
            "left": {
              "type": "Identifier",
              "value": "self",
              "name": "self"
            },
            "right": {
              "type": "Literal",
              "value": null,
              "raw": "null"
            }
          }
        ]
      }
    }
  ]
}

通过npm使用

安装

npm install unstable-parser

使用
import { parse } from "unstable-parser";
const ast = parse("需要解析的代码");

通过公共CDN使用

以 skypack 为例

import("https://cdn.skypack.dev/unstable-parser").then(({parse})=> {
    const ast = parse("需要解析的代码");
})
0.0.61

1 month ago

0.0.60

10 months ago

0.0.59

1 year ago

0.0.57

1 year ago

0.0.58

1 year ago

0.0.40

1 year ago

0.0.41

1 year ago

0.0.42

1 year ago

0.0.44

1 year ago

0.0.45

1 year ago

0.0.46

1 year ago

0.0.47

1 year ago

0.0.37

2 years ago

0.0.38

2 years ago

0.0.39

2 years ago

0.0.34

2 years ago

0.0.35

2 years ago

0.0.36

2 years ago

0.0.51

1 year ago

0.0.52

1 year ago

0.0.54

1 year ago

0.0.55

1 year ago

0.0.56

1 year ago

0.0.50

1 year ago

0.0.48

1 year ago

0.0.49

1 year ago

0.0.31

2 years ago

0.0.32

2 years ago

0.0.33

2 years ago

0.0.30

2 years ago

0.0.29

2 years ago

0.0.28

2 years ago

0.0.27

2 years ago

0.0.26

2 years ago

0.0.25

2 years ago

0.0.24

2 years ago

0.0.23

2 years ago

0.0.22

2 years ago

0.0.21

2 years ago

0.0.20

2 years ago

0.0.17

2 years ago

0.0.16

2 years ago

0.0.15

2 years ago

0.0.14

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago