@atlaspack/codeframe 2.13.3-canary.51 → 2.13.3-canary.511
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +249 -0
- package/dist/codeframe.js +215 -0
- package/lib/codeframe.js +227 -36037
- package/lib/types/codeframe.d.ts +16 -0
- package/package.json +11 -8
- package/src/{codeframe.js → codeframe.ts} +22 -21
- package/test/{codeframe.test.js → codeframe.test.ts} +0 -1
- package/tsconfig.json +12 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/lib/codeframe.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,254 @@
|
|
|
1
1
|
# @atlaspack/codeframe
|
|
2
2
|
|
|
3
|
+
## 2.13.38
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies []:
|
|
8
|
+
- @atlaspack/types-internal@2.24.1
|
|
9
|
+
|
|
10
|
+
## 2.13.37
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- Updated dependencies [[`857962a`](https://github.com/atlassian-labs/atlaspack/commit/857962a352bb0aebaf74a8765e8c44d7e875a4e9)]:
|
|
15
|
+
- @atlaspack/types-internal@2.24.0
|
|
16
|
+
|
|
17
|
+
## 2.13.36
|
|
18
|
+
|
|
19
|
+
### Patch Changes
|
|
20
|
+
|
|
21
|
+
- Updated dependencies []:
|
|
22
|
+
- @atlaspack/types-internal@2.23.2
|
|
23
|
+
|
|
24
|
+
## 2.13.35
|
|
25
|
+
|
|
26
|
+
### Patch Changes
|
|
27
|
+
|
|
28
|
+
- Updated dependencies []:
|
|
29
|
+
- @atlaspack/types-internal@2.23.1
|
|
30
|
+
|
|
31
|
+
## 2.13.34
|
|
32
|
+
|
|
33
|
+
### Patch Changes
|
|
34
|
+
|
|
35
|
+
- Updated dependencies [[`e058f0e`](https://github.com/atlassian-labs/atlaspack/commit/e058f0e7a0423ba9373e85a7dbd5c1dd43b47916)]:
|
|
36
|
+
- @atlaspack/types-internal@2.23.0
|
|
37
|
+
|
|
38
|
+
## 2.13.33
|
|
39
|
+
|
|
40
|
+
### Patch Changes
|
|
41
|
+
|
|
42
|
+
- Updated dependencies []:
|
|
43
|
+
- @atlaspack/types-internal@2.22.8
|
|
44
|
+
|
|
45
|
+
## 2.13.32
|
|
46
|
+
|
|
47
|
+
### Patch Changes
|
|
48
|
+
|
|
49
|
+
- Updated dependencies []:
|
|
50
|
+
- @atlaspack/types-internal@2.22.7
|
|
51
|
+
|
|
52
|
+
## 2.13.31
|
|
53
|
+
|
|
54
|
+
### Patch Changes
|
|
55
|
+
|
|
56
|
+
- Updated dependencies []:
|
|
57
|
+
- @atlaspack/types-internal@2.22.6
|
|
58
|
+
|
|
59
|
+
## 2.13.30
|
|
60
|
+
|
|
61
|
+
### Patch Changes
|
|
62
|
+
|
|
63
|
+
- Updated dependencies []:
|
|
64
|
+
- @atlaspack/types-internal@2.22.5
|
|
65
|
+
|
|
66
|
+
## 2.13.29
|
|
67
|
+
|
|
68
|
+
### Patch Changes
|
|
69
|
+
|
|
70
|
+
- Updated dependencies []:
|
|
71
|
+
- @atlaspack/types-internal@2.22.4
|
|
72
|
+
|
|
73
|
+
## 2.13.28
|
|
74
|
+
|
|
75
|
+
### Patch Changes
|
|
76
|
+
|
|
77
|
+
- Updated dependencies [[`02cc8b3`](https://github.com/atlassian-labs/atlaspack/commit/02cc8b32c06ca6b51806b33f6f707ca06e55e957)]:
|
|
78
|
+
- @atlaspack/types-internal@2.22.3
|
|
79
|
+
|
|
80
|
+
## 2.13.27
|
|
81
|
+
|
|
82
|
+
### Patch Changes
|
|
83
|
+
|
|
84
|
+
- Updated dependencies []:
|
|
85
|
+
- @atlaspack/types-internal@2.22.2
|
|
86
|
+
|
|
87
|
+
## 2.13.26
|
|
88
|
+
|
|
89
|
+
### Patch Changes
|
|
90
|
+
|
|
91
|
+
- Updated dependencies []:
|
|
92
|
+
- @atlaspack/types-internal@2.22.1
|
|
93
|
+
|
|
94
|
+
## 2.13.25
|
|
95
|
+
|
|
96
|
+
### Patch Changes
|
|
97
|
+
|
|
98
|
+
- Updated dependencies [[`00fa643`](https://github.com/atlassian-labs/atlaspack/commit/00fa6433202bfd3311479f6314c9ec878a789f2c)]:
|
|
99
|
+
- @atlaspack/types-internal@2.22.0
|
|
100
|
+
|
|
101
|
+
## 2.13.24
|
|
102
|
+
|
|
103
|
+
### Patch Changes
|
|
104
|
+
|
|
105
|
+
- Updated dependencies []:
|
|
106
|
+
- @atlaspack/types-internal@2.21.3
|
|
107
|
+
|
|
108
|
+
## 2.13.23
|
|
109
|
+
|
|
110
|
+
### Patch Changes
|
|
111
|
+
|
|
112
|
+
- Updated dependencies []:
|
|
113
|
+
- @atlaspack/types-internal@2.21.2
|
|
114
|
+
|
|
115
|
+
## 2.13.22
|
|
116
|
+
|
|
117
|
+
### Patch Changes
|
|
118
|
+
|
|
119
|
+
- Updated dependencies []:
|
|
120
|
+
- @atlaspack/types-internal@2.21.1
|
|
121
|
+
|
|
122
|
+
## 2.13.21
|
|
123
|
+
|
|
124
|
+
### Patch Changes
|
|
125
|
+
|
|
126
|
+
- Updated dependencies [[`8eb84ee`](https://github.com/atlassian-labs/atlaspack/commit/8eb84ee61a42bfe87c58079b610802b07a6a13e4), [`73168c2`](https://github.com/atlassian-labs/atlaspack/commit/73168c275a5d9abff9907bcf536b340bca1ed5f0), [`617a318`](https://github.com/atlassian-labs/atlaspack/commit/617a318ddc9419b38360257353fec50b9051ee13)]:
|
|
127
|
+
- @atlaspack/types-internal@2.21.0
|
|
128
|
+
|
|
129
|
+
## 2.13.20
|
|
130
|
+
|
|
131
|
+
### Patch Changes
|
|
132
|
+
|
|
133
|
+
- Updated dependencies []:
|
|
134
|
+
- @atlaspack/types-internal@2.20.8
|
|
135
|
+
|
|
136
|
+
## 2.13.19
|
|
137
|
+
|
|
138
|
+
### Patch Changes
|
|
139
|
+
|
|
140
|
+
- Updated dependencies []:
|
|
141
|
+
- @atlaspack/types-internal@2.20.7
|
|
142
|
+
|
|
143
|
+
## 2.13.18
|
|
144
|
+
|
|
145
|
+
### Patch Changes
|
|
146
|
+
|
|
147
|
+
- Updated dependencies []:
|
|
148
|
+
- @atlaspack/types-internal@2.20.6
|
|
149
|
+
|
|
150
|
+
## 2.13.17
|
|
151
|
+
|
|
152
|
+
### Patch Changes
|
|
153
|
+
|
|
154
|
+
- Updated dependencies [[`c372f3f`](https://github.com/atlassian-labs/atlaspack/commit/c372f3fd6fce8200d5cf47f41bc7895c6cbb5558)]:
|
|
155
|
+
- @atlaspack/types-internal@2.20.5
|
|
156
|
+
|
|
157
|
+
## 2.13.16
|
|
158
|
+
|
|
159
|
+
### Patch Changes
|
|
160
|
+
|
|
161
|
+
- Updated dependencies []:
|
|
162
|
+
- @atlaspack/types-internal@2.20.4
|
|
163
|
+
|
|
164
|
+
## 2.13.15
|
|
165
|
+
|
|
166
|
+
### Patch Changes
|
|
167
|
+
|
|
168
|
+
- Updated dependencies []:
|
|
169
|
+
- @atlaspack/types-internal@2.20.3
|
|
170
|
+
|
|
171
|
+
## 2.13.14
|
|
172
|
+
|
|
173
|
+
### Patch Changes
|
|
174
|
+
|
|
175
|
+
- Updated dependencies []:
|
|
176
|
+
- @atlaspack/types-internal@2.20.2
|
|
177
|
+
|
|
178
|
+
## 2.13.13
|
|
179
|
+
|
|
180
|
+
### Patch Changes
|
|
181
|
+
|
|
182
|
+
- [#785](https://github.com/atlassian-labs/atlaspack/pull/785) [`0e7dd5e`](https://github.com/atlassian-labs/atlaspack/commit/0e7dd5ec6fbe05aa9e0bb5775a9d0975f206a922) Thanks [@matt-koko](https://github.com/matt-koko)! - We need to re-publish every package in Atlaspack with the corrected types field.
|
|
183
|
+
|
|
184
|
+
- Updated dependencies [[`0e7dd5e`](https://github.com/atlassian-labs/atlaspack/commit/0e7dd5ec6fbe05aa9e0bb5775a9d0975f206a922)]:
|
|
185
|
+
- @atlaspack/types-internal@2.20.1
|
|
186
|
+
|
|
187
|
+
## 2.13.12
|
|
188
|
+
|
|
189
|
+
### Patch Changes
|
|
190
|
+
|
|
191
|
+
- Updated dependencies [[`eedcbc4`](https://github.com/atlassian-labs/atlaspack/commit/eedcbc408fc1e86a2a8e25f1a41c57146d8529e1), [`f6532d7`](https://github.com/atlassian-labs/atlaspack/commit/f6532d7a4f7f007bd4e5e36af04dd466f0b9f572)]:
|
|
192
|
+
- @atlaspack/types-internal@2.20.0
|
|
193
|
+
|
|
194
|
+
## 2.13.11
|
|
195
|
+
|
|
196
|
+
### Patch Changes
|
|
197
|
+
|
|
198
|
+
- Updated dependencies []:
|
|
199
|
+
- @atlaspack/types-internal@2.19.5
|
|
200
|
+
|
|
201
|
+
## 2.13.10
|
|
202
|
+
|
|
203
|
+
### Patch Changes
|
|
204
|
+
|
|
205
|
+
- Updated dependencies []:
|
|
206
|
+
- @atlaspack/types-internal@2.19.4
|
|
207
|
+
|
|
208
|
+
## 2.13.9
|
|
209
|
+
|
|
210
|
+
### Patch Changes
|
|
211
|
+
|
|
212
|
+
- Updated dependencies []:
|
|
213
|
+
- @atlaspack/types-internal@2.19.3
|
|
214
|
+
|
|
215
|
+
## 2.13.8
|
|
216
|
+
|
|
217
|
+
### Patch Changes
|
|
218
|
+
|
|
219
|
+
- [#742](https://github.com/atlassian-labs/atlaspack/pull/742) [`ee040bb`](https://github.com/atlassian-labs/atlaspack/commit/ee040bb6428f29b57d892ddd8107e29077d08ffd) Thanks [@yamadapc](https://github.com/yamadapc)! - Internal changes and bug fixes to environmentDeduplication flag
|
|
220
|
+
|
|
221
|
+
- Updated dependencies [[`ee040bb`](https://github.com/atlassian-labs/atlaspack/commit/ee040bb6428f29b57d892ddd8107e29077d08ffd)]:
|
|
222
|
+
- @atlaspack/types-internal@2.19.2
|
|
223
|
+
|
|
224
|
+
## 2.13.7
|
|
225
|
+
|
|
226
|
+
### Patch Changes
|
|
227
|
+
|
|
228
|
+
- Updated dependencies []:
|
|
229
|
+
- @atlaspack/types-internal@2.19.1
|
|
230
|
+
|
|
231
|
+
## 2.13.6
|
|
232
|
+
|
|
233
|
+
### Patch Changes
|
|
234
|
+
|
|
235
|
+
- Updated dependencies [[`7f5841c`](https://github.com/atlassian-labs/atlaspack/commit/7f5841c39df049f9546cccbeea2a7337e0337b45)]:
|
|
236
|
+
- @atlaspack/types-internal@2.19.0
|
|
237
|
+
|
|
238
|
+
## 2.13.5
|
|
239
|
+
|
|
240
|
+
### Patch Changes
|
|
241
|
+
|
|
242
|
+
- [#720](https://github.com/atlassian-labs/atlaspack/pull/720) [`d2fd849`](https://github.com/atlassian-labs/atlaspack/commit/d2fd849770fe6305e9c694bd97b1bd905abd9d94) Thanks [@alshdavid](https://github.com/alshdavid)! - Migrate to TypeScript
|
|
243
|
+
|
|
244
|
+
## 2.13.4
|
|
245
|
+
|
|
246
|
+
### Patch Changes
|
|
247
|
+
|
|
248
|
+
- [#645](https://github.com/atlassian-labs/atlaspack/pull/645) [`de23e0c`](https://github.com/atlassian-labs/atlaspack/commit/de23e0ce49d5504fe3947ac26640a3d951087da3) Thanks [@alshdavid](https://github.com/alshdavid)! - Updated build system and added some extra test-specific code
|
|
249
|
+
|
|
250
|
+
- [#682](https://github.com/atlassian-labs/atlaspack/pull/682) [`a5ed1b4`](https://github.com/atlassian-labs/atlaspack/commit/a5ed1b414498560f393ff491af4da25b6e8dde56) Thanks [@alshdavid](https://github.com/alshdavid)! - Updating build system
|
|
251
|
+
|
|
3
252
|
## 2.13.3
|
|
4
253
|
|
|
5
254
|
### Patch Changes
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.default = codeFrame;
|
|
7
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
+
// @ts-expect-error emphasize is not typed
|
|
9
|
+
const emphasize_1 = __importDefault(require("emphasize"));
|
|
10
|
+
const string_width_1 = __importDefault(require("string-width"));
|
|
11
|
+
// @ts-expect-error slice-ansi is not typed
|
|
12
|
+
const slice_ansi_1 = __importDefault(require("slice-ansi"));
|
|
13
|
+
const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
|
|
14
|
+
const TAB_REPLACE_REGEX = /\t/g;
|
|
15
|
+
const TAB_REPLACEMENT = ' ';
|
|
16
|
+
const DEFAULT_TERMINAL_WIDTH = 80;
|
|
17
|
+
const highlightSyntax = (txt, lang) => {
|
|
18
|
+
if (lang) {
|
|
19
|
+
try {
|
|
20
|
+
return emphasize_1.default.highlight(lang, txt).value;
|
|
21
|
+
}
|
|
22
|
+
catch (e) {
|
|
23
|
+
// fallback for unknown languages...
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return emphasize_1.default.highlightAuto(txt).value;
|
|
27
|
+
};
|
|
28
|
+
function codeFrame(code, highlights, inputOpts = {}) {
|
|
29
|
+
if (highlights.length < 1)
|
|
30
|
+
return '';
|
|
31
|
+
let opts = {
|
|
32
|
+
useColor: !!inputOpts.useColor,
|
|
33
|
+
syntaxHighlighting: !!inputOpts.syntaxHighlighting,
|
|
34
|
+
language: inputOpts.language,
|
|
35
|
+
maxLines: inputOpts.maxLines ?? 12,
|
|
36
|
+
terminalWidth: inputOpts.terminalWidth || DEFAULT_TERMINAL_WIDTH,
|
|
37
|
+
padding: inputOpts.padding || {
|
|
38
|
+
before: 1,
|
|
39
|
+
after: 2,
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
// Highlights messages and prefixes when colors are enabled
|
|
43
|
+
const highlighter = (s, bold) => {
|
|
44
|
+
if (opts.useColor) {
|
|
45
|
+
let redString = chalk_1.default.red(s);
|
|
46
|
+
return bold ? chalk_1.default.bold(redString) : redString;
|
|
47
|
+
}
|
|
48
|
+
return s;
|
|
49
|
+
};
|
|
50
|
+
// Prefix lines with the line number
|
|
51
|
+
const lineNumberPrefixer = (params) => {
|
|
52
|
+
let { lineNumber, lineNumberLength, isHighlighted } = params;
|
|
53
|
+
return `${isHighlighted ? highlighter('>') : ' '} ${lineNumber
|
|
54
|
+
? lineNumber.padStart(lineNumberLength, ' ')
|
|
55
|
+
: ' '.repeat(lineNumberLength)} | `;
|
|
56
|
+
};
|
|
57
|
+
// Make columns/lines start at 1
|
|
58
|
+
let originalHighlights = highlights;
|
|
59
|
+
highlights = highlights.map((h) => {
|
|
60
|
+
return {
|
|
61
|
+
start: {
|
|
62
|
+
column: h.start.column - 1,
|
|
63
|
+
line: h.start.line - 1,
|
|
64
|
+
},
|
|
65
|
+
end: {
|
|
66
|
+
column: h.end.column - 1,
|
|
67
|
+
line: h.end.line - 1,
|
|
68
|
+
},
|
|
69
|
+
message: h.message,
|
|
70
|
+
};
|
|
71
|
+
});
|
|
72
|
+
// Find first and last highlight
|
|
73
|
+
let firstHighlight = highlights.length > 1
|
|
74
|
+
? highlights.sort((a, b) => a.start.line - b.start.line)[0]
|
|
75
|
+
: highlights[0];
|
|
76
|
+
let lastHighlight = highlights.length > 1
|
|
77
|
+
? highlights.sort((a, b) => b.end.line - a.end.line)[0]
|
|
78
|
+
: highlights[0];
|
|
79
|
+
// Calculate first and last line index of codeframe
|
|
80
|
+
let startLine = firstHighlight.start.line - opts.padding.before;
|
|
81
|
+
startLine = startLine < 0 ? 0 : startLine;
|
|
82
|
+
let endLineIndex = lastHighlight.end.line + opts.padding.after;
|
|
83
|
+
let tail;
|
|
84
|
+
if (endLineIndex - startLine > opts.maxLines) {
|
|
85
|
+
let maxLine = startLine + opts.maxLines - 1;
|
|
86
|
+
highlights = highlights.filter((h) => h.start.line < maxLine);
|
|
87
|
+
lastHighlight = highlights[0];
|
|
88
|
+
endLineIndex = Math.min(maxLine, lastHighlight.end.line + opts.padding.after);
|
|
89
|
+
tail = originalHighlights.filter((h) => h.start.line > endLineIndex);
|
|
90
|
+
}
|
|
91
|
+
let lineNumberLength = (endLineIndex + 1).toString(10).length;
|
|
92
|
+
// Split input into lines and highlight syntax
|
|
93
|
+
let lines = code.split(NEWLINE);
|
|
94
|
+
let syntaxHighlightedLines = (opts.syntaxHighlighting ? highlightSyntax(code, opts.language) : code)
|
|
95
|
+
.replace(TAB_REPLACE_REGEX, TAB_REPLACEMENT)
|
|
96
|
+
.split(NEWLINE);
|
|
97
|
+
// Loop over all lines and create codeframe
|
|
98
|
+
let resultLines = [];
|
|
99
|
+
for (let currentLineIndex = startLine; currentLineIndex < syntaxHighlightedLines.length; currentLineIndex++) {
|
|
100
|
+
if (currentLineIndex > endLineIndex)
|
|
101
|
+
break;
|
|
102
|
+
if (currentLineIndex > syntaxHighlightedLines.length - 1)
|
|
103
|
+
break;
|
|
104
|
+
// Find highlights that need to get rendered on the current line
|
|
105
|
+
let lineHighlights = highlights
|
|
106
|
+
.filter((highlight) => highlight.start.line <= currentLineIndex &&
|
|
107
|
+
highlight.end.line >= currentLineIndex)
|
|
108
|
+
.sort((a, b) => (a.start.line < currentLineIndex ? 0 : a.start.column) -
|
|
109
|
+
(b.start.line < currentLineIndex ? 0 : b.start.column));
|
|
110
|
+
// Check if this line has a full line highlight
|
|
111
|
+
let isWholeLine = lineHighlights.length &&
|
|
112
|
+
!!lineHighlights.find((h) => h.start.line < currentLineIndex && h.end.line > currentLineIndex);
|
|
113
|
+
let lineLengthLimit = opts.terminalWidth > lineNumberLength + 7
|
|
114
|
+
? opts.terminalWidth - (lineNumberLength + 5)
|
|
115
|
+
: 10;
|
|
116
|
+
// Split the line into line parts that will fit the provided terminal width
|
|
117
|
+
let colOffset = 0;
|
|
118
|
+
let lineEndCol = lineLengthLimit;
|
|
119
|
+
let syntaxHighlightedLine = syntaxHighlightedLines[currentLineIndex];
|
|
120
|
+
if ((0, string_width_1.default)(syntaxHighlightedLine) > lineLengthLimit) {
|
|
121
|
+
if (lineHighlights.length > 0) {
|
|
122
|
+
if (lineHighlights[0].start.line === currentLineIndex) {
|
|
123
|
+
colOffset = lineHighlights[0].start.column - 5;
|
|
124
|
+
}
|
|
125
|
+
else if (lineHighlights[0].end.line === currentLineIndex) {
|
|
126
|
+
colOffset = lineHighlights[0].end.column - 5;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
colOffset = colOffset > 0 ? colOffset : 0;
|
|
130
|
+
lineEndCol = colOffset + lineLengthLimit;
|
|
131
|
+
syntaxHighlightedLine = (0, slice_ansi_1.default)(syntaxHighlightedLine, colOffset, lineEndCol);
|
|
132
|
+
}
|
|
133
|
+
// Write the syntax highlighted line part
|
|
134
|
+
resultLines.push(lineNumberPrefixer({
|
|
135
|
+
lineNumber: (currentLineIndex + 1).toString(10),
|
|
136
|
+
lineNumberLength,
|
|
137
|
+
isHighlighted: lineHighlights.length > 0,
|
|
138
|
+
}) + syntaxHighlightedLine);
|
|
139
|
+
let lineWidth = (0, string_width_1.default)(syntaxHighlightedLine);
|
|
140
|
+
let highlightLine = '';
|
|
141
|
+
if (isWholeLine) {
|
|
142
|
+
highlightLine = highlighter('^'.repeat(lineWidth));
|
|
143
|
+
}
|
|
144
|
+
else if (lineHighlights.length > 0) {
|
|
145
|
+
let lastCol = 0;
|
|
146
|
+
let highlight = null;
|
|
147
|
+
let highlightHasEnded = false;
|
|
148
|
+
for (let highlightIndex = 0; highlightIndex < lineHighlights.length; highlightIndex++) {
|
|
149
|
+
// Set highlight to current highlight
|
|
150
|
+
highlight = lineHighlights[highlightIndex];
|
|
151
|
+
highlightHasEnded = false;
|
|
152
|
+
// Calculate the startColumn and get the real width by doing a substring of the original
|
|
153
|
+
// line and replacing tabs with our tab replacement to support tab handling
|
|
154
|
+
let startCol = 0;
|
|
155
|
+
if (highlight.start.line === currentLineIndex &&
|
|
156
|
+
highlight.start.column > colOffset) {
|
|
157
|
+
startCol = lines[currentLineIndex]
|
|
158
|
+
.substring(colOffset, highlight.start.column)
|
|
159
|
+
.replace(TAB_REPLACE_REGEX, TAB_REPLACEMENT).length;
|
|
160
|
+
}
|
|
161
|
+
// Calculate the endColumn and get the real width by doing a substring of the original
|
|
162
|
+
// line and replacing tabs with our tab replacement to support tab handling
|
|
163
|
+
let endCol = lineWidth - 1;
|
|
164
|
+
if (highlight.end.line === currentLineIndex) {
|
|
165
|
+
endCol = lines[currentLineIndex]
|
|
166
|
+
.substring(colOffset, highlight.end.column)
|
|
167
|
+
.replace(TAB_REPLACE_REGEX, TAB_REPLACEMENT).length;
|
|
168
|
+
// If the endCol is too big for this line part, trim it so we can handle it in the next one
|
|
169
|
+
if (endCol > lineWidth) {
|
|
170
|
+
endCol = lineWidth - 1;
|
|
171
|
+
}
|
|
172
|
+
highlightHasEnded = true;
|
|
173
|
+
}
|
|
174
|
+
// If endcol is smaller than lastCol it overlaps with another highlight and is no longer visible, we can skip those
|
|
175
|
+
if (endCol >= lastCol) {
|
|
176
|
+
let characters = endCol - startCol + 1;
|
|
177
|
+
if (startCol > lastCol) {
|
|
178
|
+
// startCol is before lastCol, so add spaces as padding before the highlight indicators
|
|
179
|
+
highlightLine += ' '.repeat(startCol - lastCol);
|
|
180
|
+
}
|
|
181
|
+
else if (lastCol > startCol) {
|
|
182
|
+
// If last column is larger than the start, there's overlap in highlights
|
|
183
|
+
// This line adjusts the characters count to ensure we don't add too many characters
|
|
184
|
+
characters += startCol - lastCol;
|
|
185
|
+
}
|
|
186
|
+
// Don't crash (and swallow the original message) if the diagnostic is malformed (end is before start).
|
|
187
|
+
characters = Math.max(1, characters);
|
|
188
|
+
// Append the highlight indicators
|
|
189
|
+
highlightLine += highlighter('^'.repeat(characters));
|
|
190
|
+
// Set the lastCol equal to character count between start of line part and highlight end-column
|
|
191
|
+
lastCol = endCol + 1;
|
|
192
|
+
}
|
|
193
|
+
// There's no point in processing more highlights if we reached the end of the line
|
|
194
|
+
if (endCol >= lineEndCol - 1) {
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
// Append the highlight message if the current highlights ends on this line part
|
|
199
|
+
if (highlight && highlight.message && highlightHasEnded) {
|
|
200
|
+
highlightLine += ' ' + highlighter(highlight.message, true);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if (highlightLine) {
|
|
204
|
+
resultLines.push(lineNumberPrefixer({
|
|
205
|
+
lineNumberLength,
|
|
206
|
+
isHighlighted: true,
|
|
207
|
+
}) + highlightLine);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
let result = resultLines.join('\n');
|
|
211
|
+
if (tail && tail.length > 0) {
|
|
212
|
+
result += '\n\n' + codeFrame(code, tail, inputOpts);
|
|
213
|
+
}
|
|
214
|
+
return result;
|
|
215
|
+
}
|