@cjser/jsdoc-type-pratt-parser 9.1.1-cjser.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +194 -0
- package/dist/index.d.mts +733 -0
- package/dist/index.mjs +3091 -0
- package/dist-cjser/index.cjs +3027 -0
- package/package.json +159 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Simon Seyock
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
[](https://www.npmjs.com/package/jsdoc-type-pratt-parser)
|
|
2
|
+
[](https://github.com/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser/actions?query=branch%3Amain)
|
|
3
|
+
[](https://coveralls.io/github/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser?branch=main)
|
|
4
|
+
[](https://github.com/standard/ts-standard)
|
|
5
|
+
[](https://github.com/semantic-release/semantic-release)
|
|
6
|
+
|
|
7
|
+
# Jsdoc Type Pratt Parser
|
|
8
|
+
|
|
9
|
+
This project is a parser for jsdoc types. It takes jsdoc type expressions like `Array<string>` and creates an abstract
|
|
10
|
+
syntax tree (AST) out of it. It is heavily inspired by the existing libraries [catharsis](https://github.com/hegemonic/catharsis) and [jsdoctypeparser](https://github.com/jsdoctypeparser/jsdoctypeparser), but does
|
|
11
|
+
not use [PEG.js](https://pegjs.org/), instead it is written as a pratt parser.
|
|
12
|
+
|
|
13
|
+
You can find some more information about pratt parsers here:
|
|
14
|
+
* https://en.wikipedia.org/wiki/Operator-precedence_parser#Pratt_parsing
|
|
15
|
+
* http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/
|
|
16
|
+
* https://matklad.github.io/2020/04/13/simple-but-powerful-pratt-parsing.html
|
|
17
|
+
|
|
18
|
+
## Table of Contents
|
|
19
|
+
- [Live Demo](#live-demo)
|
|
20
|
+
- [Getting Started](#getting-started)
|
|
21
|
+
- [API Documentation](#api-documentation)
|
|
22
|
+
- [Available Grammars](#available-grammars)
|
|
23
|
+
- [Transforms](#transforms)
|
|
24
|
+
- [Traverse](#traverse)
|
|
25
|
+
- [Custom Parsers and Stringifiers](#custom-parsers-and-stringifiers)
|
|
26
|
+
- [Tests Status](#tests-status)
|
|
27
|
+
- [Performance](#performance)
|
|
28
|
+
- [Development](#development)
|
|
29
|
+
|
|
30
|
+
## Live Demo
|
|
31
|
+
|
|
32
|
+
A simple live demo to test expressions can be found at: https://jsdoc-type-pratt-parser.github.io/jsdoc-type-pratt-parser/
|
|
33
|
+
|
|
34
|
+
## Getting started
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
npm install jsdoc-type-pratt-parser
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
```js
|
|
41
|
+
import { parse } from 'jsdoc-type-pratt-parser'
|
|
42
|
+
|
|
43
|
+
const result = parse('SomeType<string>', 'typescript')
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## API Documentation
|
|
47
|
+
|
|
48
|
+
An API documentation can be found [here](https://jsdoc-type-pratt-parser.github.io/jsdoc-type-pratt-parser/docs/).
|
|
49
|
+
It is incomplete, but a good starting point. Please create issues or PRs if you don't find what you expect.
|
|
50
|
+
|
|
51
|
+
## Available Grammars
|
|
52
|
+
|
|
53
|
+
Three different modes (grammars) are supported: `'jsdoc'`, `'closure'` and `'typescript'`
|
|
54
|
+
|
|
55
|
+
## Transforms
|
|
56
|
+
|
|
57
|
+
A common task to do on ASTs are transforms, for example a stringification. This library includes some transform and
|
|
58
|
+
utilities to implement your own.
|
|
59
|
+
|
|
60
|
+
[`stringify`](https://jsdoc-type-pratt-parser.github.io/jsdoc-type-pratt-parser/docs/index.html#stringify):
|
|
61
|
+
|
|
62
|
+
```js
|
|
63
|
+
import { stringify } from 'jsdoc-type-pratt-parser'
|
|
64
|
+
|
|
65
|
+
const val = stringify({ type: 'JsdocTypeName', value: 'name'}) // -> 'name'
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
You can customize the stringification by using [`stringifyRules`](https://jsdoc-type-pratt-parser.github.io/jsdoc-type-pratt-parser/docs/index.html#stringifyRules)
|
|
69
|
+
and [`transform`](https://jsdoc-type-pratt-parser.github.io/jsdoc-type-pratt-parser/docs/index.html#transform):
|
|
70
|
+
|
|
71
|
+
```js
|
|
72
|
+
import { stringifyRules, transform } from 'jsdoc-type-pratt-parser'
|
|
73
|
+
|
|
74
|
+
const rules = stringifyRules()
|
|
75
|
+
|
|
76
|
+
// `result` is the current node and `transform` is a function to transform child nodes.
|
|
77
|
+
rules.NAME = (result, transform) => 'something else'
|
|
78
|
+
|
|
79
|
+
const val = transform(rules, { type: 'JsdocTypeName', value: 'name'}) // -> 'something else'
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
You can also build your own transform rules by implementing the [`TransformRules<TransformResultType>`](https://jsdoc-type-pratt-parser.github.io/jsdoc-type-pratt-parser/docs/index.html#TransformRules) interface or you
|
|
83
|
+
can build upon the [identity ruleset](https://jsdoc-type-pratt-parser.github.io/jsdoc-type-pratt-parser/docs/index.html#identityTransformRules) like this:
|
|
84
|
+
|
|
85
|
+
```js
|
|
86
|
+
import { identityTransformRules, transform } from 'jsdoc-type-pratt-parser'
|
|
87
|
+
|
|
88
|
+
const myRules = identityTransformRules()
|
|
89
|
+
myRules.NAME = () => ({ type: 'JsdocTypeName', value: 'funky' })
|
|
90
|
+
|
|
91
|
+
const val = transform(myRules, result)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
This library also supports compatibility modes for catharsis and jsdoctypeparser. The provided transform functions attempt to
|
|
95
|
+
transform the output to the expected output of the target library. This will not always be the same as some types are
|
|
96
|
+
parsed differently. These modes are thought to make transition easier, but it is advised to use the native output as
|
|
97
|
+
this will be more uniform and will contain more information.
|
|
98
|
+
|
|
99
|
+
[Catharsis compat mode](https://jsdoc-type-pratt-parser.github.io/jsdoc-type-pratt-parser/docs/index.html#catharsisTransform):
|
|
100
|
+
|
|
101
|
+
```js
|
|
102
|
+
import { parse, catharsisTransform } from 'jsdoc-type-pratt-parser'
|
|
103
|
+
|
|
104
|
+
const result = catharsisTransform(parse('myType.<string>', 'closure'))
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
[Jsdoctypeparser compat mode](https://jsdoc-type-pratt-parser.github.io/jsdoc-type-pratt-parser/docs/index.html#jtpTransform):
|
|
108
|
+
|
|
109
|
+
```js
|
|
110
|
+
import { parse, jtpTransform } from 'jsdoc-type-pratt-parser'
|
|
111
|
+
|
|
112
|
+
const result = jtpTransform(parse('myType.<string>', 'closure'))
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Traverse
|
|
116
|
+
|
|
117
|
+
You can traverse an AST with the [`traverse`](https://jsdoc-type-pratt-parser.github.io/jsdoc-type-pratt-parser/docs/index.html#traverse) function:
|
|
118
|
+
|
|
119
|
+
```js
|
|
120
|
+
import { traverse } from 'jsdoc-type-pratt-parser'
|
|
121
|
+
|
|
122
|
+
// property is the name of the property on parent that contains node
|
|
123
|
+
function onEnter(node, parent, property) {
|
|
124
|
+
console.log(node.type)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// an onEnter and/or an onLeave function can be supplied
|
|
128
|
+
traverse({ type: 'JsdocTypeName', value: 'name'}, onEnter, console.log)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Custom Parsers and Stringifiers
|
|
132
|
+
|
|
133
|
+
For TypeScript's computed property and methods, access to JavaScript
|
|
134
|
+
(or TypeScript) parsing and stringification is required (beyond the
|
|
135
|
+
types supported in `jsdoc-type-pratt-parser`). You can provide these
|
|
136
|
+
by supplying the following optional arguments:
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
import { parse as espree } from 'espree'
|
|
140
|
+
import { generate } from '@es-joy/escodegen'
|
|
141
|
+
|
|
142
|
+
const parsed = parse(
|
|
143
|
+
input,
|
|
144
|
+
mode,
|
|
145
|
+
{ computedPropertyParser: espree }
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
const stringified = stringify(
|
|
149
|
+
result,
|
|
150
|
+
generate
|
|
151
|
+
)
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Tests Status
|
|
155
|
+
|
|
156
|
+
This parser runs most tests of [catharsis](https://github.com/hegemonic/catharsis) and
|
|
157
|
+
[jsdoctypeparser](https://github.com/jsdoctypeparser/jsdoctypeparser). It compares the results of the different parsing libraries. If you
|
|
158
|
+
want to find out where the output differs, look in the tests for the comments `// This seems to be an error of ...` or
|
|
159
|
+
the `differ` keyword which indicates that differing results are produced.
|
|
160
|
+
|
|
161
|
+
## Performance
|
|
162
|
+
|
|
163
|
+
A simple [performance comparison](benchmark/benchmark.js) using [Benchmark.js](https://benchmarkjs.com/) produced the following results:
|
|
164
|
+
```
|
|
165
|
+
Testing expression: Name
|
|
166
|
+
catharsis x 37,816 ops/sec ±1.22% (1086 runs sampled)
|
|
167
|
+
jsdoc-type-pratt-parser x 602,617 ops/sec ±0.16% (1090 runs sampled)
|
|
168
|
+
jsdoctypeparser x 53,256 ops/sec ±0.73% (1081 runs sampled)
|
|
169
|
+
The fastest was jsdoc-type-pratt-parser
|
|
170
|
+
|
|
171
|
+
Testing expression: Array<number>
|
|
172
|
+
catharsis x 10,124 ops/sec ±0.56% (1084 runs sampled)
|
|
173
|
+
jsdoc-type-pratt-parser x 228,660 ops/sec ±0.40% (1084 runs sampled)
|
|
174
|
+
jsdoctypeparser x 42,365 ops/sec ±0.60% (1070 runs sampled)
|
|
175
|
+
The fastest was jsdoc-type-pratt-parser
|
|
176
|
+
|
|
177
|
+
Testing expression: { keyA: Type<A | "string val" >, keyB: function(string, B): A }
|
|
178
|
+
catharsis x 1,138 ops/sec ±0.66% (1087 runs sampled)
|
|
179
|
+
jsdoc-type-pratt-parser x 46,535 ops/sec ±0.47% (1090 runs sampled)
|
|
180
|
+
jsdoctypeparser x 18,291 ops/sec ±0.71% (1084 runs sampled)
|
|
181
|
+
The fastest was jsdoc-type-pratt-parser
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
The benchmark test uses catharsis without cache.
|
|
185
|
+
|
|
186
|
+
## Development
|
|
187
|
+
|
|
188
|
+
If you want to contribute see the [Development Guide](DEVELOPMENT.md) to get some pointers. Feel free to create issues if
|
|
189
|
+
there is information missing.
|
|
190
|
+
|
|
191
|
+
## cjser
|
|
192
|
+
|
|
193
|
+
This package is a CommonJS-compatible build generated by cjser for projects that still need `require()` support. The source version matches the original npm package version, with a cjser prerelease suffix for this generated build.
|
|
194
|
+
Original repository: https://github.com/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser
|