Parse code comments from JavaScript or any language that uses the same format.
npm install parse-comments> Parse code comments from JavaScript or any language that uses the same format.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.
Install with npm:
``sh`
$ npm install --save parse-comments
`js`
const Comments = require('parse-comments');
const comments = new Comments();
const ast = comments.parse(str);
console.log(ast);
Parses a comment like this:
`jsCustomClass
/**
* Create an instance of with the given options.
*
* @param {String} options
* @api public
*/
class CustomClass {
constructor(options) {
this.options = options;
}
set(type, fn) {
// do stuff
}
}
`
Into an array of comment objects, like this:
`jsCustomClass
[
{
type: 'BlockComment',
value: '\nCreate an instance of with the given options.\n\n@param {String} options\n@api public',CustomClass
range: [0, 117],
loc: { start: { line: 1, column: 0 }, end: { line: 6, column: 3 } },
codeStart: 119,
raw:
'\n Create an instance of with the given options.\n \n @param {String} options\n * @api public\n ',CustomClass
code: {
context: {
type: 'class',
ctor: 'CustomClass',
name: 'CustomClass',
extends: undefined,
string: 'new CustomClass()'
},
value: 'class CustomClass {',
range: [119, 138],
loc: { start: { line: 8, column: 0 }, end: { line: 8, column: 19 } }
},
description: 'Create an instance of with the given options.',`
footer: '',
examples: [],
tags: [
{
title: 'param',
name: 'options',
description: '',
type: { type: 'NameExpression', name: 'String' }
},
{ title: 'api', name: 'public', description: '' }
],
inlineTags: []
}
]
Create an instance of Comments with the given options.
Params
* {Object}: options
Example
`js`
const Comments = require('parse-comments');
const comments = new Comments();
Register a parser function of the given type
Params
* type {string|object}fn
* {Function}returns
* {Object}
Params
* fn {Function}: plugin functionreturns
* {Object}: Returns the comments instance for chaining.
Example
`js`
// plugin example
function yourPlugin(options) {
return function(comments) {
// do stuff
};
}
// usage
comments.use(yourPlugin());
Params
* type {String}: The node.type to call the handler on. You can override built-in middleware by registering a handler of the same name, or register a handler for rendering a new type.fn
* {Function}: The handler functionreturns
* {Object}: Returns the instance for chaining.
Example
`js`
comments.set('param', function(node) {
// do stuff to node
});
Params
* type {String|Object|Array}: Handler name(s), or an object of middlewarefn
* {Function}: Handler function, if type is a string or array. Otherwise this argument is ignored.returns
* {Object}: Returns the instance for chaining.
Example
`js
comments.before('param', function(node) {
// do stuff to node
});
// or
comments.before(['param', 'returns'], function(node) {
// do stuff to node
});
// or
comments.before({
param: function(node) {
// do stuff to node
},
returns: function(node) {
// do stuff to node
}
});
`
Params
* type {String|Object|Array}: Handler name(s), or an object of middlewarefn
* {Function}: Handler function, if type is a string or array. Otherwise this argument is ignored.returns
* {Object}: Returns the instance for chaining.
Example
`js
comments.after('param', function(node) {
// do stuff to node
});
// or
comments.after(['param', 'returns'], function(node) {
// do stuff to node
});
// or
comments.after({
param: function(node) {
// do stuff to node
},
returns: function(node) {
// do stuff to node
}
});
`
Params
* javascript {String}: String of javascriptoptions
* {Object}returns
* {Object}: Returns an object with description string, array of examples, array of tags (strings), and a footer if descriptions are defined both before and after tags.
Example
`js`
const parser = new ParseComments();
const tokens = parser.tokenize([string]);
Params
* str {String}: String of javascriptoptions
* {Object}returns
* {Array}: Array of objects.
Example
`js`
const parser = new ParseComments();
const comments = parser.parse(string);
Params
* str {String}: JavaScript commentoptions
* {Object}returns
* {Object}: Parsed comment object
Example
`js`
let parser = new ParseComments();
let comments = parser.parseComment(string);
`js
Params
* {}: {String}
* {String}: name
* {String}: name The name to use for foo ` returns
* {Object}: tok Takes a token from
* {Object}
`js
Params
* {}: {String}
* {String}: name
* {String}: name The name to use for foo `returns
* {Object}: tok
* {Object}
`js
Params
* {}: {String}
* {}: {...string}
* {}: {function(...a)}
* {}: {function(...a:b)}
* {}: {String|Array}
* {}: {(String|Array)}
* {}: {{foo: bar}}
* {}: {String[]}
* ` {Arrayreturns
* {String}: value The
* {Object}
`js
Params
* {}: {String}
* {}: {String|Array}
* {}: {(String|Array)}
* {}: {{foo: bar}} `returns
* {string}: str The string to parse
* {object}
Returns true if the given comment is valid. By default, comments/**
are considered valid when they begin with , and do not containjslint, jshint, eshint, or eslint. A custom isValid function may be
passed on the constructor options.
Params
* comment {Object}options
* {Object}returns
* {Boolean}
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Please read the contributing guide for advice on opening issues, pull requests, and coding standards.
Running Tests
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
`sh`
$ npm install && npm test
Building docs
_(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)_
To generate the readme, run the following command:
`sh``
$ npm install -g verbose/verb#dev verb-generate-readme && verb
| Commits | Contributor |
| --- | --- |
| 35 | jonschlinkert |
| 4 | doowb |
Jon Schlinkert
* GitHub Profile
* Twitter Profile
* LinkedIn Profile
Copyright © 2018, Jon Schlinkert.
Released under the MIT License.
*
_This file was generated by verb-generate-readme, v0.8.0, on November 24, 2018._