123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = void 0;
- const SPACES_RE = /^[ \t]+$/;
- class Buffer {
- constructor(map) {
- this._map = null;
- this._buf = "";
- this._last = 0;
- this._queue = [];
- this._position = {
- line: 1,
- column: 0
- };
- this._sourcePosition = {
- identifierName: null,
- line: null,
- column: null,
- filename: null
- };
- this._disallowedPop = null;
- this._map = map;
- }
- get() {
- this._flush();
- const map = this._map;
- const result = {
- code: this._buf.trimRight(),
- map: null,
- rawMappings: map == null ? void 0 : map.getRawMappings()
- };
- if (map) {
- Object.defineProperty(result, "map", {
- configurable: true,
- enumerable: true,
- get() {
- return this.map = map.get();
- },
- set(value) {
- Object.defineProperty(this, "map", {
- value,
- writable: true
- });
- }
- });
- }
- return result;
- }
- append(str) {
- this._flush();
- const {
- line,
- column,
- filename,
- identifierName,
- force
- } = this._sourcePosition;
- this._append(str, line, column, identifierName, filename, force);
- }
- queue(str) {
- if (str === "\n") {
- while (this._queue.length > 0 && SPACES_RE.test(this._queue[0][0])) {
- this._queue.shift();
- }
- }
- const {
- line,
- column,
- filename,
- identifierName,
- force
- } = this._sourcePosition;
- this._queue.unshift([str, line, column, identifierName, filename, force]);
- }
- _flush() {
- let item;
- while (item = this._queue.pop()) {
- this._append(...item);
- }
- }
- _append(str, line, column, identifierName, filename, force) {
- this._buf += str;
- this._last = str.charCodeAt(str.length - 1);
- let i = str.indexOf("\n");
- let last = 0;
- if (i !== 0) {
- this._mark(line, column, identifierName, filename, force);
- }
- while (i !== -1) {
- this._position.line++;
- this._position.column = 0;
- last = i + 1;
- if (last < str.length) {
- this._mark(++line, 0, identifierName, filename, force);
- }
- i = str.indexOf("\n", last);
- }
- this._position.column += str.length - last;
- }
- _mark(line, column, identifierName, filename, force) {
- var _this$_map;
- (_this$_map = this._map) == null ? void 0 : _this$_map.mark(this._position.line, this._position.column, line, column, identifierName, filename, force);
- }
- removeTrailingNewline() {
- if (this._queue.length > 0 && this._queue[0][0] === "\n") {
- this._queue.shift();
- }
- }
- removeLastSemicolon() {
- if (this._queue.length > 0 && this._queue[0][0] === ";") {
- this._queue.shift();
- }
- }
- getLastChar() {
- let last;
- if (this._queue.length > 0) {
- const str = this._queue[0][0];
- last = str.charCodeAt(0);
- } else {
- last = this._last;
- }
- return last;
- }
- endsWithCharAndNewline() {
- const queue = this._queue;
- if (queue.length > 0) {
- const last = queue[0][0];
- const lastCp = last.charCodeAt(0);
- if (lastCp !== 10) return;
- if (queue.length > 1) {
- const secondLast = queue[1][0];
- return secondLast.charCodeAt(0);
- } else {
- return this._last;
- }
- }
- }
- hasContent() {
- return this._queue.length > 0 || !!this._last;
- }
- exactSource(loc, cb) {
- this.source("start", loc, true);
- cb();
- this.source("end", loc);
- this._disallowPop("start", loc);
- }
- source(prop, loc, force) {
- if (prop && !loc) return;
- this._normalizePosition(prop, loc, this._sourcePosition, force);
- }
- withSource(prop, loc, cb) {
- if (!this._map) return cb();
- const originalLine = this._sourcePosition.line;
- const originalColumn = this._sourcePosition.column;
- const originalFilename = this._sourcePosition.filename;
- const originalIdentifierName = this._sourcePosition.identifierName;
- this.source(prop, loc);
- cb();
- 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)) {
- this._sourcePosition.line = originalLine;
- this._sourcePosition.column = originalColumn;
- this._sourcePosition.filename = originalFilename;
- this._sourcePosition.identifierName = originalIdentifierName;
- this._sourcePosition.force = false;
- this._disallowedPop = null;
- }
- }
- _disallowPop(prop, loc) {
- if (prop && !loc) return;
- this._disallowedPop = this._normalizePosition(prop, loc);
- }
- _normalizePosition(prop, loc, targetObj, force) {
- const pos = loc ? loc[prop] : null;
- if (targetObj === undefined) {
- targetObj = {
- identifierName: null,
- line: null,
- column: null,
- filename: null,
- force: false
- };
- }
- const origLine = targetObj.line;
- const origColumn = targetObj.column;
- const origFilename = targetObj.filename;
- targetObj.identifierName = prop === "start" && (loc == null ? void 0 : loc.identifierName) || null;
- targetObj.line = pos == null ? void 0 : pos.line;
- targetObj.column = pos == null ? void 0 : pos.column;
- targetObj.filename = loc == null ? void 0 : loc.filename;
- if (force || targetObj.line !== origLine || targetObj.column !== origColumn || targetObj.filename !== origFilename) {
- targetObj.force = force;
- }
- return targetObj;
- }
- getCurrentColumn() {
- const extra = this._queue.reduce((acc, item) => item[0] + acc, "");
- const lastIndex = extra.lastIndexOf("\n");
- return lastIndex === -1 ? this._position.column + extra.length : extra.length - 1 - lastIndex;
- }
- getCurrentLine() {
- const extra = this._queue.reduce((acc, item) => item[0] + acc, "");
- let count = 0;
- for (let i = 0; i < extra.length; i++) {
- if (extra[i] === "\n") count++;
- }
- return this._position.line + count;
- }
- }
- exports.default = Buffer;
|