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