UNPKG

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