1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
"use strict";
exports.__esModule = true;
var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var Whitespace = function () {
function Whitespace(tokens) {
(0, _classCallCheck3.default)(this, Whitespace);
this.tokens = tokens;
this.used = {};
}
Whitespace.prototype.getNewlinesBefore = function getNewlinesBefore(node) {
var startToken = void 0;
var endToken = void 0;
var tokens = this.tokens;
var index = this._findToken(function (token) {
return token.start - node.start;
}, 0, tokens.length);
if (index >= 0) {
while (index && node.start === tokens[index - 1].start) {
--index;
}startToken = tokens[index - 1];
endToken = tokens[index];
}
return this._getNewlinesBetween(startToken, endToken);
};
Whitespace.prototype.getNewlinesAfter = function getNewlinesAfter(node) {
var startToken = void 0;
var endToken = void 0;
var tokens = this.tokens;
var index = this._findToken(function (token) {
return token.end - node.end;
}, 0, tokens.length);
if (index >= 0) {
while (index && node.end === tokens[index - 1].end) {
--index;
}startToken = tokens[index];
endToken = tokens[index + 1];
if (endToken.type.label === ",") endToken = tokens[index + 2];
}
if (endToken && endToken.type.label === "eof") {
return 1;
} else {
return this._getNewlinesBetween(startToken, endToken);
}
};
Whitespace.prototype._getNewlinesBetween = function _getNewlinesBetween(startToken, endToken) {
if (!endToken || !endToken.loc) return 0;
var start = startToken ? startToken.loc.end.line : 1;
var end = endToken.loc.start.line;
var lines = 0;
for (var line = start; line < end; line++) {
if (typeof this.used[line] === "undefined") {
this.used[line] = true;
lines++;
}
}
return lines;
};
Whitespace.prototype._findToken = function _findToken(test, start, end) {
if (start >= end) return -1;
var middle = start + end >>> 1;
var match = test(this.tokens[middle]);
if (match < 0) {
return this._findToken(test, middle + 1, end);
} else if (match > 0) {
return this._findToken(test, start, middle);
} else if (match === 0) {
return middle;
}
return -1;
};
return Whitespace;
}();
exports.default = Whitespace;
module.exports = exports["default"];
|