@buxlabs/ast

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

abstract syntax tree class for js

原始使用文档
文档目录20 节
  1. Installation
  2. Methods
  3. has
  4. count
  5. find
  6. each
  7. first
  8. last
  9. remove
  10. walk
  11. traverse
  12. replace
  13. prepend
  14. append
  15. wrap
  16. unwrap
  17. template
  18. mark
  19. toSource
  20. toSourceMap

AbstractSyntaxTree

Build Status NSP Status

You might find the class useful if you want to abstract away some ast manipulations such as adding, removing, replacing the nodes and others. The test folder is a good starting point for examples of usage. PRs are highly welcome.

Installation

npm install @buxlabs/ast

Methods

has

Check if ast contains a node of given type.

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

count

Count ast nodes of given type.

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

find

Find all nodes of given type.

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

each

Iterate over all nodes of given type.

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

first

First first node of given type.

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

last

Find last node of given type.

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

remove

Remove all nodes that match the criteria.

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')

walk

Walks over all nodes

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

traverse

Walks over all nodes

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

replace

Replace all nodes that match the criteria.

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

prepend

Prepend a node to the body.

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

append

Append a node to the body.

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

wrap

Wrap body with given node.

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: []
        }
      }
    ]
})

unwrap

Change the code to the first BlockStatement body

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

template

Create ast partials from templates

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

mark

Add cid to all nodes

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

toSource

Convert the ast to string.

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 })

toSourceMap

Generates a source map.

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 是什么?

abstract syntax tree class for 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 和提交记录判断。