@buxlabs/ast

中文文档
0.11.0发布于 2018/05/14MITCommonJS近 30 天下载 2758

js 的抽象语法树类

中文使用文档
文档目录20 节
  1. 安装
  2. 方法
  3. 已经
  4. 计数
  5. 寻找
  6. 每个
  7. 第一个
  8. 最后一个
  9. 删除
  10. 走开
  11. 转弯
  12. 替换
  13. 预选
  14. 附录
  15. 关闭
  16. 打开
  17. 模板
  18. 标记
  19. 改为来源
  20. ưμ㼯A

抽象语法Tree

Build Status NSP Status

如果想要抽象出一些诸如添加,去除,替换节点等的自控操作,你可能会发现该类有用. 测试文件夹是使用实例的好起点. 公关非常受欢迎.

安装

npm install @buxlabs/ast

方法

已经

检查是否包含指定类型的节点 .

const source = 'const a = "x";'
const ast = new AbstractSyntaxTree(source)
ast.has('VariableDeclaration')

计数

计算给定类型的星座节点.

const source = 'const a = "x"; const b = "y";'
const ast = new AbstractSyntaxTree(source)
ast.count('VariableDeclaration')

寻找

查找指定类型的所有节点 .

const source = 'const a = "x";'
const ast = new AbstractSyntaxTree(source)
ast.find('VariableDeclaration')

每个

在所有指定类型的节点上标出 .

const source = 'const a = "x";'
const ast = new AbstractSyntaxTree(source)
ast.each('VariableDeclaration', node => {})

第一个

第一个指定类型的节点 .

const source = 'var a = "x";'
const ast = new AbstractSyntaxTree(source)
ast.first('VariableDeclaration')

最后一个

查找指定类型的最后一个节点.

const source = 'const a = "x";'
const ast = new AbstractSyntaxTree(source)
ast.last('VariableDeclaration')

删除

删除符合标准的所有节点 .

const source = '"use strict"; const b = 4;'
const ast = new AbstractSyntaxTree(source)
ast.remove({ type: 'Literal', value: 'use strict' })
const source = 'function hello () { const foo = "bar"; return "world"; }'
const ast = new AbstractSyntaxTree(source)
ast.remove('BlockStatement > VariableDeclaration')

走开

走遍所有节点

const source = 'const a = 1'
const ast = new AbstractSyntaxTree(source)
ast.walk((node, parent) => {})

转弯

走遍所有节点

const source = 'const a = 1'
const ast = new AbstractSyntaxTree(source)
ast.walk({
  enter (node) {},
  leave (node) {}
})

替换

替换所有符合标准的节点 .

const source = 'const a = 1'
const ast = new AbstractSyntaxTree(source)
ast.replace({
  enter (node) {
    if (node.type === 'VariableDeclaration') {
      node.kind = 'let'
    }
    return node
  }
})

预选

将一个节点放在身体上.

const source = 'const a = 1;'
const ast = new AbstractSyntaxTree(source)
ast.prepend({
  type: 'ExpressionStatement',
  expression: {
    type: 'Literal',
    value: 'use strict'
  }
})

附录

给身体加上一个节点.

const source = 'const a = 1;'
const ast = new AbstractSyntaxTree(source)
ast.append({
  type: 'ExpressionStatement',
  expression: {
    type: 'Literal',
    value: 'test'
  }
})

关闭

用给定的节点包裹身体.

const source = 'const a = 1;'
const ast = new AbstractSyntaxTree(source)
ast.wrap(body => {
    return [
      {
        type: 'ExpressionStatement',
        expression: {
          type: 'CallExpression',
          callee: {
            type: 'FunctionExpression',
            params: [],
            body: {
              type: 'BlockStatement',
              body
            }
          },
          arguments: []
        }
      }
    ]
})

打开

将代码更改为第一个块状体

const source = '(function () { console.log(1); }())'
const ast = new AbstractSyntaxTree(source)
ast.unwrap()
ast.toSource()

模板

从模板创建部分

const source = 'console.log(1);'
const ast = new AbstractSyntaxTree(source)
ast.template('const foo = <%= bar %>;' { bar: { type: 'Literal', value: 1 } })

标记

在所有节点中添加 cid

const ast = new AbstractSyntaxTree('const a = 1;')
ast.mark()
assert(ast.first('Program').cid === 1)
assert(ast.first('VariableDeclaration').cid === 2)

改为来源

将 ast 转换为字符串 .

const source = 'const a = 1;'
const ast = new AbstractSyntaxTree(source)
const source = ast.toSource()
const source = 'const a = 1;'
const ast = new AbstractSyntaxTree(source)
const { source, map } = ast.toSource({ sourceMap: true })

ưμ㼯A

生成一个源地图.

const source = 'const a = 1;'
const ast = new AbstractSyntaxTree(source)
const map = ast.toSourceMap()

版本与发布记录

@buxlabs/ast 共发布 41 个版本,首次发布于 2016/12/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.11.0latest2018/05/148 年前
0.10.22018/05/108 年前
0.10.12018/05/018 年前
0.10.02018/05/018 年前
0.9.62018/03/188 年前
0.9.52018/03/118 年前

关于 @buxlabs/ast 的常见问题

@buxlabs/ast 是什么?

js 的抽象语法树类

如何安装 @buxlabs/ast?

在项目目录运行 npm install @buxlabs/ast,或使用 pnpm add / yarn add / bun add。

@buxlabs/ast 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/buxlabs__ast 是否存在。

@buxlabs/ast 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@buxlabs/ast 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=8.11.1。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@buxlabs/ast 可以商用吗?

@buxlabs/ast 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@buxlabs/ast 的下载量有多少?

根据 npm 官方下载统计,@buxlabs/ast 在 2026/10/09 – 2026/11/07 这 30 天内被下载 2,758 次,日均约 92 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@buxlabs/ast 还在维护吗?

最新版本 0.11.0 发布于 2018/05/14(8 年前);至今共发布 41 个版本,其中过去 12 个月发布了 0 个;首次发布于 2016/12/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。