UNPKG

6.62 kBMarkdownView Raw
1[![NPM version][npm-image]][npm-url]
2[![build status][travis-image]][travis-url]
3[![Test coverage][coveralls-image]][coveralls-url]
4[![Downloads][downloads-image]][downloads-url]
5[![Join the chat at https://gitter.im/eslint/doctrine](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/eslint/doctrine?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
6
7# Doctrine
8
9Doctrine is a [JSDoc](http://usejsdoc.org) parser that parses documentation comments from JavaScript (you need to pass in the comment, not a whole JavaScript file).
10
11## Installation
12
13You can install Doctrine using [npm](https://npmjs.com):
14
15```
16$ npm install doctrine --save-dev
17```
18
19Doctrine can also be used in web browsers using [Browserify](http://browserify.org).
20
21## Usage
22
23Require doctrine inside of your JavaScript:
24
25```js
26var doctrine = require("doctrine");
27```
28
29### parse()
30
31The primary method is `parse()`, which accepts two arguments: the JSDoc comment to parse and an optional options object. The available options are:
32
33* `unwrap` - set to `true` to delete the leading `/**`, any `*` that begins a line, and the trailing `*/` from the source text. Default: `false`.
34* `tags` - an array of tags to return. When specified, Doctrine returns only tags in this array. For example, if `tags` is `["param"]`, then only `@param` tags will be returned. Default: `null`.
35* `recoverable` - set to `true` to keep parsing even when syntax errors occur. Default: `false`.
36* `sloppy` - set to `true` to allow optional parameters to be specified in brackets (`@param {string} [foo]`). Default: `false`.
37* `lineNumbers` - set to `true` to add `lineNumber` to each node, specifying the line on which the node is found in the source. Default: `false`.
38* `range` - set to `true` to add `range` to each node, specifying the start and end index of the node in the original comment. Default: `false`.
39
40Here's a simple example:
41
42```js
43var ast = doctrine.parse(
44 [
45 "/**",
46 " * This function comment is parsed by doctrine",
47 " * @param {{ok:String}} userName",
48 "*/"
49 ].join('\n'), { unwrap: true });
50```
51
52This example returns the following AST:
53
54 {
55 "description": "This function comment is parsed by doctrine",
56 "tags": [
57 {
58 "title": "param",
59 "description": null,
60 "type": {
61 "type": "RecordType",
62 "fields": [
63 {
64 "type": "FieldType",
65 "key": "ok",
66 "value": {
67 "type": "NameExpression",
68 "name": "String"
69 }
70 }
71 ]
72 },
73 "name": "userName"
74 }
75 ]
76 }
77
78See the [demo page](http://eslint.org/doctrine/demo/) more detail.
79
80## Team
81
82These folks keep the project moving and are resources for help:
83
84* Nicholas C. Zakas ([@nzakas](https://github.com/nzakas)) - project lead
85* Yusuke Suzuki ([@constellation](https://github.com/constellation)) - reviewer
86
87## Contributing
88
89Issues and pull requests will be triaged and responded to as quickly as possible. We operate under the [ESLint Contributor Guidelines](http://eslint.org/docs/developer-guide/contributing), so please be sure to read them before contributing. If you're not sure where to dig in, check out the [issues](https://github.com/eslint/doctrine/issues).
90
91## Frequently Asked Questions
92
93### Can I pass a whole JavaScript file to Doctrine?
94
95No. Doctrine can only parse JSDoc comments, so you'll need to pass just the JSDoc comment to Doctrine in order to work.
96
97
98### License
99
100#### doctrine
101
102Copyright JS Foundation and other contributors, https://js.foundation
103
104Licensed under the Apache License, Version 2.0 (the "License");
105you may not use this file except in compliance with the License.
106You may obtain a copy of the License at
107
108 http://www.apache.org/licenses/LICENSE-2.0
109
110Unless required by applicable law or agreed to in writing, software
111distributed under the License is distributed on an "AS IS" BASIS,
112WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
113See the License for the specific language governing permissions and
114limitations under the License.
115
116#### esprima
117
118some of functions is derived from esprima
119
120Copyright (C) 2012, 2011 [Ariya Hidayat](http://ariya.ofilabs.com/about)
121 (twitter: [@ariyahidayat](http://twitter.com/ariyahidayat)) and other contributors.
122
123Redistribution and use in source and binary forms, with or without
124modification, are permitted provided that the following conditions are met:
125
126 * Redistributions of source code must retain the above copyright
127 notice, this list of conditions and the following disclaimer.
128
129 * Redistributions in binary form must reproduce the above copyright
130 notice, this list of conditions and the following disclaimer in the
131 documentation and/or other materials provided with the distribution.
132
133THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
134AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
135IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
136ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
137DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
138(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
139LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
140ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
141(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
142THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
143
144
145#### closure-compiler
146
147some of extensions is derived from closure-compiler
148
149Apache License
150Version 2.0, January 2004
151http://www.apache.org/licenses/
152
153
154### Where to ask for help?
155
156Join our [Chatroom](https://gitter.im/eslint/doctrine)
157
158[npm-image]: https://img.shields.io/npm/v/doctrine.svg?style=flat-square
159[npm-url]: https://www.npmjs.com/package/doctrine
160[travis-image]: https://img.shields.io/travis/eslint/doctrine/master.svg?style=flat-square
161[travis-url]: https://travis-ci.org/eslint/doctrine
162[coveralls-image]: https://img.shields.io/coveralls/eslint/doctrine/master.svg?style=flat-square
163[coveralls-url]: https://coveralls.io/r/eslint/doctrine?branch=master
164[downloads-image]: http://img.shields.io/npm/dm/doctrine.svg?style=flat-square
165[downloads-url]: https://www.npmjs.com/package/doctrine