Socket
Socket
Sign inDemoInstall

jsdoctypeparser

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsdoctypeparser

Strict JsDoc type expression parser.


Version published
Weekly downloads
239K
decreased by-18.25%
Maintainers
1
Install size
1.03 MB
Created
Weekly downloads
 

Package description

What is jsdoctypeparser?

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.

What are jsdoctypeparser's main functionalities?

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));

Other packages similar to jsdoctypeparser

Readme

Source

Jsdoc type parser

Build Status NPM version

This module is Jsdoc type expression parser, it makes easy to publish a type name link by toHTML().

This parser provide:

  • Parse to object model
  • Convert a type name to a link by using toHtml()
var Parser = require('jsdoctypeparser').Parser;
var parser = new Parser();
var result = parser.parse('Array.<MyClass>=');

console.log(result.toHtml()); // ⇒ 'Array.&lt;<a href="MyClass.html">MyClass</a>&gt;|undefined'
console.log(result.toString()); // ⇒ 'Array.<MyClass>|undefined'

This parser can parse:

Live demo

The live demo is available.

Publishing

var Parser = require('jsdoctypeparser').Parser;
var parser = new Parser();
var result = parser.parse('Array.<MyClass>=');
  • result.toString()'Array.<MyClass>|undefined'

  • result.toHtml()'Array.&lt;<a href="MyClass.html">MyClass</a>&gt;|undefined'

Customize type name URI

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;
}; 

Parsing

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' }
    }
  ]
}

Specification

Type name
TypeName = {
  name: string
};
Type Union
TypeUnion = {
  optional: boolean,
  nullable: boolean,
  variable: boolean,
  nonNullable: boolean,
  all: boolean,
  unknown: boolean,
  types: Array.<TypeName|GenericType|FunctionType|RecordType>
};
Generic type
GenericType = {
  genericTypeName: string,
  parameterTypeUnions: Array.<TypeUnion>
};
Function type
FunctionType = {
  parameterTypeUnions: Array.<TypeUnion>,
  returnTypeUnion: TypeUnion|null,
  isConstructor: boolean,
  contextTypeUnion: TypeUnion|null
};
Record type
RecordType = {
  entries: Array.<RecordEntry>
};

RecordType.Entry = {
  name: string,
  typeUnion: TypeUnion
};

License

This script licensed under the MIT. See: http://orgachem.mit-license.org

Keywords

FAQs

Package last updated on 11 May 2015

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc