comments-processor.js
3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var EscapeStore = require('./escape-store');
var QuoteScanner = require('../utils/quote-scanner');
var SPECIAL_COMMENT_PREFIX = '/*!';
var COMMENT_PREFIX = '/*';
var COMMENT_SUFFIX = '*/';
var lineBreak = require('os').EOL;
function CommentsProcessor(context, keepSpecialComments, keepBreaks, saveWaypoints) {
this.comments = new EscapeStore('COMMENT');
this.specialComments = new EscapeStore('COMMENT_SPECIAL');
this.context = context;
this.restored = 0;
this.keepAll = keepSpecialComments == '*';
this.keepOne = keepSpecialComments == '1' || keepSpecialComments === 1;
this.keepBreaks = keepBreaks;
this.saveWaypoints = saveWaypoints;
}
function quoteScannerFor(data) {
var quoteMap = [];
new QuoteScanner(data).each(function (quotedString, _, startsAt) {
quoteMap.push([startsAt, startsAt + quotedString.length]);
});
return function (position) {
for (var i = 0, l = quoteMap.length; i < l; i++) {
if (quoteMap[i][0] < position && quoteMap[i][1] > position)
return true;
}
return false;
};
}
CommentsProcessor.prototype.escape = function (data) {
var tempData = [];
var nextStart = 0;
var nextEnd = 0;
var cursor = 0;
var indent = 0;
var breaksCount;
var lastBreakAt;
var newIndent;
var isQuotedAt = quoteScannerFor(data);
var saveWaypoints = this.saveWaypoints;
for (; nextEnd < data.length;) {
nextStart = data.indexOf(COMMENT_PREFIX, cursor);
if (nextStart == -1)
break;
if (isQuotedAt(nextStart)) {
tempData.push(data.substring(cursor, nextStart + COMMENT_PREFIX.length));
cursor = nextStart + COMMENT_PREFIX.length;
continue;
}
nextEnd = data.indexOf(COMMENT_SUFFIX, nextStart + COMMENT_PREFIX.length);
if (nextEnd == -1) {
this.context.warnings.push('Broken comment: \'' + data.substring(nextStart) + '\'.');
nextEnd = data.length - 2;
}
tempData.push(data.substring(cursor, nextStart));
var comment = data.substring(nextStart, nextEnd + COMMENT_SUFFIX.length);
var isSpecialComment = comment.indexOf(SPECIAL_COMMENT_PREFIX) === 0;
if (saveWaypoints) {
breaksCount = comment.split(lineBreak).length - 1;
lastBreakAt = comment.lastIndexOf(lineBreak);
newIndent = lastBreakAt > 0 ?
comment.substring(lastBreakAt + lineBreak.length).length :
indent + comment.length;
}
if (saveWaypoints || isSpecialComment) {
var metadata = saveWaypoints ? [breaksCount, newIndent] : null;
var placeholder = isSpecialComment ?
this.specialComments.store(comment, metadata) :
this.comments.store(comment, metadata);
tempData.push(placeholder);
}
if (saveWaypoints)
indent = newIndent + 1;
cursor = nextEnd + COMMENT_SUFFIX.length;
}
return tempData.length > 0 ?
tempData.join('') + data.substring(cursor, data.length) :
data;
};
function restore(context, data, from, isSpecial) {
var tempData = [];
var cursor = 0;
for (; cursor < data.length;) {
var nextMatch = from.nextMatch(data, cursor);
if (nextMatch.start < 0)
break;
tempData.push(data.substring(cursor, nextMatch.start));
var comment = from.restore(nextMatch.match);
if (isSpecial && (context.keepAll || (context.keepOne && context.restored === 0))) {
context.restored++;
tempData.push(comment);
cursor = nextMatch.end;
} else {
cursor = nextMatch.end + (context.keepBreaks && data.substring(nextMatch.end, nextMatch.end + lineBreak.length) == lineBreak ? lineBreak.length : 0);
}
}
return tempData.length > 0 ?
tempData.join('') + data.substring(cursor, data.length) :
data;
}
CommentsProcessor.prototype.restore = function (data) {
data = restore(this, data, this.comments, false);
data = restore(this, data, this.specialComments, true);
return data;
};
module.exports = CommentsProcessor;