UNPKG

2.4 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
12};
13Object.defineProperty(exports, "__esModule", { value: true });
14var babylon = require("babylon");
15/*!
16 * strip-comments <https://github.com/jonschlinkert/strip-comments>
17 *
18 * Copyright (c) 2014-2016, 2018, Jon Schlinkert.
19 * Released under the MIT License.
20 *
21 */
22var removeComment = function (str, comment, pos) {
23 var nl = '';
24 var isCommentLine = function (c) { return c.type === 'CommentLine'; };
25 if (isCommentLine(comment)) {
26 var before = str.slice(0, comment.start - pos.removed);
27 var after = str.slice(comment.end - pos.removed);
28 pos.removed += comment.end - comment.start - nl.length;
29 return before + nl + after;
30 }
31 var isCommentBlock = function (c) { return c.type === 'CommentBlock'; };
32 if (isCommentBlock(comment)) {
33 var before = str.slice(0, comment.start - pos.removed);
34 var after = str.slice(comment.end - pos.removed);
35 pos.removed += comment.end - comment.start - nl.length;
36 return before + nl + after;
37 }
38 return str;
39};
40var stripComments = function (input, options) {
41 if (typeof input !== 'string') {
42 throw new TypeError('expected a string');
43 }
44 var defaults = {
45 // we shouldn't care about this here since our goal is to strip comments,
46 // not transpiling, and this has been a common cause of parsing issues
47 allowReturnOutsideFunction: true,
48 // casting because @types/babylon hasn't updated to babylon@^7
49 plugins: ['typescript', 'jsx']
50 };
51 var opts = __assign({}, defaults, options);
52 var res = babylon.parse(input, opts);
53 var comments = res.comments;
54 var pos = { start: 0, end: 0, removed: 0 };
55 if (!comments) {
56 return input;
57 }
58 for (var _i = 0, comments_1 = comments; _i < comments_1.length; _i++) {
59 var comment = comments_1[_i];
60 input = removeComment(input, comment, pos);
61 }
62 return input;
63};
64exports.default = stripComments;
65//# sourceMappingURL=strip-comments.js.map
\No newline at end of file