Skip to content

tunnckoCore/acorn-strip-comments

Repository files navigation

Strip code comments from a string, using acorn javascript parser.

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

Install

npm i acorn-strip-comments --save

Usage

For more use-cases see the tests

const acornStripComments = require('acorn-strip-comments')

Strip all code comments, but not these that are marked as "ignored" like (//! and /*!).
Pass opts.preserve: false to remove even them.

Params

  • <input> {String}: string from which to get comments
  • [opts] {Object}: optional options, passed to acorn-extract-comments and 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
    • ignore {Function}: check function, default check comment starts with !
    • preserve {Boolean}: if true keep comments that are ignored (that pass the opts.ignore check)
    • locations {Boolean}: if true result will include acorn/esprima format comment location object
    • ecmaVersion {Number}: defaults to 6, acorn parsing version
  • returns {String}: modified string

Example

const fs = require('fs')
const strip = require('acorn-strip-comments')

const str = fs.readFileSync('./index.js', 'utf8')
const output = strip(str, {})
// => modified and cleaned string

Remove only line comments.

Params

  • <input> {String}: string from which to get comments
  • [opts] {Object}: optional options, passed to acorn-extract-comments and acorn
  • returns {String}: modified string

Examples

const output = strip(str, {block: false})
// => modified and cleaned string

or through method

const output = strip.line(str)
// => modified and cleaned string

Remove only block comments.

Params

  • <input> {String}: string from which to get comments
  • [opts] {Object}: optional options, passed to acorn-extract-comments and acorn
  • returns {String}: modified string

Examples

const output = strip(str, {line: false})
// => modified and cleaned string

or through method

const output = strip.block(str)
// => modified and cleaned string

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

Strip code comments from given input string, using acorn javascript parser

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published