1 | 'use strict';
|
2 |
|
3 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
4 |
|
5 | function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
|
6 |
|
7 | function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
|
8 |
|
9 | var util = require('util');
|
10 | var _ = require('lodash');
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | function getLine(html, node) {
|
22 | if (!node) {
|
23 | return { line: 1, col: 1 };
|
24 | }
|
25 | var linesUntil = html.substring(0, node.startIndex).split('\n');
|
26 | return { line: linesUntil.length, col: linesUntil[linesUntil.length - 1].length + 1 };
|
27 | }
|
28 |
|
29 | function norm(n) {
|
30 | return n === undefined ? -1 : n;
|
31 | }
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | var RTCodeError = function (_Error) {
|
43 | _inherits(RTCodeError, _Error);
|
44 |
|
45 | function RTCodeError(message, startOffset, endOffset, line, column) {
|
46 | _classCallCheck(this, RTCodeError);
|
47 |
|
48 | var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(RTCodeError).call(this));
|
49 |
|
50 | Error.captureStackTrace(_this, RTCodeError);
|
51 | _this.name = 'RTCodeError';
|
52 | _this.message = message || '';
|
53 | _this.index = norm(startOffset);
|
54 | _this.startOffset = norm(startOffset);
|
55 | _this.endOffset = norm(endOffset);
|
56 | _this.line = norm(line);
|
57 | _this.column = norm(column);
|
58 | return _this;
|
59 | }
|
60 |
|
61 | return RTCodeError;
|
62 | }(Error);
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | RTCodeError.build = buildError;
|
70 | RTCodeError.norm = norm;
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | function buildFormat(context, node, msg, args) {
|
80 | return buildError(context, node, util.format.apply(this, [msg].concat(args)));
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | RTCodeError.buildFormat = _.rest(buildFormat, 3);
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | function buildError(context, node, msg) {
|
99 | var loc = getNodeLoc(context, node);
|
100 | return new RTCodeError(msg, loc.start, loc.end, loc.pos.line, loc.pos.col);
|
101 | }
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | function getNodeLoc(context, node) {
|
109 | var start = node.startIndex;
|
110 | var pos = getLine(context.html, node);
|
111 | var end = void 0;
|
112 | if (node.data) {
|
113 | end = start + node.data.length;
|
114 | } else if (node.next) {
|
115 |
|
116 | end = node.next.startIndex;
|
117 | } else {
|
118 | end = context.html.length;
|
119 | }
|
120 | return {
|
121 | pos: pos,
|
122 | start: start,
|
123 | end: end
|
124 | };
|
125 | }
|
126 |
|
127 | module.exports = {
|
128 | RTCodeError: RTCodeError,
|
129 | getNodeLoc: getNodeLoc
|
130 | }; |
\ | No newline at end of file |