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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@charrue/ast-toolkit

v0.0.3

Published

AST操作辅助工具

Readme

@charrue/ast-toolkit

一款辅助AST操作的工具库。

下载

npm add @charrue/ast-toolkit -D

方法

getNodeIdentifierName

用于获取当前节点的字面名称。 如果是变量声明,则获取变量名:

import t from "@babel/types";
import { getNodeIdentifierName } from "@charrue/ast-helper";
    const name = "foo";

getNodeIdentifierName(t.identifier(name)) // "foo"

如果是函数声明,则获取函数名:

import t from "@babel/types";
import { getNodeIdentifierName } from "@charrue/ast-helper";

const name = "fn";
const fn = t.functionDeclaration(
  t.identifier(name),
  [t.identifier("arg")],
  t.blockStatement([]),
);

getNodeIdentifierName(fn) // "fn"

如果是函数调用,则返回完整的调用路径:

import t from "@babel/types";
import { getNodeIdentifierName } from "@charrue/ast-helper";

const oName = "console";
const pName = "log";
const callee = t.callExpression(
  t.memberExpression(t.identifier(oName), t.identifier(pName), false),
  [],
);

getNodeIdentifierName(callee)).toBe(`${oName}.${pName}` // "console.log"

如果不是通过.调用:

import t from "@babel/types";
import { getNodeIdentifierName } from "@charrue/ast-helper";

const oName = "console";
const pName = "'log'";
const callee = t.callExpression(
  t.memberExpression(t.identifier(oName), t.stringLiteral(pName), true),
  [],
);

getNodeIdentifierName(callee)).toBe(`${oName}.${pName}` // "console['log']"

如果是构造函数的实例化,则会返回构造函数名:

import t from "@babel/types";
import { getNodeIdentifierName } from "@charrue/ast-helper";

const name = "People";
const ctor = t.newExpression(t.identifier(name), []);

getNodeIdentifierName(ctor) // "People"

getLiteralNodeValue

用于获取字面量值,并且无法识别值中包含变量,而变量所在位置将会返回undefined。

import t from "@babel/types";
import { getLiteralNodeValue } from "@charrue/ast-helper";

const key1 = "str";
const value1 = "foo";
const key2 = "num";
const value2 = 10;
const key3 = "bool";
const value3 = false;

const result = getLiteralNodeValue(
  t.objectExpression([
    t.objectProperty(t.identifier(key1), t.stringLiteral(value1)),
    t.objectProperty(t.identifier(key2), t.numericLiteral(value2)),
    t.objectProperty(t.identifier(key3), t.booleanLiteral(value3)),
  ]),
);
// { str: "foo", num: 10, bool: false }

如果值中包含变量:

import t from "@babel/types";
import { getLiteralNodeValue } from "@charrue/ast-helper";

const key = "str";
const value = t.identifier("CONST");

const result = getLiteralNodeValue(
  t.objectExpression([t.objectProperty(t.identifier(key), value)]),
);
// { str: undefined }

getCallExpressionArguments

获取函数的入参,返回一个参数组成的数组。同样无法识别入参中的变量。

import { getCallExpressionArguments } from "@charrue/ast-helper";

const source = `const result = fun(1);
  console.log(result);`

const result = getCallExpressionArguments(source, "fun"); // [1]