Skip to content

kmizu/jsdoctypeparser

 
 

Repository files navigation

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

About

Parser module for JsDoc or Closure Compiler format type-annotation.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%