code-frame.js
3.34 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//import lineNumbers from "line-numbers";
"use strict";
exports.__esModule = true;
// istanbul ignore next
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var _repeating = require("repeating");
var _repeating2 = _interopRequireDefault(_repeating);
var _jsTokens = require("js-tokens");
var _jsTokens2 = _interopRequireDefault(_jsTokens);
var _esutils = require("esutils");
var _esutils2 = _interopRequireDefault(_esutils);
var _chalk = require("chalk");
var _chalk2 = _interopRequireDefault(_chalk);
function lineNumbers(lines) {
return lines;
}
/**
* Chalk styles for token types.
*/
var defs = {
string: _chalk2["default"].red,
punctuator: _chalk2["default"].bold,
curly: _chalk2["default"].green,
parens: _chalk2["default"].blue.bold,
square: _chalk2["default"].yellow,
keyword: _chalk2["default"].cyan,
number: _chalk2["default"].magenta,
regex: _chalk2["default"].magenta,
comment: _chalk2["default"].grey,
invalid: _chalk2["default"].inverse
};
/**
* RegExp to test for newlines in terminal.
*/
var NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
/**
* Get the type of token, specifying punctuator type.
*/
function getTokenType(match) {
var token = _jsTokens2["default"].matchToToken(match);
if (token.type === "name" && _esutils2["default"].keyword.isReservedWordES6(token.value)) {
return "keyword";
}
if (token.type === "punctuator") {
switch (token.value) {
case "{":
case "}":
return "curly";
case "(":
case ")":
return "parens";
case "[":
case "]":
return "square";
}
}
return token.type;
}
/**
* Highlight `text`.
*/
function highlight(text) {
return text.replace(_jsTokens2["default"], function () {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var type = getTokenType(args);
var colorize = defs[type];
if (colorize) {
return args[0].split(NEWLINE).map(function (str) {
return colorize(str);
}).join("\n");
} else {
return args[0];
}
});
}
/**
* Create a code frame, adding line numbers, code highlighting, and pointing to a given position.
*/
exports["default"] = function (lines, lineNumber, colNumber) {
var opts = arguments.length <= 3 || arguments[3] === undefined ? {} : arguments[3];
colNumber = Math.max(colNumber, 0);
var highlighted = opts.highlightCode && _chalk2["default"].supportsColor;
if (highlighted) lines = highlight(lines);
lines = lines.split(NEWLINE);
var start = Math.max(lineNumber - 3, 0);
var end = Math.min(lines.length, lineNumber + 3);
if (!lineNumber && !colNumber) {
start = 0;
end = lines.length;
}
var frame = lineNumbers(lines.slice(start, end), {
start: start + 1,
before: " ",
after: " | ",
transform: function transform(params) {
if (params.number !== lineNumber) {
return;
}
if (colNumber) {
params.line += "\n" + params.before + _repeating2["default"](" ", params.width) + params.after + _repeating2["default"](" ", colNumber - 1) + "^";
}
params.before = params.before.replace(/^./, ">");
}
}).join("\n");
if (highlighted) {
return _chalk2["default"].reset(frame);
} else {
return frame;
}
};
module.exports = exports["default"];