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 = 0;
|
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.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 += str;
|
103 | this._last = str.charCodeAt(str.length - 1);
|
104 | let i = str.indexOf("\n");
|
105 | let last = 0;
|
106 |
|
107 | if (i !== 0) {
|
108 | this._mark(line, column, identifierName, filename, force);
|
109 | }
|
110 |
|
111 | while (i !== -1) {
|
112 | this._position.line++;
|
113 | this._position.column = 0;
|
114 | last = i + 1;
|
115 |
|
116 | if (last < str.length) {
|
117 | this._mark(++line, 0, identifierName, filename, force);
|
118 | }
|
119 |
|
120 | i = str.indexOf("\n", last);
|
121 | }
|
122 |
|
123 | this._position.column += str.length - last;
|
124 | }
|
125 |
|
126 | _mark(line, column, identifierName, filename, force) {
|
127 | var _this$_map;
|
128 |
|
129 | (_this$_map = this._map) == null ? void 0 : _this$_map.mark(this._position.line, this._position.column, line, column, identifierName, filename, force);
|
130 | }
|
131 |
|
132 | removeTrailingNewline() {
|
133 | if (this._queue.length > 0 && this._queue[0][0] === "\n") {
|
134 | this._queue.shift();
|
135 | }
|
136 | }
|
137 |
|
138 | removeLastSemicolon() {
|
139 | if (this._queue.length > 0 && this._queue[0][0] === ";") {
|
140 | this._queue.shift();
|
141 | }
|
142 | }
|
143 |
|
144 | getLastChar() {
|
145 | let last;
|
146 |
|
147 | if (this._queue.length > 0) {
|
148 | const str = this._queue[0][0];
|
149 | last = str.charCodeAt(0);
|
150 | } else {
|
151 | last = this._last;
|
152 | }
|
153 |
|
154 | return last;
|
155 | }
|
156 |
|
157 | endsWithCharAndNewline() {
|
158 | const queue = this._queue;
|
159 |
|
160 | if (queue.length > 0) {
|
161 | const last = queue[0][0];
|
162 | const lastCp = last.charCodeAt(0);
|
163 | if (lastCp !== 10) return;
|
164 |
|
165 | if (queue.length > 1) {
|
166 | const secondLast = queue[1][0];
|
167 | return secondLast.charCodeAt(0);
|
168 | } else {
|
169 | return this._last;
|
170 | }
|
171 | }
|
172 | }
|
173 |
|
174 | hasContent() {
|
175 | return this._queue.length > 0 || !!this._last;
|
176 | }
|
177 |
|
178 | exactSource(loc, cb) {
|
179 | this.source("start", loc, true);
|
180 | cb();
|
181 | this.source("end", loc);
|
182 |
|
183 | this._disallowPop("start", loc);
|
184 | }
|
185 |
|
186 | source(prop, loc, force) {
|
187 | if (prop && !loc) return;
|
188 |
|
189 | this._normalizePosition(prop, loc, this._sourcePosition, force);
|
190 | }
|
191 |
|
192 | withSource(prop, loc, cb) {
|
193 | if (!this._map) return cb();
|
194 | const originalLine = this._sourcePosition.line;
|
195 | const originalColumn = this._sourcePosition.column;
|
196 | const originalFilename = this._sourcePosition.filename;
|
197 | const originalIdentifierName = this._sourcePosition.identifierName;
|
198 | this.source(prop, loc);
|
199 | cb();
|
200 |
|
201 | 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)) {
|
202 | this._sourcePosition.line = originalLine;
|
203 | this._sourcePosition.column = originalColumn;
|
204 | this._sourcePosition.filename = originalFilename;
|
205 | this._sourcePosition.identifierName = originalIdentifierName;
|
206 | this._sourcePosition.force = false;
|
207 | this._disallowedPop = null;
|
208 | }
|
209 | }
|
210 |
|
211 | _disallowPop(prop, loc) {
|
212 | if (prop && !loc) return;
|
213 | this._disallowedPop = this._normalizePosition(prop, loc);
|
214 | }
|
215 |
|
216 | _normalizePosition(prop, loc, targetObj, force) {
|
217 | const pos = loc ? loc[prop] : null;
|
218 |
|
219 | if (targetObj === undefined) {
|
220 | targetObj = {
|
221 | identifierName: null,
|
222 | line: null,
|
223 | column: null,
|
224 | filename: null,
|
225 | force: false
|
226 | };
|
227 | }
|
228 |
|
229 | const origLine = targetObj.line;
|
230 | const origColumn = targetObj.column;
|
231 | const origFilename = targetObj.filename;
|
232 | targetObj.identifierName = prop === "start" && (loc == null ? void 0 : loc.identifierName) || null;
|
233 | targetObj.line = pos == null ? void 0 : pos.line;
|
234 | targetObj.column = pos == null ? void 0 : pos.column;
|
235 | targetObj.filename = loc == null ? void 0 : loc.filename;
|
236 |
|
237 | if (force || targetObj.line !== origLine || targetObj.column !== origColumn || targetObj.filename !== origFilename) {
|
238 | targetObj.force = force;
|
239 | }
|
240 |
|
241 | return targetObj;
|
242 | }
|
243 |
|
244 | getCurrentColumn() {
|
245 | const extra = this._queue.reduce((acc, item) => item[0] + acc, "");
|
246 |
|
247 | const lastIndex = extra.lastIndexOf("\n");
|
248 | return lastIndex === -1 ? this._position.column + extra.length : extra.length - 1 - lastIndex;
|
249 | }
|
250 |
|
251 | getCurrentLine() {
|
252 | const extra = this._queue.reduce((acc, item) => item[0] + acc, "");
|
253 |
|
254 | let count = 0;
|
255 |
|
256 | for (let i = 0; i < extra.length; i++) {
|
257 | if (extra[i] === "\n") count++;
|
258 | }
|
259 |
|
260 | return this._position.line + count;
|
261 | }
|
262 |
|
263 | }
|
264 |
|
265 | exports.default = Buffer; |
\ | No newline at end of file |