| 1 | /*!
|
|---|
| 2 | * strip-comments <https://github.com/jonschlinkert/strip-comments>
|
|---|
| 3 | * Copyright (c) 2014-present, Jon Schlinkert.
|
|---|
| 4 | * Released under the MIT License.
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | 'use strict';
|
|---|
| 8 |
|
|---|
| 9 | const compile = require('./lib/compile');
|
|---|
| 10 | const parse = require('./lib/parse');
|
|---|
| 11 |
|
|---|
| 12 | /**
|
|---|
| 13 | * Strip all code comments from the given `input`, including protected
|
|---|
| 14 | * comments that start with `!`, unless disabled by setting `options.keepProtected`
|
|---|
| 15 | * to true.
|
|---|
| 16 | *
|
|---|
| 17 | * ```js
|
|---|
| 18 | * const str = strip('const foo = "bar";// this is a comment\n /* me too *\/');
|
|---|
| 19 | * console.log(str);
|
|---|
| 20 | * // => 'const foo = "bar";'
|
|---|
| 21 | * ```
|
|---|
| 22 | * @name strip
|
|---|
| 23 | * @param {String} `input` string from which to strip comments
|
|---|
| 24 | * @param {Object} `options` optional options, passed to [extract-comments][extract-comments]
|
|---|
| 25 | * @option {Boolean} [options] `line` if `false` strip only block comments, default `true`
|
|---|
| 26 | * @option {Boolean} [options] `block` if `false` strip only line comments, default `true`
|
|---|
| 27 | * @option {Boolean} [options] `keepProtected` Keep ignored comments (e.g. `/*!` and `//!`)
|
|---|
| 28 | * @option {Boolean} [options] `preserveNewlines` Preserve newlines after comments are stripped
|
|---|
| 29 | * @return {String} modified input
|
|---|
| 30 | * @api public
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | const strip = module.exports = (input, options) => {
|
|---|
| 34 | const opts = { ...options, block: true, line: true };
|
|---|
| 35 | return compile(parse(input, opts), opts);
|
|---|
| 36 | };
|
|---|
| 37 |
|
|---|
| 38 | /**
|
|---|
| 39 | * Strip only block comments.
|
|---|
| 40 | *
|
|---|
| 41 | * ```js
|
|---|
| 42 | * const strip = require('..');
|
|---|
| 43 | * const str = strip.block('const foo = "bar";// this is a comment\n /* me too *\/');
|
|---|
| 44 | * console.log(str);
|
|---|
| 45 | * // => 'const foo = "bar";// this is a comment'
|
|---|
| 46 | * ```
|
|---|
| 47 | * @name .block
|
|---|
| 48 | * @param {String} `input` string from which to strip comments
|
|---|
| 49 | * @param {Object} `options` pass `opts.keepProtected: true` to keep ignored comments (e.g. `/*!`)
|
|---|
| 50 | * @return {String} modified string
|
|---|
| 51 | * @api public
|
|---|
| 52 | */
|
|---|
| 53 |
|
|---|
| 54 | strip.block = (input, options) => {
|
|---|
| 55 | const opts = { ...options, block: true };
|
|---|
| 56 | return compile(parse(input, opts), opts);
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| 59 | /**
|
|---|
| 60 | * Strip only line comments.
|
|---|
| 61 | *
|
|---|
| 62 | * ```js
|
|---|
| 63 | * const str = strip.line('const foo = "bar";// this is a comment\n /* me too *\/');
|
|---|
| 64 | * console.log(str);
|
|---|
| 65 | * // => 'const foo = "bar";\n/* me too *\/'
|
|---|
| 66 | * ```
|
|---|
| 67 | * @name .line
|
|---|
| 68 | * @param {String} `input` string from which to strip comments
|
|---|
| 69 | * @param {Object} `options` pass `opts.keepProtected: true` to keep ignored comments (e.g. `//!`)
|
|---|
| 70 | * @return {String} modified string
|
|---|
| 71 | * @api public
|
|---|
| 72 | */
|
|---|
| 73 |
|
|---|
| 74 | strip.line = (input, options) => {
|
|---|
| 75 | const opts = { ...options, line: true };
|
|---|
| 76 | return compile(parse(input, opts), opts);
|
|---|
| 77 | };
|
|---|
| 78 |
|
|---|
| 79 | /**
|
|---|
| 80 | * Strip the first comment from the given `input`. Or, if `opts.keepProtected` is true,
|
|---|
| 81 | * the first non-protected comment will be stripped.
|
|---|
| 82 | *
|
|---|
| 83 | * ```js
|
|---|
| 84 | * const output = strip.first(input, { keepProtected: true });
|
|---|
| 85 | * console.log(output);
|
|---|
| 86 | * // => '//! first comment\nfoo; '
|
|---|
| 87 | * ```
|
|---|
| 88 | * @name .first
|
|---|
| 89 | * @param {String} `input`
|
|---|
| 90 | * @param {Object} `options` pass `opts.keepProtected: true` to keep comments with `!`
|
|---|
| 91 | * @return {String}
|
|---|
| 92 | * @api public
|
|---|
| 93 | */
|
|---|
| 94 |
|
|---|
| 95 | strip.first = (input, options) => {
|
|---|
| 96 | const opts = { ...options, block: true, line: true, first: true };
|
|---|
| 97 | return compile(parse(input, opts), opts);
|
|---|
| 98 | };
|
|---|
| 99 |
|
|---|
| 100 | /**
|
|---|
| 101 | * Parses a string and returns a basic CST (Concrete Syntax Tree).
|
|---|
| 102 | *
|
|---|
| 103 | * ```js
|
|---|
| 104 | * const strip = require('..');
|
|---|
| 105 | * const str = strip.block('const foo = "bar";// this is a comment\n /* me too *\/');
|
|---|
| 106 | * console.log(str);
|
|---|
| 107 | * // => 'const foo = "bar";// this is a comment'
|
|---|
| 108 | * ```
|
|---|
| 109 | * @name .block
|
|---|
| 110 | * @param {String} `input` string from which to strip comments
|
|---|
| 111 | * @param {Object} `options` pass `opts.keepProtected: true` to keep ignored comments (e.g. `/*!`)
|
|---|
| 112 | * @return {String} modified string
|
|---|
| 113 | * @api public
|
|---|
| 114 | */
|
|---|
| 115 |
|
|---|
| 116 | strip.parse = parse;
|
|---|