1 | "use strict";
|
2 |
|
3 | exports.__esModule = true;
|
4 |
|
5 | var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
|
6 |
|
7 | var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
|
8 |
|
9 | var _trimRight = require("trim-right");
|
10 |
|
11 | var _trimRight2 = _interopRequireDefault(_trimRight);
|
12 |
|
13 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
14 |
|
15 | var SPACES_RE = /^[ \t]+$/;
|
16 |
|
17 | var Buffer = function () {
|
18 | function Buffer(map) {
|
19 | (0, _classCallCheck3.default)(this, Buffer);
|
20 | this._map = null;
|
21 | this._buf = [];
|
22 | this._last = "";
|
23 | this._queue = [];
|
24 | this._position = {
|
25 | line: 1,
|
26 | column: 0
|
27 | };
|
28 | this._sourcePosition = {
|
29 | identifierName: null,
|
30 | line: null,
|
31 | column: null,
|
32 | filename: null
|
33 | };
|
34 |
|
35 | this._map = map;
|
36 | }
|
37 |
|
38 | Buffer.prototype.get = function get() {
|
39 | this._flush();
|
40 |
|
41 | var map = this._map;
|
42 | var result = {
|
43 | code: (0, _trimRight2.default)(this._buf.join("")),
|
44 | map: null,
|
45 | rawMappings: map && map.getRawMappings()
|
46 | };
|
47 |
|
48 | if (map) {
|
49 | Object.defineProperty(result, "map", {
|
50 | configurable: true,
|
51 | enumerable: true,
|
52 | get: function get() {
|
53 | return this.map = map.get();
|
54 | },
|
55 | set: function set(value) {
|
56 | Object.defineProperty(this, "map", { value: value, writable: true });
|
57 | }
|
58 | });
|
59 | }
|
60 |
|
61 | return result;
|
62 | };
|
63 |
|
64 | Buffer.prototype.append = function append(str) {
|
65 | this._flush();
|
66 | var _sourcePosition = this._sourcePosition,
|
67 | line = _sourcePosition.line,
|
68 | column = _sourcePosition.column,
|
69 | filename = _sourcePosition.filename,
|
70 | identifierName = _sourcePosition.identifierName;
|
71 |
|
72 | this._append(str, line, column, identifierName, filename);
|
73 | };
|
74 |
|
75 | Buffer.prototype.queue = function queue(str) {
|
76 | if (str === "\n") while (this._queue.length > 0 && SPACES_RE.test(this._queue[0][0])) {
|
77 | this._queue.shift();
|
78 | }var _sourcePosition2 = this._sourcePosition,
|
79 | line = _sourcePosition2.line,
|
80 | column = _sourcePosition2.column,
|
81 | filename = _sourcePosition2.filename,
|
82 | identifierName = _sourcePosition2.identifierName;
|
83 |
|
84 | this._queue.unshift([str, line, column, identifierName, filename]);
|
85 | };
|
86 |
|
87 | Buffer.prototype._flush = function _flush() {
|
88 | var item = void 0;
|
89 | while (item = this._queue.pop()) {
|
90 | this._append.apply(this, item);
|
91 | }
|
92 | };
|
93 |
|
94 | Buffer.prototype._append = function _append(str, line, column, identifierName, filename) {
|
95 | if (this._map && str[0] !== "\n") {
|
96 | this._map.mark(this._position.line, this._position.column, line, column, identifierName, filename);
|
97 | }
|
98 |
|
99 | this._buf.push(str);
|
100 | this._last = str[str.length - 1];
|
101 |
|
102 | for (var i = 0; i < str.length; i++) {
|
103 | if (str[i] === "\n") {
|
104 | this._position.line++;
|
105 | this._position.column = 0;
|
106 | } else {
|
107 | this._position.column++;
|
108 | }
|
109 | }
|
110 | };
|
111 |
|
112 | Buffer.prototype.removeTrailingNewline = function removeTrailingNewline() {
|
113 | if (this._queue.length > 0 && this._queue[0][0] === "\n") this._queue.shift();
|
114 | };
|
115 |
|
116 | Buffer.prototype.removeLastSemicolon = function removeLastSemicolon() {
|
117 | if (this._queue.length > 0 && this._queue[0][0] === ";") this._queue.shift();
|
118 | };
|
119 |
|
120 | Buffer.prototype.endsWith = function endsWith(suffix) {
|
121 | if (suffix.length === 1) {
|
122 | var last = void 0;
|
123 | if (this._queue.length > 0) {
|
124 | var str = this._queue[0][0];
|
125 | last = str[str.length - 1];
|
126 | } else {
|
127 | last = this._last;
|
128 | }
|
129 |
|
130 | return last === suffix;
|
131 | }
|
132 |
|
133 | var end = this._last + this._queue.reduce(function (acc, item) {
|
134 | return item[0] + acc;
|
135 | }, "");
|
136 | if (suffix.length <= end.length) {
|
137 | return end.slice(-suffix.length) === suffix;
|
138 | }
|
139 |
|
140 | return false;
|
141 | };
|
142 |
|
143 | Buffer.prototype.hasContent = function hasContent() {
|
144 | return this._queue.length > 0 || !!this._last;
|
145 | };
|
146 |
|
147 | Buffer.prototype.source = function source(prop, loc) {
|
148 | if (prop && !loc) return;
|
149 |
|
150 | var pos = loc ? loc[prop] : null;
|
151 |
|
152 | this._sourcePosition.identifierName = loc && loc.identifierName || null;
|
153 | this._sourcePosition.line = pos ? pos.line : null;
|
154 | this._sourcePosition.column = pos ? pos.column : null;
|
155 | this._sourcePosition.filename = loc && loc.filename || null;
|
156 | };
|
157 |
|
158 | Buffer.prototype.withSource = function withSource(prop, loc, cb) {
|
159 | if (!this._map) return cb();
|
160 |
|
161 | var originalLine = this._sourcePosition.line;
|
162 | var originalColumn = this._sourcePosition.column;
|
163 | var originalFilename = this._sourcePosition.filename;
|
164 | var originalIdentifierName = this._sourcePosition.identifierName;
|
165 |
|
166 | this.source(prop, loc);
|
167 |
|
168 | cb();
|
169 |
|
170 | this._sourcePosition.line = originalLine;
|
171 | this._sourcePosition.column = originalColumn;
|
172 | this._sourcePosition.filename = originalFilename;
|
173 | this._sourcePosition.identifierName = originalIdentifierName;
|
174 | };
|
175 |
|
176 | Buffer.prototype.getCurrentColumn = function getCurrentColumn() {
|
177 | var extra = this._queue.reduce(function (acc, item) {
|
178 | return item[0] + acc;
|
179 | }, "");
|
180 | var lastIndex = extra.lastIndexOf("\n");
|
181 |
|
182 | return lastIndex === -1 ? this._position.column + extra.length : extra.length - 1 - lastIndex;
|
183 | };
|
184 |
|
185 | Buffer.prototype.getCurrentLine = function getCurrentLine() {
|
186 | var extra = this._queue.reduce(function (acc, item) {
|
187 | return item[0] + acc;
|
188 | }, "");
|
189 |
|
190 | var count = 0;
|
191 | for (var i = 0; i < extra.length; i++) {
|
192 | if (extra[i] === "\n") count++;
|
193 | }
|
194 |
|
195 | return this._position.line + count;
|
196 | };
|
197 |
|
198 | return Buffer;
|
199 | }();
|
200 |
|
201 | exports.default = Buffer;
|
202 | module.exports = exports["default"]; |
\ | No newline at end of file |