Security News
PyPI Slashes Malware Response Time: 90% of Issues Resolved in Under 24 Hours
PyPI has drastically improved its malware response times, resolving 90% of issues in under 24 hours and removing 900 projects since March 2024.
jsdoctypeparser
Advanced tools
Package description
The jsdoctypeparser npm package is a tool for parsing and manipulating JSDoc type expressions. It allows developers to parse JSDoc type strings into an abstract syntax tree (AST) and then manipulate or analyze these types programmatically.
Parsing JSDoc Type Strings
This feature allows you to parse a JSDoc type string into an abstract syntax tree (AST). The code sample demonstrates how to parse the type string 'Array.<string>' and output the resulting AST.
const { parse } = require('jsdoctypeparser');
const typeString = 'Array.<string>';
const ast = parse(typeString);
console.log(JSON.stringify(ast, null, 2));
Stringifying AST Back to JSDoc Type String
This feature allows you to convert an AST back into a JSDoc type string. The code sample shows how to parse a type string into an AST and then convert it back to a string.
const { parse, stringify } = require('jsdoctypeparser');
const typeString = 'Array.<string>';
const ast = parse(typeString);
const newTypeString = stringify(ast);
console.log(newTypeString);
Handling Complex Type Expressions
This feature demonstrates the ability to handle complex JSDoc type expressions. The code sample parses a complex type string representing an object with properties of different types.
const { parse } = require('jsdoctypeparser');
const complexTypeString = '{a: number, b: string|boolean}';
const ast = parse(complexTypeString);
console.log(JSON.stringify(ast, null, 2));
Doctrine is a popular library for parsing JSDoc comments, including type expressions. It provides a more comprehensive solution for parsing entire JSDoc comments, not just type strings. Compared to jsdoctypeparser, Doctrine offers broader functionality but may be more complex to use if you only need to parse type expressions.
Comment-parser is a library for parsing JavaScript comments, including JSDoc comments. It focuses on extracting and parsing comments from source code, including type annotations. While it provides similar functionality for parsing JSDoc types, it is more focused on the overall comment structure rather than just type expressions.
Readme
This module is Jsdoc type expression parser, it makes easy to publish a type name link by toHTML()
.
This parser provide:
toHtml()
var Parser = require('jsdoctypeparser').Parser;
var parser = new Parser();
var result = parser.parse('Array.<MyClass>=');
console.log(result.toHtml()); // ⇒ 'Array.<<a href="MyClass.html">MyClass</a>>|undefined'
console.log(result.toString()); // ⇒ 'Array.<MyClass>|undefined'
This parser can parse:
foo.bar
, String[]
Array.<string>
, function(this: Objext, arg1, arg2): ret
Array.<Array.<string>>
, function(function(Function))
The live demo is available.
var Parser = require('jsdoctypeparser').Parser;
var parser = new Parser();
var result = parser.parse('Array.<MyClass>=');
result.toString()
⇒ 'Array.<MyClass>|undefined'
result.toHtml()
⇒ 'Array.<<a href="MyClass.html">MyClass</a>>|undefined'
You can change a file URL by set TypeBulder.TypeName.getUrlByTypeName(typeName)
.
var Builder = require('jsdoctypeparser').Builder;
Bulder.TypeName.getUrlByTypeName = function(typeName) {
// do something.
return typeName;
};
var Parser = require('jsdoctypeparser').Parser;
var parser = new Parser();
var result = parser.parse('Array.<string|number, ?Object=>|string|undefined');
The result
is:
{ // instanceof TypeBuilder.TypeUnion
optional: true,
types: [
{ // instanceof TypeBuilder.FunctionType
parameterTypeUnions: [
{ // instanceof TypeBuilder.TypeUnion
types: [
{ name: 'string' }, // instanceof TypeBuilder.TypeName
{ name: 'number' } // instanceof TypeBuilder.TypeName
]
},
{ // instanceof TypeBuilder.TypeUnion
nullable: true
optional: true
types: [
{ name: 'Object' } // instanceof TypeBuilder.TypeName
]
}
]
}, { // instanceof TypeBuilder.TypeName
{ name: 'string' }
}
]
}
TypeName = {
name: string
};
TypeUnion = {
optional: boolean,
nullable: boolean,
variable: boolean,
nonNullable: boolean,
all: boolean,
unknown: boolean,
types: Array.<TypeName|GenericType|FunctionType|RecordType>
};
GenericType = {
genericTypeName: string,
parameterTypeUnions: Array.<TypeUnion>
};
FunctionType = {
parameterTypeUnions: Array.<TypeUnion>,
returnTypeUnion: TypeUnion|null,
isConstructor: boolean,
contextTypeUnion: TypeUnion|null
};
RecordType = {
entries: Array.<RecordEntry>
};
RecordType.Entry = {
name: string,
typeUnion: TypeUnion
};
This script licensed under the MIT. See: http://orgachem.mit-license.org
FAQs
Strict JsDoc type expression parser.
The npm package jsdoctypeparser receives a total of 180,962 weekly downloads. As such, jsdoctypeparser popularity was classified as popular.
We found that jsdoctypeparser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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 has drastically improved its malware response times, resolving 90% of issues in under 24 hours and removing 900 projects since March 2024.
Security News
Research
The Socket Research team breaks down an obfuscated script designed to facilitate unauthorized file uploads to multiple external services.
Security News
Node.js has automated its security release process, doubling the number of releases, and is re-evaluating unsupported experimental features with the Next 10 group to enhance security.