encodeMappings.js
3.34 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
import { encode } from 'vlq';
import getLocator from './getLocator.js';
export default function encodeMappings ( original, intro, chunk, hires, sourcemapLocations, sourceIndex, offsets, names ) {
let rawLines = [];
let generatedCodeLine = intro.split( '\n' ).length - 1;
let rawSegments = rawLines[ generatedCodeLine ] = [];
let generatedCodeColumn = 0;
const locate = getLocator( original );
function addEdit ( content, original, loc, nameIndex, i ) {
if ( i || content.length ) {
rawSegments.push({
generatedCodeLine,
generatedCodeColumn,
sourceCodeLine: loc.line,
sourceCodeColumn: loc.column,
sourceCodeName: nameIndex,
sourceIndex
});
}
let lines = content.split( '\n' );
let lastLine = lines.pop();
if ( lines.length ) {
generatedCodeLine += lines.length;
rawLines[ generatedCodeLine ] = rawSegments = [];
generatedCodeColumn = lastLine.length;
} else {
generatedCodeColumn += lastLine.length;
}
lines = original.split( '\n' );
lastLine = lines.pop();
if ( lines.length ) {
loc.line += lines.length;
loc.column = lastLine.length;
} else {
loc.column += lastLine.length;
}
}
function addUneditedChunk ( chunk, loc ) {
let originalCharIndex = chunk.start;
let first = true;
while ( originalCharIndex < chunk.end ) {
if ( hires || first || sourcemapLocations[ originalCharIndex ] ) {
rawSegments.push({
generatedCodeLine,
generatedCodeColumn,
sourceCodeLine: loc.line,
sourceCodeColumn: loc.column,
sourceCodeName: -1,
sourceIndex
});
}
if ( original[ originalCharIndex ] === '\n' ) {
loc.line += 1;
loc.column = 0;
generatedCodeLine += 1;
rawLines[ generatedCodeLine ] = rawSegments = [];
generatedCodeColumn = 0;
} else {
loc.column += 1;
generatedCodeColumn += 1;
}
originalCharIndex += 1;
first = false;
}
}
while ( chunk ) {
let loc = locate( chunk.start );
if ( chunk.intro.length ) {
addEdit( chunk.intro, '', loc, -1, !!chunk.previous );
}
if ( chunk.edited ) {
addEdit( chunk.content, chunk.original, loc, chunk.storeName ? names.indexOf( chunk.original ) : -1, !!chunk.previous );
} else {
addUneditedChunk( chunk, loc );
}
if ( chunk.outro.length ) {
addEdit( chunk.outro, '', loc, -1, !!chunk.previous );
}
const nextChunk = chunk.next;
chunk = nextChunk;
}
offsets.sourceIndex = offsets.sourceIndex || 0;
offsets.sourceCodeLine = offsets.sourceCodeLine || 0;
offsets.sourceCodeColumn = offsets.sourceCodeColumn || 0;
offsets.sourceCodeName = offsets.sourceCodeName || 0;
const encoded = rawLines.map( segments => {
let generatedCodeColumn = 0;
return segments.map( segment => {
let arr = [
segment.generatedCodeColumn - generatedCodeColumn,
segment.sourceIndex - offsets.sourceIndex,
segment.sourceCodeLine - offsets.sourceCodeLine,
segment.sourceCodeColumn - offsets.sourceCodeColumn
];
generatedCodeColumn = segment.generatedCodeColumn;
offsets.sourceIndex = segment.sourceIndex;
offsets.sourceCodeLine = segment.sourceCodeLine;
offsets.sourceCodeColumn = segment.sourceCodeColumn;
if ( ~segment.sourceCodeName ) {
arr.push( segment.sourceCodeName - offsets.sourceCodeName );
offsets.sourceCodeName = segment.sourceCodeName;
}
return encode( arr );
}).join( ',' );
}).join( ';' );
return encoded;
}