diff options
| author | ruki <waruqi@gmail.com> | 2018-11-08 00:38:48 +0800 |
|---|---|---|
| committer | ruki <waruqi@gmail.com> | 2018-11-07 21:53:09 +0800 |
| commit | 26105034da4fcce7ac883c899d781f016559310d (patch) | |
| tree | c459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/strip-comments/index.js | |
| parent | 2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff) | |
| download | xmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip | |
switch to vuepress
Diffstat (limited to 'node_modules/strip-comments/index.js')
| -rw-r--r-- | node_modules/strip-comments/index.js | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/node_modules/strip-comments/index.js b/node_modules/strip-comments/index.js new file mode 100644 index 00000000..517fb5ba --- /dev/null +++ b/node_modules/strip-comments/index.js @@ -0,0 +1,186 @@ +/*! + * strip-comments <https://github.com/jonschlinkert/strip-comments> + * + * Copyright (c) 2014-2016, 2018, Jon Schlinkert. + * Released under the MIT License. + */ + +'use strict'; + +const assign = Object.assign; +const extract = require('babel-extract-comments'); + +/** + * Strip all code comments from the given `input`, including protected + * comments that start with `!`, unless disabled by setting `options.keepProtected` + * to true. + * + * ```js + * const str = strip('const foo = "bar";// this is a comment\n /* me too *\/'); + * console.log(str); + * // => 'const foo = "bar";' + * ``` + * @name strip + * @param {String} `input` string from which to strip comments + * @param {Object} `options` optional options, passed to [extract-comments][extract-comments] + * @option {Boolean} [options] `line` if `false` strip only block comments, default `true` + * @option {Boolean} [options] `block` if `false` strip only line comments, default `true` + * @option {Boolean} [options] `keepProtected` Keep ignored comments (e.g. `/*!` and `//!`) + * @option {Boolean} [options] `preserveNewlines` Preserve newlines after comments are stripped + * @return {String} modified input + * @api public + */ + +function strip(input, options) { + return stripComments(input, assign({ block: true, line: true }, options)); +} + +/** + * Strip only block comments. + * + * ```js + * const strip = require('..'); + * const str = strip.block('const foo = "bar";// this is a comment\n /* me too *\/'); + * console.log(str); + * // => 'const foo = "bar";// this is a comment' + * ``` + * @name .block + * @param {String} `input` string from which to strip comments + * @param {Object} `options` pass `opts.keepProtected: true` to keep ignored comments (e.g. `/*!`) + * @return {String} modified string + * @api public + */ + +strip.block = function(input, options) { + return stripComments(input, assign({ block: true }, options)); +}; + +/** + * Strip only line comments. + * + * ```js + * const str = strip.line('const foo = "bar";// this is a comment\n /* me too *\/'); + * console.log(str); + * // => 'const foo = "bar";\n/* me too *\/' + * ``` + * @name .line + * @param {String} `input` string from which to strip comments + * @param {Object} `options` pass `opts.keepProtected: true` to keep ignored comments (e.g. `//!`) + * @return {String} modified string + * @api public + */ + +strip.line = function(input, options) { + return stripComments(input, assign({ line: true }, options)); +}; + +/** + * Strip the first comment from the given `input`. Or, if `opts.keepProtected` is true, + * the first non-protected comment will be stripped. + * + * ```js + * const output = strip.first(input, { keepProtected: true }); + * console.log(output); + * // => '//! first comment\nfoo; ' + * ``` + * @name .first + * @param {String} `input` + * @param {Object} `options` pass `opts.keepProtected: true` to keep comments with `!` + * @return {String} + * @api public + */ + +strip.first = function(input, options) { + const opts = assign({ block: true, line: true, first: true }, options); + return stripComments(input, opts); +}; + +/** + * Strip comments + */ + +function stripComments(input, options) { + if (typeof input !== 'string') { + throw new TypeError('expected a string'); + } + + // strip all by default, including `ingored` comments. + const defaults = { + // we shouldn't care about this here since our goal is to strip comments, + // not transpiling, and this has been a common cause of parsing issues + allowReturnOutsideFunction: true, + block: false, + line: false, + safe: false, + first: false, + plugins: [] + }; + + const opts = assign({}, defaults, options); + opts.plugins.push('objectRestSpread'); + + if (typeof opts.keepProtected !== 'boolean') { + opts.keepProtected = opts.safe; + } + + try { + const comments = extract(input, opts); + let pos = { start: 0, end: 0, removed: 0 }; + if (!comments) return input; + + for (const comment of comments) { + if (typeof opts.filter === 'function' && opts.filter(comment, opts) === false) { + continue; + } + + input = remove(input, comment, opts, pos); + + if (opts.first === true && !isProtected(comment, opts)) { + break; + } + } + } catch (err) { + if (options.silent !== true) { + throw err; + } + } + return input; +} + +/** + * Remove a single comment from the given string. + */ + +function remove(str, comment, options, pos) { + let nl = ''; + + if (isProtected(comment, options)) { + return str; + } + + if (options && options.preserveNewlines) { + nl = comment.value.replace(/[^\r\n]/g, ''); + } + + if (comment.type === 'CommentLine' && options.line === true) { + const before = str.slice(0, comment.start - pos.removed); + const after = str.slice(comment.end - pos.removed); + pos.removed += comment.end - comment.start - nl.length; + return before + nl + after; + } + + if (comment.type === 'CommentBlock' && options.block === true) { + const before = str.slice(0, comment.start - pos.removed); + const after = str.slice(comment.end - pos.removed); + pos.removed += comment.end - comment.start - nl.length; + return before + nl + after; + } + + return str; +} + +function isProtected(comment, options) { + return options && options.keepProtected === true && /^\*?!/.test(comment.value); +} + +module.exports = strip; |
