Awesome
acorn-extract-comments
Extract JavaScript code comments from a string, using
acorn
.
Install
npm i acorn-extract-comments --save
Usage
For more use-cases see the tests
const extract = require('acorn-extract-comments')
acornExtractComments
Extract all code comments, including block/line and also these that are marked as "ignored" like (
//!
and/*!
)
Params
<input>
{String}: string from which to get comments[opts]
{Object}: optional options, passed toacorn
ast
{Boolean}: iftrue
the ast is added to the resulting arrayline
{Boolean}: iffalse
get only block comments, defaulttrue
block
{Boolean}: iffalse
get line comments, defaulttrue
preserve
{Boolean|Function}: iftrue
will get only comments that are not ignoredlocations
{Boolean}: iftrue
result will includeacorn
location objectecmaVersion
{Number}: defaults to6
, acorn parsing version
returns
{Array}: can have.ast
property ifopts.ast: true
Example
const fs = require('fs')
const extract = require('acorn-extract-comments')
const str = fs.readFileSync('./index.js', 'utf8')
const comments = extract(str, {})
// => ['array', 'of', 'all', 'code', 'comments']
.line
Extract only line comments.
Params
<input>
{String}: string from which to get comments[opts]
{Object}: optional options, passed toacorn
returns
{Array}: can have.ast
property ifopts.ast: true
Example
const comments = extract(str, {block: false})
// => ['array', 'of', 'line', 'comments']
or through method
const comments = extract.line(str)
// => ['all', 'line', 'comments']
.block
Extract only block comments.
Params
<input>
{String}: string from which to get comments[opts]
{Object}: optional options, passed toacorn
returns
{Array}: can have.ast
property ifopts.ast: true
Examples
const comments = extract(str, {line: false})
// => ['array', 'of', 'block', 'comments']
or through method
const comments = extract.block(str)
// => ['array', 'of', 'block', 'comments']
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
But before doing anything, please read the CONTRIBUTING.md guidelines.