@gerhobbelt/ast-util

中文文档
0.6.1-4发布于 2017/08/27Apache-2.0CommonJS近 30 天下载 2.4万

Utilities for AST transformers.

原始使用文档
文档目录7 节
  1. Install
  2. API
  3. Usage
  4. Contributing
  5. Setup
  6. Pull Requests
  7. Acknowledgements

ast-util

Utilities for AST transformers.

Install

$ npm install [--save] ast-util

API

# callArraySlice(scope, node[, begin, end])

Returns a call to Array.prototype.slice with node as the context and begin and end as the arguments to slice.

# callFunctionBind(scope, fn, context[, args])

Returns a call to Function.prototype.bind using either call or apply depending on what the value of args is. If args is an expression then apply is used. If args is an array of expressions, then call.

# callGet(scope, object, property, receiver)

The [[Get]] internal method on objects would look something like helpers/get.js.

# callGetOwnPropertyDescriptor(scope, object, property)

Returns a call to Object.getOwnPropertyDescriptor with the given object and property.

# callGetPrototypeOf(scope, object)

Returns a call to Object.getPrototypeOf with the given object.

# callHasOwnProperty(scope, node, property)

Returns a call to hasOwnProperty with node as the context and property as the property to check.

# callSharedMethod(scope, callee, args)

Returns a call to the given callee with args as the arguments. If callee is a string then it is treated as a globally-accessible function such as Object.defineProperty which will be stored in a unique temporary variable. Subsequent calls to this function will re-use the same temporary variable.

# callSharedMethodWithContext(scope, callee, context, args)

Returns a call to the given callee with context as the method context and args as the arguments. If callee is a string then it is treated as a globally-accessible function such as Array.prototype.slice which will be stored in a unique temporary variable. Subsequent calls to this function will re-use the same temporary variable.

# getGlobals(ast)

Gets a list of identifiers referencing global variables anywhere within the given ast. Assuming the ast is for this code:

var a;
function b(){ return c; }
b(d);

Then getGlobals will return two identifiers, c and d.

# identifierForString(string)

Generate a safe JavaScript identifier for the given string.

# injectShared(scope, name, expression)

Injects a shared variable with a unique identifier. Only the first call with the same scope and name will result in a variable declaration being created. The expression passed in can either be an AST node or a function to generate one. This function is generally used to inject repeatedly-used values and prevent repeated execution.

# injectVariable(scope, identifier[, init])

Injects a variable with the given identifier into the given scope as a var declaration with an optional initial value.

# isReference(path)

Determines whether the given path is a value reference. For example, a and b are references, but c is not:

a(b.c);

Only identifiers count as references.

# isUsed(scope, name)

Determines whether the given name should be considered "used" in the given scope. For a name to be used, it should either:

  1. Be declared in this scope or a parent scope.
  2. Be referenced in this scope, a parent scope, or any child scopes.

For example, a, b, and d are used in the global scope of this example while c is not:

var a;
function b() {}

try {
  a = b(d);
} catch (c) {
}

# sharedFor(scope, name)

Injects a shared variable by getting the named value from a dotted path. For example, this will return an identifier that can be used in place of the named expression:

sharedFor(scope, 'Object.defineProperty')

Subsequent calls to sharedFor in the same scope will return the same identifier.

# uniqueIdentifier(scope[, name])

Generates an identifier guaranteed not to collide with any others in the given scope. This function will also never generate the same identifier twice for any scope whose global scope already got that identifier.

Called in a scope with no global references and no variables, the first time this function is called it will return an identifier named $__0.

When called with a name that name will be used with a prefix, "$__", if possible. If that name is already used then it will append incrementing numbers until it finds a name that isn't used.

Usage

These methods are useful to source transforms, such as transpilers or macros. Such transforms often have to insert variables into scopes and replace expressions. Using injectVariable and injectShared are specifically for that purpose. In conjunction with ast-types, here's how you'd write a simple version of a swap macro:

// var tmp;
var tmp = util.injectVariable(
  this.scope,
  util.uniqueIdentifier(this.scope)
);

this.replace(
  b.sequenceExpression([
    // tmp = left
    b.assignmentExpression(
      '=',
      tmp,
      left
    ),
    // left = right
    b.assignmentExpression(
      '=',
      left,
      right
    ),
    // right = tmp
    b.assignmentExpression(
      '=',
      right,
      tmp
    )
  ])
);

See examples/swap-macro.js for a more complete example.

Contributing

Build Status

Setup

First, install the development dependencies:

$ npm install

Then, try running the tests:

$ make test

If you're adding or editing code that injects helpers into a scope, you'll need to edit and run the Makefile to have it generate the files in lib/helpers from the files in helpers.

Pull Requests

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Acknowledgements

Huge thanks to Ben Newman for ast-types, on which much of this library depends.

版本与发布记录

@gerhobbelt/ast-util 共发布 1 个版本,首次发布于 2017/08/27;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.6.1-4latest2017/08/279 年前

关于 @gerhobbelt/ast-util 的常见问题

@gerhobbelt/ast-util 是什么?

Utilities for AST transformers.

如何安装 @gerhobbelt/ast-util?

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

@gerhobbelt/ast-util 支持 TypeScript 吗?

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

@gerhobbelt/ast-util 支持 ESM 还是 CommonJS?

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

@gerhobbelt/ast-util 需要什么版本的 Node.js?

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

@gerhobbelt/ast-util 可以商用吗?

@gerhobbelt/ast-util 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@gerhobbelt/ast-util 的下载量有多少?

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

@gerhobbelt/ast-util 还在维护吗?

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