UNPKG

3.62 kBJavaScriptView Raw
1'use strict';
2
3function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
4
5function _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
7function _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
9var util = require('util');
10var _ = require('lodash');
11
12/**
13 * @typedef {{line: number, col: number}} Pos
14 */
15
16/**
17 * @param {string} html
18 * @param node
19 * @return {Pos}
20 */
21function 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
29function norm(n) {
30 return n === undefined ? -1 : n;
31}
32
33/**
34 * @param {string} message
35 * @param {number=} startOffset
36 * @param {number=} endOffset
37 * @param {number=} line
38 * @param {number=} column
39 * @constructor
40 */
41
42var 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 * @type {buildError}
66 */
67
68
69RTCodeError.build = buildError;
70RTCodeError.norm = norm;
71
72/**
73 * @param {*} context
74 * @param {*} node
75 * @param {string} msg
76 * @param args
77 * @return {RTCodeError}
78 */
79function buildFormat(context, node, msg, args) {
80 return buildError(context, node, util.format.apply(this, [msg].concat(args)));
81}
82
83/**
84 * @param {*} context
85 * @param {*} node
86 * @param {string} msg
87 * @param {Array.<string>} args
88 * @return {RTCodeError}
89 */
90RTCodeError.buildFormat = _.rest(buildFormat, 3);
91
92/**
93 * @param {*} context
94 * @param {*} node
95 * @param {string} msg
96 * @return {RTCodeError}
97 */
98function 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 * @param context
105 * @param node
106 * @return {{pos:Pos, start:number, end:number}}
107 */
108function 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 // eslint-disable-line
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
127module.exports = {
128 RTCodeError: RTCodeError,
129 getNodeLoc: getNodeLoc
130};
\No newline at end of file