Strict JsDoc type expression parser.
npm install jsdoctypeparser

The parser can parse:
* JSDoc type expressions
* foo.bar, String[]
* Closure Compiler type expressions
* Array, function(arg1, arg2): ret
* some Typescript types
* (x: number) => string, typeof x, import("./some-module")
* Complex type expressions
* Array, function(function(Function))
The live demo is available.
``javascript
const {parse} = require('jsdoctypeparser');
const ast = parse('Array
`
The ast becomes:
`json`
{
"type": "GENERIC",
"subject": {
"type": "NAME",
"name": "Array"
},
"objects": [
{
"type": "NAME",
"name": "MyClass"
}
],
"meta": {
"syntax": "ANGLE_BRACKET"
}
}
See the AST specifications.
We can stringify the AST nodes by using publish.
`javascript
const {publish} = require('jsdoctypeparser');
const ast = {
type: 'GENERIC',
subject: {
type: 'NAME',
name: 'Array'
},
objects: [
{
type: 'NAME',
name: 'MyClass'
}
]
};
const string = publish(ast);
`
The string becomes:
`json`
"Array
#### Custom publishing
We can change the stringification strategy by using the 2nd parameter of publish(node, publisher).publisher
The MUST have handlers for all node types (see lib/NodeType.js).
And we can override default behavior by using createDefaultPublisher.
`javascript
const {publish, createDefaultPublisher} = require('jsdoctypeparser');
const ast = {
type: 'NAME',
name: 'MyClass',
};
const customPublisher = createDefaultPublisher();
customPublisher.NAME = (node, pub) =>
${node.name};
const string = publish(ast, customPublisher);
`
The string becomes:
`html`
MyClass
We can traverse the AST by using traverse.
This function takes 3 parameters (a node and an onEnter handler, an onLeave handler).
The handlers take a visiting node.
`javascript
const {parse, traverse} = require('jsdoctypeparser');
const ast = parse('Array<{ key1: function(), key2: A.B.C }>');
function onEnter(node, parentName, parentNode) {
console.log('enter', node.type, parentName, parentNode.type);
}
function onLeave(node, parentName, parentNode) {
console.log('leave', node.type, parentName, parentNode.type);
}
traverse(ast, onEnter, onLeave);
`
The output will be:
``
enter GENERIC null null
enter NAME subject GENERIC
leave NAME subject GENERIC
enter RECORD objects GENERIC
enter RECORD_ENTRY entries RECORD
enter FUNCTION value RECORD_ENTRY
leave FUNCTION value RECORD_ENTRY
leave RECORD_ENTRY entries RECORD
enter RECORD_ENTRY entries RECORD
enter MEMBER value RECORD_ENTRY
enter MEMBER owner MEMBER
enter NAME owner MEMBER
leave NAME owner MEMBER
leave MEMBER owner MEMBER
leave MEMBER value RECORD_ENTRY
leave RECORD_ENTRY entries RECORD
leave RECORD objects GENERIC
leave GENERIC null null
Example:
`javascript`
/**
* @type {name}
*/
Structure:
`javascript`
{
"type": "NAME",
"name": string
}
Example:
`javascript`
/**
* @type {owner.name}
* @type {superOwner.owner.name}
*/
Structure:
`javascript`
{
"type": "MEMBER",
"name": string,
"quoteStyle": "none",
"owner": node,
"hasEventPrefix": boolean
}
Example:
`javascript`
/**
* @type {owner~name}
*/
Structure:
`javascript`
{
"type": "INNER_MEMBER",
"name": string,
"quoteStyle": "none",
"owner": node,
"hasEventPrefix": boolean
}
Example:
`javascript`
/**
* @type {owner#name}
*/
Structure:
`javascript`
{
"type": "INSTANCE_MEMBER",
"name": string,
"quoteStyle": "none",
"owner": node,
"hasEventPrefix": boolean
}
Example:
`javascript`
/**
* @type {left|right}
* @type {(left|right)}
*/
Structure:
`javascript`
{
"type": "UNION",
"left": node,
"right": node
}
Example:
`javascript`
/**
* @type {left&right}
* @type {(left&right)}
*/
Structure:
`javascript`
{
"type": "INTERSECTION",
"left": node,
"right": node
}
Example:
`javascript`
/**
* @type {{}}
* @type {{ key: value }}
* @type {{ key: value, anyKey }}
*/
Structure:
`javascript`
{
"type": "RECORD",
"entries": [
recordEntryNode,
recordEntryNode,
...
]
}
Structure:
`javascript`
{
"type": "RECORD_ENTRY",
"key": string,
"value": node (or null)
}
Example:
`javascript`
/**
* @type {Subject
Structure:
`javascript`
{
"type": "GENERIC",
"subject": node,
"objects": [
node,
node,
...
],
"meta": {
"syntax": ("ANGLE_BRACKET" or "ANGLE_BRACKET_WITH_DOT" or "SQUARE_BRACKET")
}
}
Example:
`javascript`
/**
* @type {function()}
* @type {function(param, param): return}
* @type {function(this: Context)}
* @type {function(new: Class)}
*/
Structure:
`javascript`
{
"type": "FUNCTION",
"params": [
node,
node,
...
],
"returns": node (or null),
"new": node (or null),
"this": node (or null)
}
Example:
`javascript`
/**
* @type {Optional=}
*/
Structure:
`javascript`
{
"type": "OPTIONAL",
"value": node,
"meta": {
"syntax": ("PREFIX_EQUALS_SIGN" or "SUFFIX_EQUALS_SIGN")
}
}
Example:
`javascript`
/**
* @type {?Nullable}
*/
Structure:
`javascript`
{
"type": "NULLABLE",
"value": node,
"meta": {
"syntax": ("PREFIX_QUESTION_MARK" or "SUFFIX_QUESTION_MARK")
}
}
Example:
`javascript`
/**
* @type {!NotNullable}
*/
Structure:
`javascript`
{
"type": "NOT_NULLABLE",
"value": node,
"meta": {
"syntax": ("PREFIX_BANG" or "SUFFIX_BANG")
}
}
Example:
`javascript`
/**
* @type {...Variadic}
* @type {Variadic...}
* @type {...}
*/
Structure:
`javascript`
{
"type": "VARIADIC",
"value": node (or null),
"meta": {
"syntax": ("PREFIX_DOTS" or "SUFFIX_DOTS" or "ONLY_DOTS")
}
}
Example:
`javascript`
/**
* @type {module:path/to/file.Module}
*/
Structure:
`javascript`
{
"type": "MODULE",
"value": node
}
Example:
`javascript`
/**
* @type {module:path/to/file.Module}
* ^^^^^^^^^^^^
*/
Structure:
`javascript`
{
"type": "FILE_PATH",
"path": string
}
Example:
`javascript`
/**
* @type {external:External}
*/
Structure:
`javascript`
{
"type": "EXTERNAL",
"value": node
}
Example:
`javascript`
/**
* @type {"abc"}
* @type {"can\"escape"}
*/
Structure:
`javascript`
{
"type": "STRING_VALUE",
"quoteStyle": "double",
"string": string
}
Example:
`javascript`
/**
* @type {123}
* @type {0b11}
* @type {0o77}
* @type {0xff}
*/
Structure:
`javascript`
{
"type": "NUMBER_VALUE",
"number": string
}
Example:
`javascript`
/**
@type {}
*/
Structure:
`javascript`
{
"type": "ANY"
}
Example:
`javascript`
/**
* @type {?}
*/
Structure:
`javascript`
{
"type": "UNKNOWN"
}
Example:
`javascript`
/**
* @type {(Foo)}
*/
Structure:
`javascript`
{
"type": "PARENTHESIS",
"value": node
}
We can use a parenthesis to change operator orders.
`javascript`
/**
* @type {(module:path/to/file.js).foo}
*/
To parse a type into a JSON structure, you may pass a string argument
containing the structure to parse (with the JSON results equivalent to the
parsing example above):
``
jsdoctypeparser 'Array
Note: There is no need to prefix the path to the jsdoctypeparser binary,./node_modules/.bin/
e.g., with when you are running within one of thepackage.json scripts` or if you have installed the package globally.