| 1 | "use strict"; |
| 2 | |
| 3 | Object.defineProperty(exports, "__esModule", { |
| 4 | value: true |
| 5 | }); |
| 6 | exports.default = void 0; |
| 7 | class Buffer { |
| 8 | constructor(map, indentChar) { |
| 9 | this._map = null; |
| 10 | this._buf = ""; |
| 11 | this._str = ""; |
| 12 | this._appendCount = 0; |
| 13 | this._last = 0; |
| 14 | this._queue = []; |
| 15 | this._queueCursor = 0; |
| 16 | this._canMarkIdName = true; |
| 17 | this._indentChar = ""; |
| 18 | this._fastIndentations = []; |
| 19 | this._position = { |
| 20 | line: 1, |
| 21 | column: 0 |
| 22 | }; |
| 23 | this._sourcePosition = { |
| 24 | identifierName: undefined, |
| 25 | identifierNamePos: undefined, |
| 26 | line: undefined, |
| 27 | column: undefined, |
| 28 | filename: undefined |
| 29 | }; |
| 30 | this._map = map; |
| 31 | this._indentChar = indentChar; |
| 32 | for (let i = 0; i < 64; i++) { |
| 33 | this._fastIndentations.push(indentChar.repeat(i)); |
| 34 | } |
| 35 | this._allocQueue(); |
| 36 | } |
| 37 | _allocQueue() { |
| 38 | const queue = this._queue; |
| 39 | for (let i = 0; i < 16; i++) { |
| 40 | queue.push({ |
| 41 | char: 0, |
| 42 | repeat: 1, |
| 43 | line: undefined, |
| 44 | column: undefined, |
| 45 | identifierName: undefined, |
| 46 | identifierNamePos: undefined, |
| 47 | filename: "" |
| 48 | }); |
| 49 | } |
| 50 | } |
| 51 | _pushQueue(char, repeat, line, column, filename) { |
| 52 | const cursor = this._queueCursor; |
| 53 | if (cursor === this._queue.length) { |
| 54 | this._allocQueue(); |
| 55 | } |
| 56 | const item = this._queue[cursor]; |
| 57 | item.char = char; |
| 58 | item.repeat = repeat; |
| 59 | item.line = line; |
| 60 | item.column = column; |
| 61 | item.filename = filename; |
| 62 | this._queueCursor++; |
| 63 | } |
| 64 | _popQueue() { |
| 65 | if (this._queueCursor === 0) { |
| 66 | throw new Error("Cannot pop from empty queue"); |
| 67 | } |
| 68 | return this._queue[--this._queueCursor]; |
| 69 | } |
| 70 | get() { |
| 71 | this._flush(); |
| 72 | const map = this._map; |
| 73 | const result = { |
| 74 | code: (this._buf + this._str).trimRight(), |
| 75 | decodedMap: map == null ? void 0 : map.getDecoded(), |
| 76 | get __mergedMap() { |
| 77 | return this.map; |
| 78 | }, |
| 79 | get map() { |
| 80 | const resultMap = map ? map.get() : null; |
| 81 | result.map = resultMap; |
| 82 | return resultMap; |
| 83 | }, |
| 84 | set map(value) { |
| 85 | Object.defineProperty(result, "map", { |
| 86 | value, |
| 87 | writable: true |
| 88 | }); |
| 89 | }, |
| 90 | get rawMappings() { |
| 91 | const mappings = map == null ? void 0 : map.getRawMappings(); |
| 92 | result.rawMappings = mappings; |
| 93 | return mappings; |
| 94 | }, |
| 95 | set rawMappings(value) { |
| 96 | Object.defineProperty(result, "rawMappings", { |
| 97 | value, |
| 98 | writable: true |
| 99 | }); |
| 100 | } |
| 101 | }; |
| 102 | return result; |
| 103 | } |
| 104 | append(str, maybeNewline) { |
| 105 | this._flush(); |
| 106 | this._append(str, this._sourcePosition, maybeNewline); |
| 107 | } |
| 108 | appendChar(char) { |
| 109 | this._flush(); |
| 110 | this._appendChar(char, 1, this._sourcePosition); |
| 111 | } |
| 112 | queue(char) { |
| 113 | if (char === 10) { |
| 114 | while (this._queueCursor !== 0) { |
| 115 | const char = this._queue[this._queueCursor - 1].char; |
| 116 | if (char !== 32 && char !== 9) { |
| 117 | break; |
| 118 | } |
| 119 | this._queueCursor--; |
| 120 | } |
| 121 | } |
| 122 | const sourcePosition = this._sourcePosition; |
| 123 | this._pushQueue(char, 1, sourcePosition.line, sourcePosition.column, sourcePosition.filename); |
| 124 | } |
| 125 | queueIndentation(repeat) { |
| 126 | if (repeat === 0) return; |
| 127 | this._pushQueue(-1, repeat, undefined, undefined, undefined); |
| 128 | } |
| 129 | _flush() { |
| 130 | const queueCursor = this._queueCursor; |
| 131 | const queue = this._queue; |
| 132 | for (let i = 0; i < queueCursor; i++) { |
| 133 | const item = queue[i]; |
| 134 | this._appendChar(item.char, item.repeat, item); |
| 135 | } |
| 136 | this._queueCursor = 0; |
| 137 | } |
| 138 | _appendChar(char, repeat, sourcePos) { |
| 139 | this._last = char; |
| 140 | if (char === -1) { |
| 141 | const fastIndentation = this._fastIndentations[repeat]; |
| 142 | if (fastIndentation !== undefined) { |
| 143 | this._str += fastIndentation; |
| 144 | } else { |
| 145 | this._str += repeat > 1 ? this._indentChar.repeat(repeat) : this._indentChar; |
| 146 | } |
| 147 | } else { |
| 148 | this._str += repeat > 1 ? String.fromCharCode(char).repeat(repeat) : String.fromCharCode(char); |
| 149 | } |
| 150 | if (char !== 10) { |
| 151 | this._mark(sourcePos.line, sourcePos.column, sourcePos.identifierName, sourcePos.identifierNamePos, sourcePos.filename); |
| 152 | this._position.column += repeat; |
| 153 | } else { |
| 154 | this._position.line++; |
| 155 | this._position.column = 0; |
| 156 | } |
| 157 | if (this._canMarkIdName) { |
| 158 | sourcePos.identifierName = undefined; |
| 159 | sourcePos.identifierNamePos = undefined; |
| 160 | } |
| 161 | } |
| 162 | _append(str, sourcePos, maybeNewline) { |
| 163 | const len = str.length; |
| 164 | const position = this._position; |
| 165 | this._last = str.charCodeAt(len - 1); |
| 166 | if (++this._appendCount > 4096) { |
| 167 | +this._str; |
| 168 | this._buf += this._str; |
| 169 | this._str = str; |
| 170 | this._appendCount = 0; |
| 171 | } else { |
| 172 | this._str += str; |
| 173 | } |
| 174 | if (!maybeNewline && !this._map) { |
| 175 | position.column += len; |
| 176 | return; |
| 177 | } |
| 178 | const { |
| 179 | column, |
| 180 | identifierName, |
| 181 | identifierNamePos, |
| 182 | filename |
| 183 | } = sourcePos; |
| 184 | let line = sourcePos.line; |
| 185 | if ((identifierName != null || identifierNamePos != null) && this._canMarkIdName) { |
| 186 | sourcePos.identifierName = undefined; |
| 187 | sourcePos.identifierNamePos = undefined; |
| 188 | } |
| 189 | let i = str.indexOf("\n"); |
| 190 | let last = 0; |
| 191 | if (i !== 0) { |
| 192 | this._mark(line, column, identifierName, identifierNamePos, filename); |
| 193 | } |
| 194 | while (i !== -1) { |
| 195 | position.line++; |
| 196 | position.column = 0; |
| 197 | last = i + 1; |
| 198 | if (last < len && line !== undefined) { |
| 199 | this._mark(++line, 0, undefined, undefined, filename); |
| 200 | } |
| 201 | i = str.indexOf("\n", last); |
| 202 | } |
| 203 | position.column += len - last; |
| 204 | } |
| 205 | _mark(line, column, identifierName, identifierNamePos, filename) { |
| 206 | var _this$_map; |
| 207 | (_this$_map = this._map) == null || _this$_map.mark(this._position, line, column, identifierName, identifierNamePos, filename); |
| 208 | } |
| 209 | removeTrailingNewline() { |
| 210 | const queueCursor = this._queueCursor; |
| 211 | if (queueCursor !== 0 && this._queue[queueCursor - 1].char === 10) { |
| 212 | this._queueCursor--; |
| 213 | } |
| 214 | } |
| 215 | removeLastSemicolon() { |
| 216 | const queueCursor = this._queueCursor; |
| 217 | if (queueCursor !== 0 && this._queue[queueCursor - 1].char === 59) { |
| 218 | this._queueCursor--; |
| 219 | } |
| 220 | } |
| 221 | getLastChar() { |
| 222 | const queueCursor = this._queueCursor; |
| 223 | return queueCursor !== 0 ? this._queue[queueCursor - 1].char : this._last; |
| 224 | } |
| 225 | getNewlineCount() { |
| 226 | const queueCursor = this._queueCursor; |
| 227 | let count = 0; |
| 228 | if (queueCursor === 0) return this._last === 10 ? 1 : 0; |
| 229 | for (let i = queueCursor - 1; i >= 0; i--) { |
| 230 | if (this._queue[i].char !== 10) { |
| 231 | break; |
| 232 | } |
| 233 | count++; |
| 234 | } |
| 235 | return count === queueCursor && this._last === 10 ? count + 1 : count; |
| 236 | } |
| 237 | endsWithCharAndNewline() { |
| 238 | const queue = this._queue; |
| 239 | const queueCursor = this._queueCursor; |
| 240 | if (queueCursor !== 0) { |
| 241 | const lastCp = queue[queueCursor - 1].char; |
| 242 | if (lastCp !== 10) return; |
| 243 | if (queueCursor > 1) { |
| 244 | return queue[queueCursor - 2].char; |
| 245 | } else { |
| 246 | return this._last; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | hasContent() { |
| 251 | return this._queueCursor !== 0 || !!this._last; |
| 252 | } |
| 253 | exactSource(loc, cb) { |
| 254 | if (!this._map) { |
| 255 | cb(); |
| 256 | return; |
| 257 | } |
| 258 | this.source("start", loc); |
| 259 | const identifierName = loc.identifierName; |
| 260 | const sourcePos = this._sourcePosition; |
| 261 | if (identifierName) { |
| 262 | this._canMarkIdName = false; |
| 263 | sourcePos.identifierName = identifierName; |
| 264 | } |
| 265 | cb(); |
| 266 | if (identifierName) { |
| 267 | this._canMarkIdName = true; |
| 268 | sourcePos.identifierName = undefined; |
| 269 | sourcePos.identifierNamePos = undefined; |
| 270 | } |
| 271 | this.source("end", loc); |
| 272 | } |
| 273 | source(prop, loc) { |
| 274 | if (!this._map) return; |
| 275 | this._normalizePosition(prop, loc, 0); |
| 276 | } |
| 277 | sourceWithOffset(prop, loc, columnOffset) { |
| 278 | if (!this._map) return; |
| 279 | this._normalizePosition(prop, loc, columnOffset); |
| 280 | } |
| 281 | _normalizePosition(prop, loc, columnOffset) { |
| 282 | const pos = loc[prop]; |
| 283 | const target = this._sourcePosition; |
| 284 | if (pos) { |
| 285 | target.line = pos.line; |
| 286 | target.column = Math.max(pos.column + columnOffset, 0); |
| 287 | target.filename = loc.filename; |
| 288 | } |
| 289 | } |
| 290 | getCurrentColumn() { |
| 291 | const queue = this._queue; |
| 292 | const queueCursor = this._queueCursor; |
| 293 | let lastIndex = -1; |
| 294 | let len = 0; |
| 295 | for (let i = 0; i < queueCursor; i++) { |
| 296 | const item = queue[i]; |
| 297 | if (item.char === 10) { |
| 298 | lastIndex = len; |
| 299 | } |
| 300 | len += item.repeat; |
| 301 | } |
| 302 | return lastIndex === -1 ? this._position.column + len : len - 1 - lastIndex; |
| 303 | } |
| 304 | getCurrentLine() { |
| 305 | let count = 0; |
| 306 | const queue = this._queue; |
| 307 | for (let i = 0; i < this._queueCursor; i++) { |
| 308 | if (queue[i].char === 10) { |
| 309 | count++; |
| 310 | } |
| 311 | } |
| 312 | return this._position.line + count; |
| 313 | } |
| 314 | } |
| 315 | exports.default = Buffer; |
| 316 | |
| 317 | //# sourceMappingURL=buffer.js.map |
| 318 |