Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
jsdoc-type-pratt-parser
Advanced tools
[![Npm Package](https://badgen.net/npm/v/jsdoc-type-pratt-parser)](https://www.npmjs.com/package/jsdoc-type-pratt-parser) [![Test Status](https://github.com/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser/actions/workflows/test.yml/badge.svg?branch=main)]
This project is a parser for jsdoc types. 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.
A simple live demo to test expressions can be found at: https://jsdoc-type-pratt-parser.github.io/jsdoc-type-pratt-parser/
npm install jsdoc-type-pratt-parser@alpha
import { parse } from 'jsdoc-type-pratt-parser'
const result = parse('myType.<string>', 'closure')
This library 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'))
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 traverse a result tree 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)
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)
Three different modes (grammars) are supported: 'jsdoc'
, 'closure'
and 'typescript'
This parser runs most tests of https://github.com/hegemonic/catharsis and
https://github.com/jsdoctypeparser/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.
An API documentation can be found here: https://jsdoc-type-pratt-parser.github.io/jsdoc-type-pratt-parser/docs/modules.html
A simple performance comparision using Benchmark.js produced the following results:
Testing expression: Name
catharsis x 36,338 ops/sec ±1.10% (1071 runs sampled)
jsdoc-type-pratt-parser x 400,260 ops/sec ±0.87% (1070 runs sampled)
jsdoctypeparser x 61,847 ops/sec ±1.18% (1071 runs sampled)
The fastest was jsdoc-type-pratt-parser
Testing expression: Array<number>
catharsis x 7,969 ops/sec ±1.05% (1079 runs sampled)
jsdoc-type-pratt-parser x 159,001 ops/sec ±0.95% (1074 runs sampled)
jsdoctypeparser x 42,278 ops/sec ±1.01% (1070 runs sampled)
The fastest was jsdoc-type-pratt-parser
Testing expression: { keyA: Type<A | "string val" >, keyB: function(string, B): A }
catharsis x 933 ops/sec ±1.15% (1070 runs sampled)
jsdoc-type-pratt-parser x 29,596 ops/sec ±0.90% (1068 runs sampled)
jsdoctypeparser x 16,206 ops/sec ±1.38% (1055 runs sampled)
The fastest was jsdoc-type-pratt-parser
the test uses catharsis without cache, as this is just a simple lookup table that could easily be implemented for any parser.
2.0.0 (2021-10-15)
value
to key
(4cb9dd0)JsdocTypeKeyValue
results, if it is a key value with a string key. The key is no longer named value
but key
to avoid confusion.FAQs
[![Npm Package](https://badgen.net/npm/v/jsdoc-type-pratt-parser)](https://www.npmjs.com/package/jsdoc-type-pratt-parser) [![Test Status](https://github.com/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser/actions/workflows/test.yml/badge.svg?branch=main)]
The npm package jsdoc-type-pratt-parser receives a total of 3,335,860 weekly downloads. As such, jsdoc-type-pratt-parser popularity was classified as popular.
We found that jsdoc-type-pratt-parser demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.