Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

jsdoc-type-pratt-parser

Npm Package [Test Status]

jsdoc, pratt, parser

readme

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@alpha
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)

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.

changelog

4.1.0 (2024-08-05)

Bug Fixes

  • allow unions in index signatures (aeba637)

Features

4.0.0 (2023-03-15)

Features

  • allow square bracket property access (cc01ccf)
  • parse index signatures and mapped types (dd49463)
  • remove unneeded variadic property from ObjectFieldResult (20bf77d)
  • split KeyValueResult in different types for objects and other uses (6b388d3)

BREAKING CHANGES

  • ObjectResult no longer contains KeyValueResult, but uses the new types ObjectFieldResult and JsdocObjectFieldResult.

For mode typescript this basically means a simple rename of type JsdocTypeKeyValue to JsdocTypeObjectField: input: { key: string } before:

{
  "type": "JsdocTypeObject",
  "meta": {
    "separator": "comma"
  },
  "elements": [
    {
      "type": "JsdocTypeKeyValue",
      "key": "key",
      "right": {
        "type": "JsdocTypeName",
        "value": "string"
      },
      "optional": false,
      "readonly": false,
      "variadic": false,
      "meta": {
        "hasLeftSideExpression": false
      }
    }
  ]
}

after:

{
  "type": "JsdocTypeObject",
  "meta": {
    "separator": "comma"
  },
  "elements": [
    {
      "type": "JsdocTypeObjectField",
      "key": "key",
      "right": {
        "type": "JsdocTypeName",
        "value": "string"
      },
      "optional": false,
      "readonly": false,
      "variadic": false,
      "meta": {
        "quote": undefined
      }
    }
  ]
}

for jsdoc mode the JsdocTypeKeyValue can now be either JsdocTypeObjectField or JsdocTypeJsdocObjectField: input: { Array<string>: string } before:

{
  "type": "JsdocTypeObject",
  "meta": {
    "separator": "comma"
  },
  "elements": [
    {
      "type": "JsdocTypeKeyValue",
      "left": {
        "type": "JsdocTypeGeneric",
        "left": {
          "type": "JsdocTypeName",
          "value": "Array"
        },
        "elements": [
          {
            "type": "JsdocTypeName",
            "value": "string"
          }
        ],
        "meta": {
          "brackets": "angle",
          "dot": false
        }
      },
      "right": {
        "type": "JsdocTypeName",
        "value": "string"
      },
      "meta": {
        "hasLeftSideExpression": true
      }
    }
  ]
}

after:

{
  "type": "JsdocTypeObject",
  "meta": {
    "separator": "comma"
  },
  "elements": [
    {
      "type": "JsdocTypeJsdocObjectField",
      "left": {
        "type": "JsdocTypeGeneric",
        "left": {
          "type": "JsdocTypeName",
          "value": "Array"
        },
        "elements": [
          {
            "type": "JsdocTypeName",
            "value": "string"
          }
        ],
        "meta": {
          "brackets": "angle",
          "dot": false
        }
      },
      "right": {
        "type": "JsdocTypeName",
        "value": "string"
      }
    }
  ]
}

3.2.0-dev.3 (2023-03-13)

Features

  • allow square bracket property access (e069109)

3.2.0-dev.2 (2023-03-13)

Features

  • parse index signatures and mapped types (4373e01)

3.2.0-dev.1 (2023-03-13)

Features

  • remove unneeded variadic property from ObjectFieldResult (d392f93)
  • split KeyValueResult in different types for objects and other uses (aa6c495)

3.1.0 (2022-05-19)

Bug Fixes

Features

  • allow 'new' as a keyword for arrow functions (5ab04e7)
  • allow new as a function keyword (58ace96)
  • allow typed args parameter for functions (b538628)

3.0.1 (2022-04-19)

Bug Fixes

  • ObjectParslet: accept linebreaks (99d4bae)

3.0.0 (2022-04-19)

Bug Fixes

  • Lexer: use new lexer interface (03ed15e)

Features

  • Lexer has a readonly state (13ec065)

BREAKING CHANGES

  • the advance function of the lexer does not change the internal state of the object, but does instead return a new Lexer object. The getters token(), peek() and previous() have been replaced with readonly properties current, next, previous.
  • the API of the Parser class has changed. It can't parse multiple expressions anymore, but needs to be instantiated for each expression. The expression is passed to the constructor. The current lexer can be accessed via the property lexer.

2.2.5 (2022-03-13)

Bug Fixes

  • ObjectParslet: support trailing comma or semicolon (58b057a)

2.2.4 (2022-03-11)

Bug Fixes

  • typos in error messages (1c7c372)

2.2.3 (2022-02-12)

Bug Fixes

  • remove some unneeded code (55b2e68)

2.2.2 (2022-02-03)

Bug Fixes

  • typescript: ensure readonly discoverable within object context (675e431)
  • use correct precedence for right side of arrow function (6b0a410)

2.2.2 (2022-01-17)

Bug Fixes

  • use correct precedence for right side of arrow function (6b0a410)

2.2.1 (2022-01-09)

Bug Fixes

  • use parseFloat for number types (09a0717)

2.2.0 (2022-01-09)

Features

2.1.0 (2022-01-08)

Features

  • add path grammar for NamePathParslet (9bb29c4)

2.0.2 (2021-12-28)

Bug Fixes

2.0.1 (2021-12-28)

Bug Fixes

2.0.0 (2021-10-15)

Features

  • KeyValue: add discriminator for different KeyValue types and rename value to key (4cb9dd0)
  • KeyValue: enable labeled KeyValue results (576c2d8)
  • traverse: use visitorKeys and sinon-chai for tests (8a98f9a)

BREAKING CHANGES

  • KeyValue: For JsdocTypeKeyValue results, if it is a key value with a string key. The key is no longer named value but key to avoid confusion.

1.2.0 (2021-10-02)

Features

  • parser: add readonly modifier for object properties (ad3f0c9)

1.1.1 (2021-07-27)

Bug Fixes

1.1.0 (2021-07-27)

Bug Fixes

  • Add chores token. (dc1d3e9)
  • Allow semicolon as object entry separator. (49b19a3)
  • Remove unneeded check. (d7839ba)

Features

1.0.5 (2021-07-27)

Bug Fixes

  • Add chores token. (dc1d3e9)
  • Allow semicolon as object entry separator. (49b19a3)
  • Remove unneeded check. (d7839ba)

1.0.9-dev.1 (2021-07-20)

Bug Fixes

  • Add chores token. (dc1d3e9)
  • Allow semicolon as object entry separator. (49b19a3)
  • Remove unneeded check. (d7839ba)

1.0.9 (2021-07-20)

Bug Fixes

  • Add chores token. (dc1d3e9)
  • Allow semicolon as object entry separator. (49b19a3)
  • Remove unneeded check. (d7839ba)

1.0.8 (2021-06-02)

Reverts

1.0.7 (2021-06-02)

Bug Fixes

1.0.6 (2021-06-02)

Bug Fixes

1.0.5 (2021-06-02)

Bug Fixes

1.0.4 (2021-06-02)

Bug Fixes

  • confine extended Unicode escapes to 0x10FFFF (937bcfb)
  • support Unicode escape sequences for identifiers (edb18a0)

1.0.3 (2021-06-01)

Bug Fixes

  • Precedence for key value pairs in objects (ebde3ca), closes #63

1.0.2 (2021-05-31)

Bug Fixes

  • Make NumberResult terminal (9dc78b5)
  • Name paths have properties not names on the right side. (34c85a7)

1.0.1 (2021-05-31)

Bug Fixes

  • liberalize ID start and continue values as per Unicode (keeping hyphen as part of continue) (67f0929)

1.0.0 (2021-05-30)

Bug Fixes

  • tsconfig.json no lib (6df24dc)
  • gh-pages: set author (a28b3dd)
  • gh-pages: use actor (74256b1)
  • gh-pages: whitespace (16e97c4)
  • github actions: rename test workflow (d70f819)
  • pages: fix deploy script (1e04ebb)
  • pages: move deployment to release.yaml (748c18a)
  • readme: performance text (824bdad)
  • readme: test badge pointing to correct workflow (dacfe87)
  • readme: use npm links to alpha channel (cf13883)

Features

  • function is of type JsdocTypeName now. (225c337)
  • add module grammar, add parallel grammar (2fe4fd7)
  • add not nullable to base grammar (7d0910f)
  • Keys of KEY_VALUE are now static values and no ParseResults. (c2bfa24)
  • make types eslint friendly (497cd59)
  • meta brackets change (4bbc137)
  • meta position change (f85ce88)
  • meta quotes changed (3faf6c1)
  • Parser exposes infixParse and accepts Lexer (415b7c9)
  • path type change (7601ce5)
  • remove reservedWord meta (d73d64d)
  • removed submodules (dc87398)
  • SpecialNamePathParslet switches to other grammar. Allow module: in typescript and closure. (25edc18)
  • use KEY_VALUE for typed record entries (80fd550)

BREAKING CHANGES

  • Names no longer have the meta property reservedWord
  • pathType property uses 'instance', 'inner' and 'property' now
  • brackets property uses 'square' and 'angle' now
  • position property is lowercase. example: 'suffix'
  • For meta properties use 'single' and 'double' instead of ''' and '"'
  • All types are now prefixed with JsdocType and are made CamelCase
  • JSDOC_OBJECT_KEY_VALUE is now KEY_VALUE.
  • key left was removed from KEY_VALUE and is replaced by value. For the special record entries of jsdoc mode a new type JSDOC_OBJECT_KEY_VALUE was introduced.

1.0.0-alpha.24 (2021-05-29)

Bug Fixes

Features

  • function is of type JsdocTypeName now. (225c337)
  • add module grammar, add parallel grammar (2fe4fd7)
  • add not nullable to base grammar (7d0910f)
  • Parser exposes infixParse and accepts Lexer (415b7c9)
  • SpecialNamePathParslet switches to other grammar. Allow module: in typescript and closure. (25edc18)

1.0.0-alpha.23 (2021-05-24)

Features

1.0.0-alpha.22 (2021-05-23)

Features

BREAKING CHANGES

  • Names no longer have the meta property reservedWord
  • pathType property uses 'instance', 'inner' and 'property' now
  • brackets property uses 'square' and 'angle' now
  • position property is lowercase. example: 'suffix'
  • For meta properties use 'single' and 'double' instead of ''' and '"'
  • All types are now prefixed with JsdocType and are made CamelCase
  • JSDOC_OBJECT_KEY_VALUE is now KEY_VALUE.

1.0.0-alpha.21 (2021-05-23)

Features

  • Keys of KEY_VALUE are now static values and no ParseResults. (c2bfa24)

BREAKING CHANGES

  • key left was removed from KEY_VALUE and is replaced by value. For the special record entries of jsdoc mode a new type JSDOC_OBJECT_KEY_VALUE was introduced.

1.0.0-alpha.20 (2021-05-19)

Bug Fixes

  • readme: test badge pointing to correct workflow (dacfe87)

1.0.0-alpha.19 (2021-05-19)

Bug Fixes

  • readme: use npm links to alpha channel (cf13883)

1.0.0-alpha.18 (2021-05-19)

Bug Fixes

1.0.0-alpha.17 (2021-05-19)

Bug Fixes

1.0.0-alpha.16 (2021-05-19)

Bug Fixes

  • gh-pages: set author (a28b3dd)
  • github actions: rename test workflow (d70f819)

1.0.0-alpha.15 (2021-05-19)

Bug Fixes

  • pages: move deployment to release.yaml (748c18a)

1.0.0-alpha.14 (2021-05-19)

Bug Fixes

  • pages: fix deploy script (1e04ebb)

1.0.0-alpha.13 (2021-05-19)

Bug Fixes

  • readme: performance text (824bdad)