| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.default = void 0;
|
|---|
| 7 | const spaceIndents = [];
|
|---|
| 8 | for (let i = 0; i < 32; i++) {
|
|---|
| 9 | spaceIndents.push(" ".repeat(i * 2));
|
|---|
| 10 | }
|
|---|
| 11 | class Buffer {
|
|---|
| 12 | constructor(map, indentChar) {
|
|---|
| 13 | this._map = null;
|
|---|
| 14 | this._buf = "";
|
|---|
| 15 | this._str = "";
|
|---|
| 16 | this._appendCount = 0;
|
|---|
| 17 | this._last = 0;
|
|---|
| 18 | this._canMarkIdName = true;
|
|---|
| 19 | this._indentChar = "";
|
|---|
| 20 | this._queuedChar = 0;
|
|---|
| 21 | this._position = {
|
|---|
| 22 | line: 1,
|
|---|
| 23 | column: 0
|
|---|
| 24 | };
|
|---|
| 25 | this._sourcePosition = {
|
|---|
| 26 | identifierName: undefined,
|
|---|
| 27 | identifierNamePos: undefined,
|
|---|
| 28 | line: undefined,
|
|---|
| 29 | column: undefined,
|
|---|
| 30 | filename: undefined
|
|---|
| 31 | };
|
|---|
| 32 | this._map = map;
|
|---|
| 33 | this._indentChar = indentChar;
|
|---|
| 34 | }
|
|---|
| 35 | get() {
|
|---|
| 36 | const {
|
|---|
| 37 | _map,
|
|---|
| 38 | _last
|
|---|
| 39 | } = this;
|
|---|
| 40 | if (this._queuedChar !== 32) {
|
|---|
| 41 | this._flush();
|
|---|
| 42 | }
|
|---|
| 43 | const code = _last === 10 ? (this._buf + this._str).trimRight() : this._buf + this._str;
|
|---|
| 44 | if (_map === null) {
|
|---|
| 45 | return {
|
|---|
| 46 | code: code,
|
|---|
| 47 | decodedMap: undefined,
|
|---|
| 48 | map: null,
|
|---|
| 49 | rawMappings: undefined
|
|---|
| 50 | };
|
|---|
| 51 | }
|
|---|
| 52 | const result = {
|
|---|
| 53 | code: code,
|
|---|
| 54 | decodedMap: _map.getDecoded(),
|
|---|
| 55 | get __mergedMap() {
|
|---|
| 56 | return this.map;
|
|---|
| 57 | },
|
|---|
| 58 | get map() {
|
|---|
| 59 | const resultMap = _map.get();
|
|---|
| 60 | result.map = resultMap;
|
|---|
| 61 | return resultMap;
|
|---|
| 62 | },
|
|---|
| 63 | set map(value) {
|
|---|
| 64 | Object.defineProperty(result, "map", {
|
|---|
| 65 | value,
|
|---|
| 66 | writable: true
|
|---|
| 67 | });
|
|---|
| 68 | },
|
|---|
| 69 | get rawMappings() {
|
|---|
| 70 | const mappings = _map.getRawMappings();
|
|---|
| 71 | result.rawMappings = mappings;
|
|---|
| 72 | return mappings;
|
|---|
| 73 | },
|
|---|
| 74 | set rawMappings(value) {
|
|---|
| 75 | Object.defineProperty(result, "rawMappings", {
|
|---|
| 76 | value,
|
|---|
| 77 | writable: true
|
|---|
| 78 | });
|
|---|
| 79 | }
|
|---|
| 80 | };
|
|---|
| 81 | return result;
|
|---|
| 82 | }
|
|---|
| 83 | append(str, maybeNewline) {
|
|---|
| 84 | this._flush();
|
|---|
| 85 | this._append(str, maybeNewline);
|
|---|
| 86 | }
|
|---|
| 87 | appendChar(char) {
|
|---|
| 88 | this._flush();
|
|---|
| 89 | this._appendChar(char, 1, true);
|
|---|
| 90 | }
|
|---|
| 91 | queue(char) {
|
|---|
| 92 | this._flush();
|
|---|
| 93 | this._queuedChar = char;
|
|---|
| 94 | }
|
|---|
| 95 | _flush() {
|
|---|
| 96 | const queuedChar = this._queuedChar;
|
|---|
| 97 | if (queuedChar !== 0) {
|
|---|
| 98 | this._appendChar(queuedChar, 1, true);
|
|---|
| 99 | this._queuedChar = 0;
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 | _appendChar(char, repeat, useSourcePos) {
|
|---|
| 103 | this._last = char;
|
|---|
| 104 | if (char === -1) {
|
|---|
| 105 | const indent = repeat >= 64 ? this._indentChar.repeat(repeat) : spaceIndents[repeat / 2];
|
|---|
| 106 | this._str += indent;
|
|---|
| 107 | } else {
|
|---|
| 108 | this._str += repeat > 1 ? String.fromCharCode(char).repeat(repeat) : String.fromCharCode(char);
|
|---|
| 109 | }
|
|---|
| 110 | const isSpace = char === 32;
|
|---|
| 111 | const position = this._position;
|
|---|
| 112 | if (char !== 10) {
|
|---|
| 113 | if (this._map) {
|
|---|
| 114 | const sourcePos = this._sourcePosition;
|
|---|
| 115 | if (useSourcePos && sourcePos) {
|
|---|
| 116 | this._map.mark(position, sourcePos.line, sourcePos.column, isSpace ? undefined : sourcePos.identifierName, isSpace ? undefined : sourcePos.identifierNamePos, sourcePos.filename);
|
|---|
| 117 | if (!isSpace && this._canMarkIdName) {
|
|---|
| 118 | sourcePos.identifierName = undefined;
|
|---|
| 119 | sourcePos.identifierNamePos = undefined;
|
|---|
| 120 | }
|
|---|
| 121 | } else {
|
|---|
| 122 | this._map.mark(position);
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 | position.column += repeat;
|
|---|
| 126 | } else {
|
|---|
| 127 | position.line++;
|
|---|
| 128 | position.column = 0;
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 | _append(str, maybeNewline) {
|
|---|
| 132 | const len = str.length;
|
|---|
| 133 | const position = this._position;
|
|---|
| 134 | const sourcePos = this._sourcePosition;
|
|---|
| 135 | this._last = -1;
|
|---|
| 136 | if (++this._appendCount > 4096) {
|
|---|
| 137 | +this._str;
|
|---|
| 138 | this._buf += this._str;
|
|---|
| 139 | this._str = str;
|
|---|
| 140 | this._appendCount = 0;
|
|---|
| 141 | } else {
|
|---|
| 142 | this._str += str;
|
|---|
| 143 | }
|
|---|
| 144 | const hasMap = this._map !== null;
|
|---|
| 145 | if (!maybeNewline && !hasMap) {
|
|---|
| 146 | position.column += len;
|
|---|
| 147 | return;
|
|---|
| 148 | }
|
|---|
| 149 | const {
|
|---|
| 150 | column,
|
|---|
| 151 | identifierName,
|
|---|
| 152 | identifierNamePos,
|
|---|
| 153 | filename
|
|---|
| 154 | } = sourcePos;
|
|---|
| 155 | let line = sourcePos.line;
|
|---|
| 156 | if ((identifierName != null || identifierNamePos != null) && this._canMarkIdName) {
|
|---|
| 157 | sourcePos.identifierName = undefined;
|
|---|
| 158 | sourcePos.identifierNamePos = undefined;
|
|---|
| 159 | }
|
|---|
| 160 | let i = str.indexOf("\n");
|
|---|
| 161 | let last = 0;
|
|---|
| 162 | if (hasMap && i !== 0) {
|
|---|
| 163 | this._map.mark(position, line, column, identifierName, identifierNamePos, filename);
|
|---|
| 164 | }
|
|---|
| 165 | while (i !== -1) {
|
|---|
| 166 | position.line++;
|
|---|
| 167 | position.column = 0;
|
|---|
| 168 | last = i + 1;
|
|---|
| 169 | if (last < len && line !== undefined) {
|
|---|
| 170 | line++;
|
|---|
| 171 | if (hasMap) {
|
|---|
| 172 | this._map.mark(position, line, 0, undefined, undefined, filename);
|
|---|
| 173 | }
|
|---|
| 174 | }
|
|---|
| 175 | i = str.indexOf("\n", last);
|
|---|
| 176 | }
|
|---|
| 177 | position.column += len - last;
|
|---|
| 178 | }
|
|---|
| 179 | removeLastSemicolon() {
|
|---|
| 180 | if (this._queuedChar === 59) {
|
|---|
| 181 | this._queuedChar = 0;
|
|---|
| 182 | }
|
|---|
| 183 | }
|
|---|
| 184 | getLastChar(checkQueue) {
|
|---|
| 185 | if (!checkQueue) {
|
|---|
| 186 | return this._last;
|
|---|
| 187 | }
|
|---|
| 188 | const queuedChar = this._queuedChar;
|
|---|
| 189 | return queuedChar !== 0 ? queuedChar : this._last;
|
|---|
| 190 | }
|
|---|
| 191 | getNewlineCount() {
|
|---|
| 192 | return this._queuedChar === 0 && this._last === 10 ? 1 : 0;
|
|---|
| 193 | }
|
|---|
| 194 | hasContent() {
|
|---|
| 195 | return this._last !== 0;
|
|---|
| 196 | }
|
|---|
| 197 | exactSource(loc, cb) {
|
|---|
| 198 | if (!this._map) {
|
|---|
| 199 | cb();
|
|---|
| 200 | return;
|
|---|
| 201 | }
|
|---|
| 202 | this.source("start", loc);
|
|---|
| 203 | const identifierName = loc.identifierName;
|
|---|
| 204 | const sourcePos = this._sourcePosition;
|
|---|
| 205 | if (identifierName != null) {
|
|---|
| 206 | this._canMarkIdName = false;
|
|---|
| 207 | sourcePos.identifierName = identifierName;
|
|---|
| 208 | }
|
|---|
| 209 | cb();
|
|---|
| 210 | if (identifierName != null) {
|
|---|
| 211 | this._canMarkIdName = true;
|
|---|
| 212 | sourcePos.identifierName = undefined;
|
|---|
| 213 | sourcePos.identifierNamePos = undefined;
|
|---|
| 214 | }
|
|---|
| 215 | this.source("end", loc);
|
|---|
| 216 | }
|
|---|
| 217 | source(prop, loc) {
|
|---|
| 218 | if (!this._map) return;
|
|---|
| 219 | this._normalizePosition(prop, loc, 0);
|
|---|
| 220 | }
|
|---|
| 221 | sourceWithOffset(prop, loc, columnOffset) {
|
|---|
| 222 | if (!this._map) return;
|
|---|
| 223 | this._normalizePosition(prop, loc, columnOffset);
|
|---|
| 224 | }
|
|---|
| 225 | _normalizePosition(prop, loc, columnOffset) {
|
|---|
| 226 | this._flush();
|
|---|
| 227 | const pos = loc[prop];
|
|---|
| 228 | const target = this._sourcePosition;
|
|---|
| 229 | if (pos) {
|
|---|
| 230 | target.line = pos.line;
|
|---|
| 231 | target.column = Math.max(pos.column + columnOffset, 0);
|
|---|
| 232 | target.filename = loc.filename;
|
|---|
| 233 | }
|
|---|
| 234 | }
|
|---|
| 235 | getCurrentColumn() {
|
|---|
| 236 | return this._position.column + (this._queuedChar ? 1 : 0);
|
|---|
| 237 | }
|
|---|
| 238 | getCurrentLine() {
|
|---|
| 239 | return this._position.line;
|
|---|
| 240 | }
|
|---|
| 241 | }
|
|---|
| 242 | exports.default = Buffer;
|
|---|
| 243 |
|
|---|
| 244 | //# sourceMappingURL=buffer.js.map
|
|---|