Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

doc-parser

glayzzle496BSD-3-Clause0.4.9TypeScript support: included

Parses docblocks comments

doc, parser, annotation, docblock, documention, docs

readme

DocBlock & Annotations Parser

npm version Build Status Coverage Status XO code style Gitter

This library is a javascript LALR(1) parser that parses docblocks and extracts annotations under an structured syntax tree.

Install

npm install doc-parser --save

And simple usage :

var DocParser = require('doc-parser');
var reader = new DocParser();
var data = reader.parse('/** @hello world */');

Supported syntaxes

/**
 * Some description
 * @return boolean
 * @return map<string, SomeClass>
 * @author Ioan CHIRIAC <me@domain.com>
 * @throws Exception
 * @deprecated
 * @table('tableName', true)
 * @table(
 *   name='tableName',
 *   primary=true
 * )
 * @annotation1 @annotation2
 * @Target(["METHOD", "PROPERTY"])
 * @Attributes([
 *   @Attribute("stringProperty", type = "string"),
 *   @Attribute("annotProperty",  type = "SomeAnnotationClass"),
 * ])
 * @json({
 *   "key": "value",
 *   "object": { "inner": true },
 *   "list": [1, 2, 3]
 * })
 * <node>
 * Some inner multi line content
 * </node>
 */

AST structure

{
  kind: 'doc',
  summary: 'Some description retrieved from the first line of the coment',
  body: [
    {
      kind: 'return',
      type: 'void',
      description: 'Some extra informations'
    }
  ]
}

Declaring custom doc blocks

By default, doc-parser supports @return,@param,@throws and @deprecated doc blocks.

You can extend the support to any doc block :

// lets handle @global (type) (var) (description)
var DocParser = require('doc-parser');
var reader = new DocParser({
  'global': [
    {
      property: 'type',
      parser: 'type',
      optional: true
    },
    {
      property: 'what',
      parser: 'variable',
      optional: true
    },
    {
      property: 'description',
      parser: 'text',
      optional: true,
      default: ''
    }
  ]
});
var data = reader.parse('/** @global string some description */');

This will result in a new kind of doc block with the specified properties. Here a list of supported parsers :

  • type : a simple type, class name, or array of types
  • variable : a variable name
  • text : a line of text (will eat every token until the current line ends)
  • version: a semantic version
  • array : an array of items
  • object : a json object definition
  • boolean : a boolean
  • number : a number (integer or float)
  • string : a simple or double quoted text

Misc

This library is released under BSD-3 license clause.

changelog

Version 0.4.8 (2017-3-12)

Patches

...full changes

Version 0.4.7 (2017-2-21)

Patches

  • start a typescript definition: 1b4c39c

...full changes

Version 0.4.6 (2017-1-27)

Patches

...full changes

Version 0.4.5 (2017-1-27)

Minor Changes

...full changes

Version 0.4.4 (2017-1-20)

Patches

...full changes

Version 0.4.3 (2017-1-19)

Patches

  • avoid reserved keywords like constructor: f820f4d

...full changes

Version 0.4.2 (2017-1-19)

Patches

  • Update README.md: 6e49587
  • protect from reading an undefined char: 9453437

...full changes

Version 0.4.1 (2017-1-19)

Patches

  • fix infinite loop when parsing a block: 4a05f0b

...full changes

Version 0.4.0 (2017-1-10)

Major Changes

  • implement annotations & improve tests: af164d6

...full changes

Version 0.3.0 (2017-1-9)

Major Changes

  • rewrite parser structure (xo++): edda0c4
  • start to define generic parsers for grammar: ffbe9c3
  • rename package: 6b01724
  • implement type, text & tests: 7ff0b34

Minor Changes

  • fix xo on tests: ddde5f8
  • extract summary: 1134c84
  • add a backup function & implement float numbers: 0802ad5
  • add new symbols: 6216fe0
  • implement the variable reader: cf2eb94
  • implement semver parser & deprecated block + test: afedeff
  • implement object parsing: 9861453

Patches

...full changes

Version 0.2.2 (2017-1-5)

Patches

...full changes

Version 0.2.1 (2017-1-5)

Patches

...full changes

Version 0.2.0 (2017-1-5)

Major Changes

Minor Changes

...full changes

Version 0.1.0 (2017-1-5)

Major Changes

Patches