1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = void 0;
|
---|
7 | const SPACES_RE = /^[ \t]+$/;
|
---|
8 |
|
---|
9 | class Buffer {
|
---|
10 | constructor(map) {
|
---|
11 | this._map = null;
|
---|
12 | this._buf = [];
|
---|
13 | this._last = "";
|
---|
14 | this._queue = [];
|
---|
15 | this._position = {
|
---|
16 | line: 1,
|
---|
17 | column: 0
|
---|
18 | };
|
---|
19 | this._sourcePosition = {
|
---|
20 | identifierName: null,
|
---|
21 | line: null,
|
---|
22 | column: null,
|
---|
23 | filename: null
|
---|
24 | };
|
---|
25 | this._disallowedPop = null;
|
---|
26 | this._map = map;
|
---|
27 | }
|
---|
28 |
|
---|
29 | get() {
|
---|
30 | this._flush();
|
---|
31 |
|
---|
32 | const map = this._map;
|
---|
33 | const result = {
|
---|
34 | code: this._buf.join("").trimRight(),
|
---|
35 | map: null,
|
---|
36 | rawMappings: map == null ? void 0 : map.getRawMappings()
|
---|
37 | };
|
---|
38 |
|
---|
39 | if (map) {
|
---|
40 | Object.defineProperty(result, "map", {
|
---|
41 | configurable: true,
|
---|
42 | enumerable: true,
|
---|
43 |
|
---|
44 | get() {
|
---|
45 | return this.map = map.get();
|
---|
46 | },
|
---|
47 |
|
---|
48 | set(value) {
|
---|
49 | Object.defineProperty(this, "map", {
|
---|
50 | value,
|
---|
51 | writable: true
|
---|
52 | });
|
---|
53 | }
|
---|
54 |
|
---|
55 | });
|
---|
56 | }
|
---|
57 |
|
---|
58 | return result;
|
---|
59 | }
|
---|
60 |
|
---|
61 | append(str) {
|
---|
62 | this._flush();
|
---|
63 |
|
---|
64 | const {
|
---|
65 | line,
|
---|
66 | column,
|
---|
67 | filename,
|
---|
68 | identifierName,
|
---|
69 | force
|
---|
70 | } = this._sourcePosition;
|
---|
71 |
|
---|
72 | this._append(str, line, column, identifierName, filename, force);
|
---|
73 | }
|
---|
74 |
|
---|
75 | queue(str) {
|
---|
76 | if (str === "\n") {
|
---|
77 | while (this._queue.length > 0 && SPACES_RE.test(this._queue[0][0])) {
|
---|
78 | this._queue.shift();
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | const {
|
---|
83 | line,
|
---|
84 | column,
|
---|
85 | filename,
|
---|
86 | identifierName,
|
---|
87 | force
|
---|
88 | } = this._sourcePosition;
|
---|
89 |
|
---|
90 | this._queue.unshift([str, line, column, identifierName, filename, force]);
|
---|
91 | }
|
---|
92 |
|
---|
93 | _flush() {
|
---|
94 | let item;
|
---|
95 |
|
---|
96 | while (item = this._queue.pop()) {
|
---|
97 | this._append(...item);
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | _append(str, line, column, identifierName, filename, force) {
|
---|
102 | this._buf.push(str);
|
---|
103 |
|
---|
104 | this._last = str[str.length - 1];
|
---|
105 | let i = str.indexOf("\n");
|
---|
106 | let last = 0;
|
---|
107 |
|
---|
108 | if (i !== 0) {
|
---|
109 | this._mark(line, column, identifierName, filename, force);
|
---|
110 | }
|
---|
111 |
|
---|
112 | while (i !== -1) {
|
---|
113 | this._position.line++;
|
---|
114 | this._position.column = 0;
|
---|
115 | last = i + 1;
|
---|
116 |
|
---|
117 | if (last < str.length) {
|
---|
118 | this._mark(++line, 0, identifierName, filename, force);
|
---|
119 | }
|
---|
120 |
|
---|
121 | i = str.indexOf("\n", last);
|
---|
122 | }
|
---|
123 |
|
---|
124 | this._position.column += str.length - last;
|
---|
125 | }
|
---|
126 |
|
---|
127 | _mark(line, column, identifierName, filename, force) {
|
---|
128 | var _this$_map;
|
---|
129 |
|
---|
130 | (_this$_map = this._map) == null ? void 0 : _this$_map.mark(this._position.line, this._position.column, line, column, identifierName, filename, force);
|
---|
131 | }
|
---|
132 |
|
---|
133 | removeTrailingNewline() {
|
---|
134 | if (this._queue.length > 0 && this._queue[0][0] === "\n") {
|
---|
135 | this._queue.shift();
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | removeLastSemicolon() {
|
---|
140 | if (this._queue.length > 0 && this._queue[0][0] === ";") {
|
---|
141 | this._queue.shift();
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | endsWith(suffix) {
|
---|
146 | if (suffix.length === 1) {
|
---|
147 | let last;
|
---|
148 |
|
---|
149 | if (this._queue.length > 0) {
|
---|
150 | const str = this._queue[0][0];
|
---|
151 | last = str[str.length - 1];
|
---|
152 | } else {
|
---|
153 | last = this._last;
|
---|
154 | }
|
---|
155 |
|
---|
156 | return last === suffix;
|
---|
157 | }
|
---|
158 |
|
---|
159 | const end = this._last + this._queue.reduce((acc, item) => item[0] + acc, "");
|
---|
160 |
|
---|
161 | if (suffix.length <= end.length) {
|
---|
162 | return end.slice(-suffix.length) === suffix;
|
---|
163 | }
|
---|
164 |
|
---|
165 | return false;
|
---|
166 | }
|
---|
167 |
|
---|
168 | hasContent() {
|
---|
169 | return this._queue.length > 0 || !!this._last;
|
---|
170 | }
|
---|
171 |
|
---|
172 | exactSource(loc, cb) {
|
---|
173 | this.source("start", loc, true);
|
---|
174 | cb();
|
---|
175 | this.source("end", loc);
|
---|
176 |
|
---|
177 | this._disallowPop("start", loc);
|
---|
178 | }
|
---|
179 |
|
---|
180 | source(prop, loc, force) {
|
---|
181 | if (prop && !loc) return;
|
---|
182 |
|
---|
183 | this._normalizePosition(prop, loc, this._sourcePosition, force);
|
---|
184 | }
|
---|
185 |
|
---|
186 | withSource(prop, loc, cb) {
|
---|
187 | if (!this._map) return cb();
|
---|
188 | const originalLine = this._sourcePosition.line;
|
---|
189 | const originalColumn = this._sourcePosition.column;
|
---|
190 | const originalFilename = this._sourcePosition.filename;
|
---|
191 | const originalIdentifierName = this._sourcePosition.identifierName;
|
---|
192 | this.source(prop, loc);
|
---|
193 | cb();
|
---|
194 |
|
---|
195 | if ((!this._sourcePosition.force || this._sourcePosition.line !== originalLine || this._sourcePosition.column !== originalColumn || this._sourcePosition.filename !== originalFilename) && (!this._disallowedPop || this._disallowedPop.line !== originalLine || this._disallowedPop.column !== originalColumn || this._disallowedPop.filename !== originalFilename)) {
|
---|
196 | this._sourcePosition.line = originalLine;
|
---|
197 | this._sourcePosition.column = originalColumn;
|
---|
198 | this._sourcePosition.filename = originalFilename;
|
---|
199 | this._sourcePosition.identifierName = originalIdentifierName;
|
---|
200 | this._sourcePosition.force = false;
|
---|
201 | this._disallowedPop = null;
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | _disallowPop(prop, loc) {
|
---|
206 | if (prop && !loc) return;
|
---|
207 | this._disallowedPop = this._normalizePosition(prop, loc);
|
---|
208 | }
|
---|
209 |
|
---|
210 | _normalizePosition(prop, loc, targetObj, force) {
|
---|
211 | const pos = loc ? loc[prop] : null;
|
---|
212 |
|
---|
213 | if (targetObj === undefined) {
|
---|
214 | targetObj = {
|
---|
215 | identifierName: null,
|
---|
216 | line: null,
|
---|
217 | column: null,
|
---|
218 | filename: null,
|
---|
219 | force: false
|
---|
220 | };
|
---|
221 | }
|
---|
222 |
|
---|
223 | const origLine = targetObj.line;
|
---|
224 | const origColumn = targetObj.column;
|
---|
225 | const origFilename = targetObj.filename;
|
---|
226 | targetObj.identifierName = prop === "start" && (loc == null ? void 0 : loc.identifierName) || null;
|
---|
227 | targetObj.line = pos == null ? void 0 : pos.line;
|
---|
228 | targetObj.column = pos == null ? void 0 : pos.column;
|
---|
229 | targetObj.filename = loc == null ? void 0 : loc.filename;
|
---|
230 |
|
---|
231 | if (force || targetObj.line !== origLine || targetObj.column !== origColumn || targetObj.filename !== origFilename) {
|
---|
232 | targetObj.force = force;
|
---|
233 | }
|
---|
234 |
|
---|
235 | return targetObj;
|
---|
236 | }
|
---|
237 |
|
---|
238 | getCurrentColumn() {
|
---|
239 | const extra = this._queue.reduce((acc, item) => item[0] + acc, "");
|
---|
240 |
|
---|
241 | const lastIndex = extra.lastIndexOf("\n");
|
---|
242 | return lastIndex === -1 ? this._position.column + extra.length : extra.length - 1 - lastIndex;
|
---|
243 | }
|
---|
244 |
|
---|
245 | getCurrentLine() {
|
---|
246 | const extra = this._queue.reduce((acc, item) => item[0] + acc, "");
|
---|
247 |
|
---|
248 | let count = 0;
|
---|
249 |
|
---|
250 | for (let i = 0; i < extra.length; i++) {
|
---|
251 | if (extra[i] === "\n") count++;
|
---|
252 | }
|
---|
253 |
|
---|
254 | return this._position.line + count;
|
---|
255 | }
|
---|
256 |
|
---|
257 | }
|
---|
258 |
|
---|
259 | exports.default = Buffer; |
---|