Awesome
acorn-strip-comments
Strip code comments from a string, using
acorn
javascript parser.
Install
npm i acorn-strip-comments --save
Usage
For more use-cases see the tests
const acornStripComments = require('acorn-strip-comments')
acornStripComments
Strip all code comments, but not these that are marked as "ignored" like (
//!
and/*!
).
Passopts.preserve: false
to remove even them.
Params
<input>
{String}: string from which to get comments[opts]
{Object}: optional options, passed toacorn-extract-comments
andacorn
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
ignore
{Function}: check function, default check comment starts with!
preserve
{Boolean}: iftrue
keep comments that are ignored (that pass theopts.ignore
check)locations
{Boolean}: iftrue
result will includeacorn/esprima
format comment location objectecmaVersion
{Number}: defaults to6
, 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
.line
Remove only line comments.
Params
<input>
{String}: string from which to get comments[opts]
{Object}: optional options, passed toacorn-extract-comments
andacorn
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
.block
Remove only block comments.
Params
<input>
{String}: string from which to get comments[opts]
{Object}: optional options, passed toacorn-extract-comments
andacorn
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.