forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsourcemapPublic.ts
More file actions
146 lines (132 loc) · 6.07 KB
/
sourcemapPublic.ts
File metadata and controls
146 lines (132 loc) · 6.07 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
namespace ts {
export interface DecodedSourceMapAnnotation {
generatedLine: number;
generatedCharacter: number;
annotations: SourceMapAnnotation[];
}
/**
* Decodes the custom `x_ms_ts_annotations` field of a SourceMap.
*/
export function decodeSourceMapAnnotations(annotations: string, names: readonly string[]): Iterator<DecodedSourceMapAnnotation> {
const reader = createCharCodeReader(annotations);
let done = false;
let generatedLine = 0;
let generatedCharacter = 0;
let nameIndex = 0;
return {
next() {
debugger;
while (!done && reader.pos < reader.length) {
const ch = reader.peek();
if (ch === CharacterCodes.semicolon) {
generatedLine++;
generatedCharacter = 0;
reader.read();
continue;
}
if (ch === CharacterCodes.comma) {
reader.read();
continue;
}
generatedCharacter += base64VLQFormatDecode(reader, noop);
if (generatedCharacter < 0) break;
if (isSegmentEnd()) continue;
nameIndex += base64VLQFormatDecode(reader, noop);
const name = elementAt(names, nameIndex);
if (name) {
const annotations = decodeAnnotations(createCharCodeReader(name));
if (annotations) {
return { value: { generatedLine, generatedCharacter, annotations }, done };
}
}
}
done = true;
return { value: undefined!, done: true } as { value: never, done: true };
}
};
function isSegmentEnd() {
return (reader.pos === reader.length ||
reader.peek() === CharacterCodes.comma ||
reader.peek() === CharacterCodes.semicolon);
}
function decodeAnnotations(reader: CharCodeReader): SourceMapAnnotation[] | undefined {
let nameIndex = 0;
const annotations = decodeAnnotationWorker();
if (Array.isArray(annotations) && annotations.every(isSourceMapAnnotation)) {
return annotations;
}
function decodeAnnotationWorker(): unknown {
if (reader.pos < reader.length) {
let ch = reader.peek();
if (ch === CharacterCodes.hash) {
reader.read();
return base64VLQFormatDecode(reader, noop);
}
else if (ch === CharacterCodes.at) {
reader.read();
nameIndex += base64VLQFormatDecode(reader, noop);
return elementAt(names, nameIndex);
}
else if (ch === CharacterCodes.t || ch === CharacterCodes.f) {
reader.read();
return ch === CharacterCodes.t;
}
else if (ch === CharacterCodes.exclamation) {
reader.read();
return null; // eslint-disable-line no-null/no-null
}
else if (ch === CharacterCodes.openBracket) {
const array: unknown[] = [];
reader.read();
while (reader.pos < reader.length) {
ch = reader.peek();
if (ch === CharacterCodes.closeBracket) {
reader.read();
return array;
}
if (array.length > 0) {
if (ch !== CharacterCodes.comma) {
return;
}
reader.read();
}
array.push(decodeAnnotationWorker());
}
}
else if (ch === CharacterCodes.openBrace) {
const obj: any = {};
let hasReadProperty = false;
reader.read();
while (reader.pos < reader.length) {
ch = reader.peek();
if (ch === CharacterCodes.closeBrace) {
reader.read();
return obj;
}
if (hasReadProperty) {
if (ch !== CharacterCodes.comma) {
return;
}
reader.read();
if (reader.pos >= reader.length) return;
}
const key = decodeAnnotationWorker();
if (typeof key !== "string") return;
if (reader.pos >= reader.length) return;
ch = reader.peek();
if (ch !== CharacterCodes.colon) return;
reader.read();
if (reader.pos >= reader.length) return;
const value = decodeAnnotationWorker();
obj[key] = value;
hasReadProperty = true;
}
}
}
}
}
}
function isSourceMapAnnotation(value: unknown): value is SourceMapAnnotation {
return typeof value === "object" && !!value && typeof (value as SourceMapAnnotation).name === "string";
}
}