-
-
Notifications
You must be signed in to change notification settings - Fork 364
/
index.ts
189 lines (158 loc) · 4.63 KB
/
index.ts
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
import { SegmentWithData, SegmentWithoutData } from 'muggle-string';
export * from 'muggle-string';
export interface Mapping<T = any> {
source?: string;
sourceRange: [number, number];
generatedRange: [number, number];
data: T;
};
export class SourceMapBase<Data = undefined> {
private _memo: {
sourceRange: {
offset: number;
mappings: Set<Mapping<Data>>;
}[];
generatedRange: {
offset: number;
mappings: Set<Mapping<Data>>;
}[];
} | undefined;
private get memo() {
if (!this._memo) {
const self = this;
this._memo = {
sourceRange: createMemo('sourceRange'),
generatedRange: createMemo('generatedRange'),
};
function createMemo(key: 'sourceRange' | 'generatedRange') {
const offsets = new Set<number>();
for (const mapping of self.mappings) {
offsets.add(mapping[key][0]);
offsets.add(mapping[key][1]);
}
const arr: {
offset: number,
mappings: Set<Mapping<Data>>,
}[] = [...offsets].sort((a, b) => a - b).map(offset => ({ offset, mappings: new Set() }));
for (const mapping of self.mappings) {
const startIndex = binarySearch(mapping[key][0])!;
const endIndex = binarySearch(mapping[key][1])!;
for (let i = startIndex; i <= endIndex; i++) {
arr[i].mappings.add(mapping);
}
}
return arr;
function binarySearch(start: number) {
let low = 0;
let high = arr.length - 1;
while (low <= high) {
const mid = Math.floor((low + high) / 2);
const midValue = arr[mid];
if (midValue.offset < start) {
low = mid + 1;
}
else if (midValue.offset > start) {
high = mid - 1;
}
else {
return mid;
}
}
}
}
}
return this._memo;
}
constructor(public readonly mappings: Mapping<Data>[]) {
}
public toSourceOffset(start: number, baseOnRight: boolean = false) {
for (const mapped of this.matcing(start, 'generatedRange', 'sourceRange', baseOnRight)) {
return mapped;
}
}
public toGeneratedOffset(start: number, baseOnRight: boolean = false) {
for (const mapped of this.matcing(start, 'sourceRange', 'generatedRange', baseOnRight)) {
return mapped;
}
}
public toSourceOffsets(start: number, baseOnRight: boolean = false) {
return this.matcing(start, 'generatedRange', 'sourceRange', baseOnRight);
}
public toGeneratedOffsets(start: number, baseOnRight: boolean = false) {
return this.matcing(start, 'sourceRange', 'generatedRange', baseOnRight);
}
public * matcing(startOffset: number, from: 'sourceRange' | 'generatedRange', to: 'sourceRange' | 'generatedRange', baseOnRight: boolean) {
const memo = this.memo[from];
if (memo.length === 0)
return;
const {
low: start,
high: end,
} = this.binarySearchMemo(memo, startOffset);
const skip = new Set<Mapping<Data>>();
for (let i = start; i <= end; i++) {
for (const mapping of memo[i].mappings) {
if (skip.has(mapping)) {
continue;
}
skip.add(mapping);
const mapped = this.matchOffset(startOffset, mapping[from], mapping[to], baseOnRight);
if (mapped !== undefined) {
yield [mapped, mapping] as const;
}
}
}
}
public matchOffset(start: number, mappedFromRange: [number, number], mappedToRange: [number, number], baseOnRight: boolean): number | undefined {
if (start >= mappedFromRange[0] && start <= mappedFromRange[1]) {
let offset = mappedToRange[0] + start - mappedFromRange[0];
if (baseOnRight) {
offset += (mappedToRange[1] - mappedToRange[0]) - (mappedFromRange[1] - mappedFromRange[0]);
}
return offset;
}
}
private binarySearchMemo(array: typeof this.memo['sourceRange'], start: number) {
let low = 0;
let high = array.length - 1;
while (low <= high) {
const mid = Math.floor((low + high) / 2);
const midValue = array[mid];
if (midValue.offset < start) {
low = mid + 1;
}
else if (midValue.offset > start) {
high = mid - 1;
}
else {
low = mid;
high = mid;
break;
}
}
return {
low: Math.max(Math.min(low, high, array.length - 1), 0),
high: Math.min(Math.max(low, high, 0), array.length - 1),
};
}
}
export function buildMappings<T>(chunks: SegmentWithoutData[] | SegmentWithData<T>[]) {
let length = 0;
const mappings: Mapping<T>[] = [];
for (const segment of chunks) {
if (typeof segment === 'string') {
length += segment.length;
}
else {
mappings.push({
generatedRange: [length, length + segment[0].length],
source: segment[1],
sourceRange: typeof segment[2] === 'number' ? [segment[2], segment[2] + segment[0].length] : segment[2],
// @ts-ignore
data: segment[3],
});
length += segment[0].length;
}
}
return mappings;
}