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