npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

unstable-parser

v0.0.61

Published

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

Downloads

16

Readme

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("需要解析的代码");
})