Skip to content

Extract JavaScript code (block, jsdoc style) comments from a string, using `acorn`. Optionally returns the AST and first line of code after comment - useful for parsing code context and api docs.

License

Notifications You must be signed in to change notification settings

tunnckoCore/acorn-extract-comments

Repository files navigation

Extract JavaScript code comments from a string, using acorn.

code climate standard code style travis build status coverage status dependency status

Install

npm i acorn-extract-comments --save

Usage

For more use-cases see the tests

const extract = require('acorn-extract-comments')

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 to acorn
    • ast {Boolean}: if true the ast is added to the resulting array
    • line {Boolean}: if false get only block comments, default true
    • block {Boolean}: if false get line comments, default true
    • preserve {Boolean|Function}: if true will get only comments that are not ignored
    • locations {Boolean}: if true result will include acorn location object
    • ecmaVersion {Number}: defaults to 6, acorn parsing version
  • returns {Array}: can have .ast property if opts.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']

Extract only line comments.

Params

  • <input> {String}: string from which to get comments
  • [opts] {Object}: optional options, passed to acorn
  • returns {Array}: can have .ast property if opts.ast: true

Example

const comments = extract(str, {block: false})
// => ['array', 'of', 'line', 'comments']

or through method

const comments = extract.line(str)
// => ['all', 'line', 'comments']

Extract only block comments.

Params

  • <input> {String}: string from which to get comments
  • [opts] {Object}: optional options, passed to acorn
  • returns {Array}: can have .ast property if opts.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.

tunnckocore.tk keybase tunnckocore tunnckoCore npm tunnckoCore twitter tunnckoCore github

About

Extract JavaScript code (block, jsdoc style) comments from a string, using `acorn`. Optionally returns the AST and first line of code after comment - useful for parsing code context and api docs.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published