jsdoc-type-pratt-parser

中文文档
9.5.0发布于 2026/11/04MITTypeScriptES Module构建来源已验证

Npm Package [Test Status]

原始使用文档
文档目录11 节
  1. Table of Contents
  2. Live Demo
  3. Getting started
  4. API Documentation
  5. Available Grammars
  6. Transforms
  7. Traverse
  8. Custom Parsers and Stringifiers
  9. Tests Status
  10. Performance
  11. Development

Npm Package Test Status Coverage Status Code Style semantic-release

Jsdoc Type Pratt Parser

This project is a parser for jsdoc types. It takes jsdoc type expressions like Array<string> and creates an abstract syntax tree (AST) out of it. It is heavily inspired by the existing libraries catharsis and jsdoctypeparser, but does not use PEG.js, instead it is written as a pratt parser.

You can find some more information about pratt parsers here:

Table of Contents

Live Demo

A simple live demo to test expressions can be found at: https://jsdoc-type-pratt-parser.github.io/jsdoc-type-pratt-parser/

Getting started

npm install jsdoc-type-pratt-parser
import { parse } from 'jsdoc-type-pratt-parser'

const result = parse('SomeType<string>', 'typescript')

API Documentation

An API documentation can be found here. It is incomplete, but a good starting point. Please create issues or PRs if you don't find what you expect.

Available Grammars

Three different modes (grammars) are supported: 'jsdoc', 'closure' and 'typescript'

Transforms

A common task to do on ASTs are transforms, for example a stringification. This library includes some transform and utilities to implement your own.

stringify:

import { stringify } from 'jsdoc-type-pratt-parser'

const val = stringify({ type: 'JsdocTypeName', value: 'name'}) // -> 'name'

You can customize the stringification by using stringifyRules and transform:

import { stringifyRules, transform } from 'jsdoc-type-pratt-parser'

const rules = stringifyRules()

// `result` is the current node and `transform` is a function to transform child nodes.
rules.NAME = (result, transform) => 'something else'

const val = transform(rules, { type: 'JsdocTypeName', value: 'name'}) // -> 'something else'

You can also build your own transform rules by implementing the TransformRules<TransformResultType> interface or you can build upon the identity ruleset like this:

import { identityTransformRules, transform } from 'jsdoc-type-pratt-parser'

const myRules = identityTransformRules()
myRules.NAME = () => ({ type: 'JsdocTypeName', value: 'funky' })

const val = transform(myRules, result)

This library also supports compatibility modes for catharsis and jsdoctypeparser. The provided transform functions attempt to transform the output to the expected output of the target library. This will not always be the same as some types are parsed differently. These modes are thought to make transition easier, but it is advised to use the native output as this will be more uniform and will contain more information.

Catharsis compat mode:

import { parse, catharsisTransform } from 'jsdoc-type-pratt-parser'

const result = catharsisTransform(parse('myType.<string>', 'closure'))

Jsdoctypeparser compat mode:

import { parse, jtpTransform } from 'jsdoc-type-pratt-parser'

const result = jtpTransform(parse('myType.<string>', 'closure'))

Traverse

You can traverse an AST with the traverse function:

import { traverse } from 'jsdoc-type-pratt-parser'

// property is the name of the property on parent that contains node
function onEnter(node, parent, property) {
  console.log(node.type)
}

// an onEnter and/or an onLeave function can be supplied
traverse({ type: 'JsdocTypeName', value: 'name'}, onEnter, console.log)

Custom visitor keys (by node type) can be supplied as a fourth argument. They take precedence over the built-in visitorKeys, which is useful for nodes whose structure is not known to this package (e.g., the ASTs of computed properties):

traverse(ast, onEnter, onLeave, {
  // Visit the `expression` property of a custom node type
  MyCustomNode: ['expression'],
  // Nodes of unknown type are visited only if keys are given for them
  Identifier: []
})

Custom Parsers and Stringifiers

For TypeScript's computed property and methods, access to JavaScript (or TypeScript) parsing and stringification is required (beyond the types supported in jsdoc-type-pratt-parser). You can provide these by supplying the following optional arguments:

import { parse as espree } from 'espree'
import { generate } from '@es-joy/escodegen'

const parsed = parse(
  input,
  mode,
  { computedPropertyParser: espree }
)

const stringified = stringify(
  result,
  generate
)

Tests Status

This parser runs most tests of catharsis and jsdoctypeparser. It compares the results of the different parsing libraries. If you want to find out where the output differs, look in the tests for the comments // This seems to be an error of ... or the differ keyword which indicates that differing results are produced.

Performance

A simple performance comparison using Benchmark.js produced the following results:

Testing expression: Name
catharsis x 37,816 ops/sec ±1.22% (1086 runs sampled)
jsdoc-type-pratt-parser x 602,617 ops/sec ±0.16% (1090 runs sampled)
jsdoctypeparser x 53,256 ops/sec ±0.73% (1081 runs sampled)
The fastest was jsdoc-type-pratt-parser

Testing expression: Array<number>
catharsis x 10,124 ops/sec ±0.56% (1084 runs sampled)
jsdoc-type-pratt-parser x 228,660 ops/sec ±0.40% (1084 runs sampled)
jsdoctypeparser x 42,365 ops/sec ±0.60% (1070 runs sampled)
The fastest was jsdoc-type-pratt-parser

Testing expression: { keyA: Type<A | "string val" >, keyB: function(string, B): A }
catharsis x 1,138 ops/sec ±0.66% (1087 runs sampled)
jsdoc-type-pratt-parser x 46,535 ops/sec ±0.47% (1090 runs sampled)
jsdoctypeparser x 18,291 ops/sec ±0.71% (1084 runs sampled)
The fastest was jsdoc-type-pratt-parser

The benchmark test uses catharsis without cache.

Development

If you want to contribute see the Development Guide to get some pointers. Feel free to create issues if there is information missing.

版本与发布记录

jsdoc-type-pratt-parser 共发布 113 个版本,首次发布于 2021/02/20;过去 12 个月发布了 19 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
9.5.0latest2026/11/041 天前
9.4.02026/10/2213 天前
9.3.02026/10/2016 天前
9.2.32026/10/1322 天前
9.2.22026/09/171 个月前
9.2.12026/08/302 个月前

其他发布通道:alpha → 1.0.0-alpha.24,dev → 6.2.0-dev.2。

关于 jsdoc-type-pratt-parser 的常见问题

jsdoc-type-pratt-parser 是什么?

[]

如何安装 jsdoc-type-pratt-parser?

在项目目录运行 npm install jsdoc-type-pratt-parser,或使用 pnpm add / yarn add / bun add。

jsdoc-type-pratt-parser 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

jsdoc-type-pratt-parser 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

jsdoc-type-pratt-parser 需要什么版本的 Node.js?

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

jsdoc-type-pratt-parser 可以商用吗?

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

jsdoc-type-pratt-parser 还在维护吗?

最新版本 9.5.0 发布于 2026/11/04(1 天前);至今共发布 113 个版本,其中过去 12 个月发布了 19 个;首次发布于 2021/02/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。