1 |
3a515b92
|
cagy
|
/*
|
2 |
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
3 |
|
|
Author Tobias Koppers @sokra
|
4 |
|
|
*/
|
5 |
|
|
"use strict";
|
6 |
|
|
|
7 |
|
|
var Source = require("./Source");
|
8 |
|
|
var SourceNode = require("source-map").SourceNode;
|
9 |
|
|
|
10 |
|
|
class Replacement {
|
11 |
|
|
constructor(start, end, content, insertIndex, name) {
|
12 |
|
|
this.start = start;
|
13 |
|
|
this.end = end;
|
14 |
|
|
this.content = content;
|
15 |
|
|
this.insertIndex = insertIndex;
|
16 |
|
|
this.name = name;
|
17 |
|
|
}
|
18 |
|
|
}
|
19 |
|
|
|
20 |
|
|
class ReplaceSource extends Source {
|
21 |
|
|
constructor(source, name) {
|
22 |
|
|
super();
|
23 |
|
|
this._source = source;
|
24 |
|
|
this._name = name;
|
25 |
|
|
/** @type {Replacement[]} */
|
26 |
|
|
this.replacements = [];
|
27 |
|
|
}
|
28 |
|
|
|
29 |
|
|
replace(start, end, newValue, name) {
|
30 |
|
|
if(typeof newValue !== "string")
|
31 |
|
|
throw new Error("insertion must be a string, but is a " + typeof newValue);
|
32 |
|
|
this.replacements.push(new Replacement(start, end, newValue, this.replacements.length, name));
|
33 |
|
|
}
|
34 |
|
|
|
35 |
|
|
insert(pos, newValue, name) {
|
36 |
|
|
if(typeof newValue !== "string")
|
37 |
|
|
throw new Error("insertion must be a string, but is a " + typeof newValue + ": " + newValue);
|
38 |
|
|
this.replacements.push(new Replacement(pos, pos - 1, newValue, this.replacements.length, name));
|
39 |
|
|
}
|
40 |
|
|
|
41 |
|
|
source(options) {
|
42 |
|
|
return this._replaceString(this._source.source());
|
43 |
|
|
}
|
44 |
|
|
|
45 |
|
|
original() {
|
46 |
|
|
return this._source;
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
_sortReplacements() {
|
50 |
|
|
this.replacements.sort(function(a, b) {
|
51 |
|
|
var diff = b.end - a.end;
|
52 |
|
|
if(diff !== 0)
|
53 |
|
|
return diff;
|
54 |
|
|
diff = b.start - a.start;
|
55 |
|
|
if(diff !== 0)
|
56 |
|
|
return diff;
|
57 |
|
|
return b.insertIndex - a.insertIndex;
|
58 |
|
|
});
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
_replaceString(str) {
|
62 |
|
|
if(typeof str !== "string")
|
63 |
|
|
throw new Error("str must be a string, but is a " + typeof str + ": " + str);
|
64 |
|
|
this._sortReplacements();
|
65 |
|
|
var result = [str];
|
66 |
|
|
this.replacements.forEach(function(repl) {
|
67 |
|
|
var remSource = result.pop();
|
68 |
|
|
var splitted1 = this._splitString(remSource, Math.floor(repl.end + 1));
|
69 |
|
|
var splitted2 = this._splitString(splitted1[0], Math.floor(repl.start));
|
70 |
|
|
result.push(splitted1[1], repl.content, splitted2[0]);
|
71 |
|
|
}, this);
|
72 |
|
|
|
73 |
|
|
// write out result array in reverse order
|
74 |
|
|
let resultStr = "";
|
75 |
|
|
for(let i = result.length - 1; i >= 0; --i) {
|
76 |
|
|
resultStr += result[i];
|
77 |
|
|
}
|
78 |
|
|
return resultStr;
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
node(options) {
|
82 |
|
|
var node = this._source.node(options);
|
83 |
|
|
if(this.replacements.length === 0) {
|
84 |
|
|
return node;
|
85 |
|
|
}
|
86 |
|
|
this._sortReplacements();
|
87 |
|
|
var replace = new ReplacementEnumerator(this.replacements);
|
88 |
|
|
var output = [];
|
89 |
|
|
var position = 0;
|
90 |
|
|
var sources = Object.create(null);
|
91 |
|
|
var sourcesInLines = Object.create(null);
|
92 |
|
|
|
93 |
|
|
// We build a new list of SourceNodes in "output"
|
94 |
|
|
// from the original mapping data
|
95 |
|
|
|
96 |
|
|
var result = new SourceNode();
|
97 |
|
|
|
98 |
|
|
// We need to add source contents manually
|
99 |
|
|
// because "walk" will not handle it
|
100 |
|
|
node.walkSourceContents(function(sourceFile, sourceContent) {
|
101 |
|
|
result.setSourceContent(sourceFile, sourceContent);
|
102 |
|
|
sources["$" + sourceFile] = sourceContent;
|
103 |
|
|
});
|
104 |
|
|
|
105 |
|
|
var replaceInStringNode = this._replaceInStringNode.bind(this, output, replace, function getOriginalSource(mapping) {
|
106 |
|
|
var key = "$" + mapping.source;
|
107 |
|
|
var lines = sourcesInLines[key];
|
108 |
|
|
if(!lines) {
|
109 |
|
|
var source = sources[key];
|
110 |
|
|
if(!source) return null;
|
111 |
|
|
lines = source.split("\n").map(function(line) {
|
112 |
|
|
return line + "\n";
|
113 |
|
|
});
|
114 |
|
|
sourcesInLines[key] = lines;
|
115 |
|
|
}
|
116 |
|
|
// line is 1-based
|
117 |
|
|
if(mapping.line > lines.length) return null;
|
118 |
|
|
var line = lines[mapping.line - 1];
|
119 |
|
|
return line.substr(mapping.column);
|
120 |
|
|
});
|
121 |
|
|
|
122 |
|
|
node.walk(function(chunk, mapping) {
|
123 |
|
|
position = replaceInStringNode(chunk, position, mapping);
|
124 |
|
|
});
|
125 |
|
|
|
126 |
|
|
// If any replacements occur after the end of the original file, then we append them
|
127 |
|
|
// directly to the end of the output
|
128 |
|
|
var remaining = replace.footer();
|
129 |
|
|
if(remaining) {
|
130 |
|
|
output.push(remaining);
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
result.add(output);
|
134 |
|
|
|
135 |
|
|
return result;
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
listMap(options) {
|
139 |
|
|
this._sortReplacements();
|
140 |
|
|
var map = this._source.listMap(options);
|
141 |
|
|
var currentIndex = 0;
|
142 |
|
|
var replacements = this.replacements;
|
143 |
|
|
var idxReplacement = replacements.length - 1;
|
144 |
|
|
var removeChars = 0;
|
145 |
|
|
map = map.mapGeneratedCode(function(str) {
|
146 |
|
|
var newCurrentIndex = currentIndex + str.length;
|
147 |
|
|
if(removeChars > str.length) {
|
148 |
|
|
removeChars -= str.length;
|
149 |
|
|
str = "";
|
150 |
|
|
} else {
|
151 |
|
|
if(removeChars > 0) {
|
152 |
|
|
str = str.substr(removeChars);
|
153 |
|
|
currentIndex += removeChars;
|
154 |
|
|
removeChars = 0;
|
155 |
|
|
}
|
156 |
|
|
var finalStr = "";
|
157 |
|
|
while(idxReplacement >= 0 && replacements[idxReplacement].start < newCurrentIndex) {
|
158 |
|
|
var repl = replacements[idxReplacement];
|
159 |
|
|
var start = Math.floor(repl.start);
|
160 |
|
|
var end = Math.floor(repl.end + 1);
|
161 |
|
|
var before = str.substr(0, Math.max(0, start - currentIndex));
|
162 |
|
|
if(end <= newCurrentIndex) {
|
163 |
|
|
var after = str.substr(Math.max(0, end - currentIndex));
|
164 |
|
|
finalStr += before + repl.content;
|
165 |
|
|
str = after;
|
166 |
|
|
currentIndex = Math.max(currentIndex, end);
|
167 |
|
|
} else {
|
168 |
|
|
finalStr += before + repl.content;
|
169 |
|
|
str = "";
|
170 |
|
|
removeChars = end - newCurrentIndex;
|
171 |
|
|
}
|
172 |
|
|
idxReplacement--;
|
173 |
|
|
}
|
174 |
|
|
str = finalStr + str;
|
175 |
|
|
}
|
176 |
|
|
currentIndex = newCurrentIndex;
|
177 |
|
|
return str;
|
178 |
|
|
});
|
179 |
|
|
var extraCode = "";
|
180 |
|
|
while(idxReplacement >= 0) {
|
181 |
|
|
extraCode += replacements[idxReplacement].content;
|
182 |
|
|
idxReplacement--;
|
183 |
|
|
}
|
184 |
|
|
if(extraCode) {
|
185 |
|
|
map.add(extraCode);
|
186 |
|
|
}
|
187 |
|
|
return map;
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
_splitString(str, position) {
|
191 |
|
|
return position <= 0 ? ["", str] : [str.substr(0, position), str.substr(position)];
|
192 |
|
|
}
|
193 |
|
|
|
194 |
|
|
_replaceInStringNode(output, replace, getOriginalSource, node, position, mapping) {
|
195 |
|
|
var original = undefined;
|
196 |
|
|
|
197 |
|
|
do {
|
198 |
|
|
var splitPosition = replace.position - position;
|
199 |
|
|
// If multiple replaces occur in the same location then the splitPosition may be
|
200 |
|
|
// before the current position for the subsequent splits. Ensure it is >= 0
|
201 |
|
|
if(splitPosition < 0) {
|
202 |
|
|
splitPosition = 0;
|
203 |
|
|
}
|
204 |
|
|
if(splitPosition >= node.length || replace.done) {
|
205 |
|
|
if(replace.emit) {
|
206 |
|
|
var nodeEnd = new SourceNode(
|
207 |
|
|
mapping.line,
|
208 |
|
|
mapping.column,
|
209 |
|
|
mapping.source,
|
210 |
|
|
node,
|
211 |
|
|
mapping.name
|
212 |
|
|
);
|
213 |
|
|
output.push(nodeEnd);
|
214 |
|
|
}
|
215 |
|
|
return position + node.length;
|
216 |
|
|
}
|
217 |
|
|
|
218 |
|
|
var originalColumn = mapping.column;
|
219 |
|
|
|
220 |
|
|
// Try to figure out if generated code matches original code of this segement
|
221 |
|
|
// If this is the case we assume that it's allowed to move mapping.column
|
222 |
|
|
// Because getOriginalSource can be expensive we only do it when neccessary
|
223 |
|
|
|
224 |
|
|
var nodePart;
|
225 |
|
|
if(splitPosition > 0) {
|
226 |
|
|
nodePart = node.slice(0, splitPosition);
|
227 |
|
|
if(original === undefined) {
|
228 |
|
|
original = getOriginalSource(mapping);
|
229 |
|
|
}
|
230 |
|
|
if(original && original.length >= splitPosition && original.startsWith(nodePart)) {
|
231 |
|
|
mapping.column += splitPosition;
|
232 |
|
|
original = original.substr(splitPosition);
|
233 |
|
|
}
|
234 |
|
|
}
|
235 |
|
|
|
236 |
|
|
var emit = replace.next();
|
237 |
|
|
if(!emit) {
|
238 |
|
|
// Stop emitting when we have found the beginning of the string to replace.
|
239 |
|
|
// Emit the part of the string before splitPosition
|
240 |
|
|
if(splitPosition > 0) {
|
241 |
|
|
var nodeStart = new SourceNode(
|
242 |
|
|
mapping.line,
|
243 |
|
|
originalColumn,
|
244 |
|
|
mapping.source,
|
245 |
|
|
nodePart,
|
246 |
|
|
mapping.name
|
247 |
|
|
);
|
248 |
|
|
output.push(nodeStart);
|
249 |
|
|
}
|
250 |
|
|
|
251 |
|
|
// Emit the replacement value
|
252 |
|
|
if(replace.value) {
|
253 |
|
|
output.push(new SourceNode(
|
254 |
|
|
mapping.line,
|
255 |
|
|
mapping.column,
|
256 |
|
|
mapping.source,
|
257 |
|
|
replace.value,
|
258 |
|
|
mapping.name || replace.name
|
259 |
|
|
));
|
260 |
|
|
}
|
261 |
|
|
}
|
262 |
|
|
|
263 |
|
|
// Recurse with remainder of the string as there may be multiple replaces within a single node
|
264 |
|
|
node = node.substr(splitPosition);
|
265 |
|
|
position += splitPosition;
|
266 |
|
|
} while (true);
|
267 |
|
|
}
|
268 |
|
|
}
|
269 |
|
|
|
270 |
|
|
class ReplacementEnumerator {
|
271 |
|
|
/**
|
272 |
|
|
* @param {Replacement[]} replacements list of replacements
|
273 |
|
|
*/
|
274 |
|
|
constructor(replacements) {
|
275 |
|
|
this.replacements = replacements || [];
|
276 |
|
|
this.index = this.replacements.length;
|
277 |
|
|
this.done = false;
|
278 |
|
|
this.emit = false;
|
279 |
|
|
// Set initial start position
|
280 |
|
|
this.next();
|
281 |
|
|
}
|
282 |
|
|
|
283 |
|
|
next() {
|
284 |
|
|
if(this.done)
|
285 |
|
|
return true;
|
286 |
|
|
if(this.emit) {
|
287 |
|
|
// Start point found. stop emitting. set position to find end
|
288 |
|
|
var repl = this.replacements[this.index];
|
289 |
|
|
var end = Math.floor(repl.end + 1);
|
290 |
|
|
this.position = end;
|
291 |
|
|
this.value = repl.content;
|
292 |
|
|
this.name = repl.name;
|
293 |
|
|
} else {
|
294 |
|
|
// End point found. start emitting. set position to find next start
|
295 |
|
|
this.index--;
|
296 |
|
|
if(this.index < 0) {
|
297 |
|
|
this.done = true;
|
298 |
|
|
} else {
|
299 |
|
|
var nextRepl = this.replacements[this.index];
|
300 |
|
|
var start = Math.floor(nextRepl.start);
|
301 |
|
|
this.position = start;
|
302 |
|
|
}
|
303 |
|
|
}
|
304 |
|
|
if(this.position < 0)
|
305 |
|
|
this.position = 0;
|
306 |
|
|
this.emit = !this.emit;
|
307 |
|
|
return this.emit;
|
308 |
|
|
}
|
309 |
|
|
|
310 |
|
|
footer() {
|
311 |
|
|
if(!this.done && !this.emit)
|
312 |
|
|
this.next(); // If we finished _replaceInNode mid emit we advance to next entry
|
313 |
|
|
if(this.done) {
|
314 |
|
|
return [];
|
315 |
|
|
} else {
|
316 |
|
|
var resultStr = "";
|
317 |
|
|
for(var i = this.index; i >= 0; i--) {
|
318 |
|
|
var repl = this.replacements[i];
|
319 |
|
|
// this doesn't need to handle repl.name, because in SourceMaps generated code
|
320 |
|
|
// without pointer to original source can't have a name
|
321 |
|
|
resultStr += repl.content;
|
322 |
|
|
}
|
323 |
|
|
return resultStr;
|
324 |
|
|
}
|
325 |
|
|
}
|
326 |
|
|
}
|
327 |
|
|
|
328 |
|
|
require("./SourceAndMapMixin")(ReplaceSource.prototype);
|
329 |
|
|
|
330 |
|
|
module.exports = ReplaceSource;
|