-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
335 lines (311 loc) · 11.7 KB
/
Copy pathindex.js
File metadata and controls
335 lines (311 loc) · 11.7 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import sourceMap from 'source-map';
import * as acorn from 'acorn';
/**
* @type {{
* line: number,
* column: number
* }}
*/
let Location;
/**
* @type {{
* start: !Location,
* end: !Location,
* offset: undefined|(!Location)
* }}
*/
let LocationSpan;
/**
* @type {{
* file: (string|undefined),
* sourceRoot: (string|undefined),
* sources: !Array<string>,
* sourcesContent: (!Array<string>|undefined),
* names: !Array<string>,
* mappings: string
* }}
*/
let SourceMap;
/**
* @param {!string} input
* @return {!Array<number>} Array of indexes indicating the index in the original string of start each new line
*/
function getLineIndexes(input) {
const lineIndexes = [0];
let currentIndex = 0;
input.split('\n').forEach((line, index) => {
lineIndexes[index] = currentIndex;
currentIndex += line.length + 1;
});
return lineIndexes;
}
/**
* @param {number} index
* @param {!Array<number>} lineIndexes
* @return {number}
*/
function getLineNumForIndex(index, lineIndexes) {
let lineNum = 0;
for (let i = 0; i < lineIndexes.length; i++) {
if (lineIndexes[i] > index) {
break;
}
lineNum = i;
}
return lineNum + 1;
}
/**
* @param {!LocationSpan} a
* @param {!LocationSpan} b
* @return {number}
*/
function compareLocations(a, b) {
if (a.start.line !== b.start.line) {
return a.start.line - b.start.line;
}
if (a.start.column !== b.start.column) {
return a.start.column - b.start.column;
}
if (a.end.line !== b.end.line) {
return a.end.line - b.end.line;
}
if (a.end.column !== b.end.column) {
return a.end.column - b.end.column;
}
return a.id - b.id;
}
export default class StringReplaceSourceMap {
/**
* @param {!string} originalString
* @param {string|SourceMap|null} originalSourceMap
*/
constructor(originalString, originalSourceMap) {
this.string = originalString;
/**
* Array of indexes indicating the index start of each new line
* @type {!Array<number>}
*/
this.lineIndexes = getLineIndexes(originalString);
if (!originalSourceMap) {
this.sourceMap = null;
} else if (typeof originalSourceMap === 'string') {
this.sourceMap = /** @type {!SourceMap} */ (JSON.parse(originalSourceMap));
} else {
this.sourceMap = originalSourceMap;
}
this.locationUpdates = [];
this.nextId = 0;
}
/** @param {string} newString */
append(newString) {
this.replace(this.string.length, this.string.length, newString);
}
/** @param {string} newString */
prepend(newString) {
this.replace(0, 0, newString);
}
/**
* @param {number} start
* @param {number} end
* @param {?string=} newString
*/
replace(start, end, newString) {
const lineIndexesForNewString = getLineIndexes(newString);
const originalStringStartLine = getLineNumForIndex(start, this.lineIndexes);
const originalStringEndLine = getLineNumForIndex(end, this.lineIndexes);
const locationUpdates = {
start: {
index: start,
line: originalStringStartLine,
column: start - this.lineIndexes[originalStringStartLine - 1]
},
end: {
index: end,
line: originalStringEndLine,
column: end - this.lineIndexes[originalStringEndLine - 1]
},
newString,
newStringNumLines: lineIndexesForNewString.length,
preserveMapping: !(newString === null || newString === undefined || newString.length === 0),
id: this.nextId
};
this.nextId++;
const lineOffset = (lineIndexesForNewString.length - 1) - (originalStringEndLine - originalStringStartLine);
const originalStringLastLineLength = locationUpdates.end.column -
(locationUpdates.start.line === locationUpdates.end.line ? locationUpdates.start.column : 0);
const newStringLastLineLength = newString.length -
(lineIndexesForNewString.length > 1 ? lineIndexesForNewString[lineIndexesForNewString.length - 1] : 0);
locationUpdates.offsetInfo = {
column: locationUpdates.end.column,
line: lineOffset
};
if (locationUpdates.start.line === locationUpdates.end.line && lineIndexesForNewString.length === 1) {
locationUpdates.offsetInfo.resetColumn = false;
locationUpdates.offsetInfo.offset = newStringLastLineLength - originalStringLastLineLength;
} else if (locationUpdates.start.line !== locationUpdates.end.line && lineIndexesForNewString.length === 1) {
locationUpdates.offsetInfo.resetColumn = true;
locationUpdates.offsetInfo.offset = locationUpdates.start.column + newStringLastLineLength - locationUpdates.end.column;
} else if (locationUpdates.start.line === locationUpdates.end.line && lineIndexesForNewString.length > 1) {
locationUpdates.offsetInfo.resetColumn = true;
locationUpdates.offsetInfo.offset = newStringLastLineLength - locationUpdates.end.column;
} else {
locationUpdates.offsetInfo.resetColumn = false;
locationUpdates.offsetInfo.offset = newStringLastLineLength - originalStringLastLineLength;
}
this.locationUpdates.push(locationUpdates);
}
/** @return {!string} */
toString() {
const locationUpdates = this.locationUpdates.slice().sort(compareLocations);
let updatedString = this.string;
for (let i = locationUpdates.length - 1; i >= 0; i--) {
updatedString = updatedString.substr(0, locationUpdates[i].start.index) +
(locationUpdates[i].newString || '') +
updatedString.substr(locationUpdates[i].end.index);
}
return updatedString;
}
/** @return {!SourceMap} */
async generateSourceMap() {
if (!this.sourceMap) {
throw new Error('An input source map must be provided to generate an output source map.');
}
const sourceMapOptions = {};
if (this.sourceMap.file) {
sourceMapOptions.file = this.sourceMap.file;
}
if (this.sourceMap.sourceRoot) {
sourceMapOptions.sourceRoot = this.sourceMap.sourceRoot;
}
const sourceMapGenerator = new sourceMap.SourceMapGenerator(sourceMapOptions);
const locationUpdates = this.locationUpdates.slice().sort(compareLocations);
let locationUpdatesIndex = 0;
let lineOffset = 0;
let columnOffsets = {
line: 0,
offsets: []
};
let currentLocationUpdate = locationUpdates[locationUpdatesIndex];
let sourcesReferenced = new Set();
await sourceMap.SourceMapConsumer.with(this.sourceMap, null, async (sourceMapConsumer) => {
let currentLine = 0;
sourceMapConsumer.eachMapping((mapping) => {
let mappingRecord = {
source: mapping.source === '' ? ' ' : mapping.source, // empty source names throw errors
generated: {
line: mapping.generatedLine,
column: mapping.generatedColumn
}
};
sourcesReferenced.add(mapping.source);
if (mapping.originalLine !== null && mapping.originalLine !== undefined) {
mappingRecord.original = {
line: mapping.originalLine,
column: mapping.originalColumn
};
} else {
mappingRecord.original = null;
}
if (mapping.name !== null && mapping.name !== undefined) {
mappingRecord.name = mapping.name;
}
// Advance the locationUpdate if necessary
if (currentLocationUpdate) {
// Find an exact match - may need to remove the mapping if the replacement string is empty
if (currentLocationUpdate.start.line === mappingRecord.generated.line &&
currentLocationUpdate.start.column === mappingRecord.generated.column &&
!currentLocationUpdate.preserveMapping) {
mappingRecord = null;
// Check if the next location is still ahead of the current mapping record.
} else if (currentLocationUpdate.start.line > mappingRecord.generated.line ||
(currentLocationUpdate.start.line === mappingRecord.generated.line &&
currentLocationUpdate.start.column >= mappingRecord.generated.column)) {
// do nothing
// Check if the current mapping record is inside the the next replacement. If so, remove the mapping
} else if (
(currentLocationUpdate.end.line > mappingRecord.generated.line ||
(currentLocationUpdate.end.line == mappingRecord.generated.line &&
currentLocationUpdate.end.column > mappingRecord.generated.column)) &&
(currentLocationUpdate.start.line < mappingRecord.generated.line ||
(currentLocationUpdate.start.line === mappingRecord.generated.line &&
currentLocationUpdate.start.column < mappingRecord.generated.column))) {
mappingRecord = null;
// Check if the current mapping record is past the replacement. If so, move to the next replacement update.
} else if (mappingRecord.generated.line > currentLocationUpdate.end.line ||
(currentLocationUpdate.end.line === mappingRecord.generated.line &&
currentLocationUpdate.end.column <= mappingRecord.generated.column)) {
lineOffset += currentLocationUpdate.offsetInfo.line;
if (columnOffsets.line === currentLocationUpdate.end.line) {
columnOffsets.offsets.push(currentLocationUpdate.offsetInfo);
} else {
columnOffsets.line = currentLocationUpdate.end.line;
columnOffsets.offsets = [currentLocationUpdate.offsetInfo];
}
locationUpdatesIndex++;
currentLocationUpdate = locationUpdates[locationUpdatesIndex];
}
}
if (mappingRecord) {
let columnOffset = 0;
if (mappingRecord.generated.line === columnOffsets.line) {
columnOffset = columnOffsets.offsets.reduce((calculatedOffset, offsetInfo) => {
if (offsetInfo.resetColumn) {
return offsetInfo.offset;
}
if (mappingRecord.generated.column >= offsetInfo.column) {
calculatedOffset += offsetInfo.offset;
}
return calculatedOffset;
}, 0);
}
if (mappingRecord.generated.line !== currentLine) {
currentLine = mappingRecord.generated.line;
}
mappingRecord.generated.line += lineOffset;
mappingRecord.generated.column += columnOffset;
sourceMapGenerator.addMapping(mappingRecord);
}
});
});
if (this.sourceMap.sourcesContent) {
this.sourceMap.sources.forEach((sourceFile, index) => {
if (sourceFile === ' ') {
sourceFile = '';
this.sourceMap.sources[index] = sourceFile;
}
if (sourcesReferenced.has(sourceFile) && this.sourceMap.sourcesContent[index]) {
sourceMapGenerator.setSourceContent(sourceFile, this.sourceMap.sourcesContent[index]);
}
});
}
return sourceMapGenerator.toJSON();
}
/**
* Helper method to generate an identity source map for a JS file
*
* @param {string} sourcePath of the file
* @param {string} jsSource
* @return {!Object}
*/
static generateJsIdentityMap(sourcePath, jsSource) {
const generator = new sourceMap.SourceMapGenerator({ file: sourcePath });
const tokenizer = acorn.tokenizer(jsSource, {
allowHashBang: true,
locations: true,
ecmaVersion: 'latest'
});
for (let token = tokenizer.getToken(); token.type.label !== 'eof'; token = tokenizer.getToken()) {
const mapping = {
original: token.loc.start,
generated: token.loc.start,
source: sourcePath,
};
if (token.type.label === 'name') {
mapping.name = token.value;
}
generator.addMapping(mapping);
}
generator.setSourceContent(sourcePath, jsSource);
return generator.toJSON();
}
}