-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
buffer.ts
383 lines (329 loc) 路 11.5 KB
/
buffer.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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import type SourceMap from "./source-map";
import type * as t from "@babel/types";
const SPACES_RE = /^[ \t]+$/;
/**
* The Buffer class exists to manage the queue of tokens being pushed onto the output string
* in such a way that the final string buffer is treated as write-only until the final .get()
* call. This allows V8 to optimize the output efficiently by not requiring it to store the
* string in contiguous memory.
*/
export default class Buffer {
constructor(map?: SourceMap | null) {
this._map = map;
}
_map: SourceMap = null;
_buf: Array<any> = [];
_last: string = "";
_queue: Array<
[
str: string,
line: number,
column: number,
identifierName: string | null,
filename: string | null | undefined,
force: boolean | undefined,
]
> = [];
_position: any = {
line: 1,
column: 0,
};
_sourcePosition: any = {
identifierName: null,
line: null,
column: null,
filename: null,
};
_disallowedPop: any | null = null;
/**
* Get the final string output from the buffer, along with the sourcemap if one exists.
*/
get(): any {
this._flush();
const map = this._map;
const result = {
// Whatever trim is used here should not execute a regex against the
// source string since it may be arbitrarily large after all transformations
code: this._buf.join("").trimRight(),
map: null,
rawMappings: map?.getRawMappings(),
};
if (map) {
// The `.map` property is lazy to allow callers to use the raw mappings
// without any overhead
Object.defineProperty(result, "map", {
configurable: true,
enumerable: true,
get() {
return (this.map = map.get());
},
set(value) {
Object.defineProperty(this, "map", { value, writable: true });
},
});
}
return result;
}
/**
* Add a string to the buffer that cannot be reverted.
*/
append(str: string): void {
this._flush();
const { line, column, filename, identifierName, force } =
this._sourcePosition;
this._append(str, line, column, identifierName, filename, force);
}
/**
* Add a string to the buffer than can be reverted.
*/
queue(str: string): void {
// Drop trailing spaces when a newline is inserted.
if (str === "\n") {
while (this._queue.length > 0 && SPACES_RE.test(this._queue[0][0])) {
this._queue.shift();
}
}
const { line, column, filename, identifierName, force } =
this._sourcePosition;
this._queue.unshift([str, line, column, identifierName, filename, force]);
}
_flush(): void {
let item: [
string,
number,
number,
string | null | undefined,
string | null | undefined,
boolean | undefined,
];
while ((item = this._queue.pop())) {
this._append(...item);
}
}
_append(
str: string,
line: number,
column: number,
identifierName?: string | null,
filename?: string | null,
force?: boolean,
): void {
this._buf.push(str);
this._last = str[str.length - 1];
// Search for newline chars. We search only for `\n`, since both `\r` and
// `\r\n` are normalized to `\n` during parse. We exclude `\u2028` and
// `\u2029` for performance reasons, they're so uncommon that it's probably
// ok. It's also unclear how other sourcemap utilities handle them...
let i = str.indexOf("\n");
let last = 0;
// If the string starts with a newline char, then adding a mark is redundant.
// This catches both "no newlines" and "newline after several chars".
if (i !== 0) {
this._mark(line, column, identifierName, filename, force);
}
// Now, find each reamining newline char in the string.
while (i !== -1) {
this._position.line++;
this._position.column = 0;
last = i + 1;
// We mark the start of each line, which happens directly after this newline char
// unless this is the last char.
if (last < str.length) {
this._mark(++line, 0, identifierName, filename, force);
}
i = str.indexOf("\n", last);
}
this._position.column += str.length - last;
}
_mark(
line: number,
column: number,
identifierName?: string | null,
filename?: string | null,
force?: boolean,
): void {
this._map?.mark(
this._position.line,
this._position.column,
line,
column,
identifierName,
filename,
force,
);
}
removeTrailingNewline(): void {
if (this._queue.length > 0 && this._queue[0][0] === "\n") {
this._queue.shift();
}
}
removeLastSemicolon(): void {
if (this._queue.length > 0 && this._queue[0][0] === ";") {
this._queue.shift();
}
}
endsWith(suffix: string): boolean {
// Fast path to avoid iterating over this._queue.
if (suffix.length === 1) {
let last;
if (this._queue.length > 0) {
const str = this._queue[0][0];
last = str[str.length - 1];
} else {
last = this._last;
}
return last === suffix;
}
const end =
this._last + this._queue.reduce((acc, item) => item[0] + acc, "");
if (suffix.length <= end.length) {
return end.slice(-suffix.length) === suffix;
}
// We assume that everything being matched is at most a single token plus some whitespace,
// which everything currently is, but otherwise we'd have to expand _last or check _buf.
return false;
}
hasContent(): boolean {
return this._queue.length > 0 || !!this._last;
}
/**
* Certain sourcemap usecases expect mappings to be more accurate than
* Babel's generic sourcemap handling allows. For now, we special-case
* identifiers to allow for the primary cases to work.
* The goal of this line is to ensure that the map output from Babel will
* have an exact range on identifiers in the output code. Without this
* line, Babel would potentially include some number of trailing tokens
* that are printed after the identifier, but before another location has
* been assigned.
* This allows tooling like Rollup and Webpack to more accurately perform
* their own transformations. Most importantly, this allows the import/export
* transformations performed by those tools to loose less information when
* applying their own transformations on top of the code and map results
* generated by Babel itself.
*
* The primary example of this is the snippet:
*
* import mod from "mod";
* mod();
*
* With this line, there will be one mapping range over "mod" and another
* over "();", where previously it would have been a single mapping.
*/
exactSource(loc: any, cb: () => void) {
// In cases where parent expressions start at the same locations as the
// identifier itself, the current active location could already be the
// start of this range. We use 'force' here to explicitly start a new
// mapping range for this new token.
this.source("start", loc, true /* force */);
cb();
// In cases where tokens are printed after this item, we want to
// ensure that they get the location of the _end_ of the identifier.
// To accomplish this, we assign the location and explicitly disable
// the standard Buffer withSource previous-position "reactivation"
// logic. This means that if another item calls '.source()' to set
// the location after the identifier, it is fine, but the position won't
// be automatically replaced with the previous value.
this.source("end", loc);
this._disallowPop("start", loc);
}
/**
* Sets a given position as the current source location so generated code after this call
* will be given this position in the sourcemap.
*/
source(prop: string, loc: t.SourceLocation, force?: boolean): void {
if (prop && !loc) return;
// Since this is called extremely often, we re-use the same _sourcePosition
// object for the whole lifetime of the buffer.
this._normalizePosition(prop, loc, this._sourcePosition, force);
}
/**
* Call a callback with a specific source location and restore on completion.
*/
withSource(prop: string, loc: t.SourceLocation, cb: () => void): void {
if (!this._map) return cb();
// Use the call stack to manage a stack of "source location" data because
// the _sourcePosition object is mutated over the course of code generation,
// and constantly copying it would be slower.
const originalLine = this._sourcePosition.line;
const originalColumn = this._sourcePosition.column;
const originalFilename = this._sourcePosition.filename;
const originalIdentifierName = this._sourcePosition.identifierName;
this.source(prop, loc);
cb();
if (
// If the current active position is forced, we only want to reactivate
// the old position if it is different from the newest position.
(!this._sourcePosition.force ||
this._sourcePosition.line !== originalLine ||
this._sourcePosition.column !== originalColumn ||
this._sourcePosition.filename !== originalFilename) &&
// Verify if reactivating this specific position has been disallowed.
(!this._disallowedPop ||
this._disallowedPop.line !== originalLine ||
this._disallowedPop.column !== originalColumn ||
this._disallowedPop.filename !== originalFilename)
) {
this._sourcePosition.line = originalLine;
this._sourcePosition.column = originalColumn;
this._sourcePosition.filename = originalFilename;
this._sourcePosition.identifierName = originalIdentifierName;
this._sourcePosition.force = false;
this._disallowedPop = null;
}
}
/**
* Allow printers to disable the default location-reset behavior of the
* sourcemap output, so that certain printers can be sure that the
* "end" location that they set is actually treated as the end position.
*/
_disallowPop(prop: string, loc: t.SourceLocation) {
if (prop && !loc) return;
this._disallowedPop = this._normalizePosition(prop, loc);
}
_normalizePosition(prop: string, loc: any, targetObj?: any, force?: boolean) {
const pos = loc ? loc[prop] : null;
if (targetObj === undefined) {
// Initialize with fields so that the object doesn't change shape.
targetObj = {
identifierName: null,
line: null,
column: null,
filename: null,
force: false,
};
}
const origLine = targetObj.line;
const origColumn = targetObj.column;
const origFilename = targetObj.filename;
targetObj.identifierName =
(prop === "start" && loc?.identifierName) || null;
targetObj.line = pos?.line;
targetObj.column = pos?.column;
targetObj.filename = loc?.filename;
// We want to skip reassigning `force` if we're re-setting the same position.
if (
force ||
targetObj.line !== origLine ||
targetObj.column !== origColumn ||
targetObj.filename !== origFilename
) {
targetObj.force = force;
}
return targetObj;
}
getCurrentColumn(): number {
const extra = this._queue.reduce((acc, item) => item[0] + acc, "");
const lastIndex = extra.lastIndexOf("\n");
return lastIndex === -1
? this._position.column + extra.length
: extra.length - 1 - lastIndex;
}
getCurrentLine(): number {
const extra = this._queue.reduce((acc, item) => item[0] + acc, "");
let count = 0;
for (let i = 0; i < extra.length; i++) {
if (extra[i] === "\n") count++;
}
return this._position.line + count;
}
}