@atlaspack/codeframe 2.13.3-canary.50 → 2.13.3-canary.500

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 CHANGED
@@ -1,5 +1,247 @@
1
1
  # @atlaspack/codeframe
2
2
 
3
+ ## 2.13.37
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [[`857962a`](https://github.com/atlassian-labs/atlaspack/commit/857962a352bb0aebaf74a8765e8c44d7e875a4e9)]:
8
+ - @atlaspack/types-internal@2.24.0
9
+
10
+ ## 2.13.36
11
+
12
+ ### Patch Changes
13
+
14
+ - Updated dependencies []:
15
+ - @atlaspack/types-internal@2.23.2
16
+
17
+ ## 2.13.35
18
+
19
+ ### Patch Changes
20
+
21
+ - Updated dependencies []:
22
+ - @atlaspack/types-internal@2.23.1
23
+
24
+ ## 2.13.34
25
+
26
+ ### Patch Changes
27
+
28
+ - Updated dependencies [[`e058f0e`](https://github.com/atlassian-labs/atlaspack/commit/e058f0e7a0423ba9373e85a7dbd5c1dd43b47916)]:
29
+ - @atlaspack/types-internal@2.23.0
30
+
31
+ ## 2.13.33
32
+
33
+ ### Patch Changes
34
+
35
+ - Updated dependencies []:
36
+ - @atlaspack/types-internal@2.22.8
37
+
38
+ ## 2.13.32
39
+
40
+ ### Patch Changes
41
+
42
+ - Updated dependencies []:
43
+ - @atlaspack/types-internal@2.22.7
44
+
45
+ ## 2.13.31
46
+
47
+ ### Patch Changes
48
+
49
+ - Updated dependencies []:
50
+ - @atlaspack/types-internal@2.22.6
51
+
52
+ ## 2.13.30
53
+
54
+ ### Patch Changes
55
+
56
+ - Updated dependencies []:
57
+ - @atlaspack/types-internal@2.22.5
58
+
59
+ ## 2.13.29
60
+
61
+ ### Patch Changes
62
+
63
+ - Updated dependencies []:
64
+ - @atlaspack/types-internal@2.22.4
65
+
66
+ ## 2.13.28
67
+
68
+ ### Patch Changes
69
+
70
+ - Updated dependencies [[`02cc8b3`](https://github.com/atlassian-labs/atlaspack/commit/02cc8b32c06ca6b51806b33f6f707ca06e55e957)]:
71
+ - @atlaspack/types-internal@2.22.3
72
+
73
+ ## 2.13.27
74
+
75
+ ### Patch Changes
76
+
77
+ - Updated dependencies []:
78
+ - @atlaspack/types-internal@2.22.2
79
+
80
+ ## 2.13.26
81
+
82
+ ### Patch Changes
83
+
84
+ - Updated dependencies []:
85
+ - @atlaspack/types-internal@2.22.1
86
+
87
+ ## 2.13.25
88
+
89
+ ### Patch Changes
90
+
91
+ - Updated dependencies [[`00fa643`](https://github.com/atlassian-labs/atlaspack/commit/00fa6433202bfd3311479f6314c9ec878a789f2c)]:
92
+ - @atlaspack/types-internal@2.22.0
93
+
94
+ ## 2.13.24
95
+
96
+ ### Patch Changes
97
+
98
+ - Updated dependencies []:
99
+ - @atlaspack/types-internal@2.21.3
100
+
101
+ ## 2.13.23
102
+
103
+ ### Patch Changes
104
+
105
+ - Updated dependencies []:
106
+ - @atlaspack/types-internal@2.21.2
107
+
108
+ ## 2.13.22
109
+
110
+ ### Patch Changes
111
+
112
+ - Updated dependencies []:
113
+ - @atlaspack/types-internal@2.21.1
114
+
115
+ ## 2.13.21
116
+
117
+ ### Patch Changes
118
+
119
+ - 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)]:
120
+ - @atlaspack/types-internal@2.21.0
121
+
122
+ ## 2.13.20
123
+
124
+ ### Patch Changes
125
+
126
+ - Updated dependencies []:
127
+ - @atlaspack/types-internal@2.20.8
128
+
129
+ ## 2.13.19
130
+
131
+ ### Patch Changes
132
+
133
+ - Updated dependencies []:
134
+ - @atlaspack/types-internal@2.20.7
135
+
136
+ ## 2.13.18
137
+
138
+ ### Patch Changes
139
+
140
+ - Updated dependencies []:
141
+ - @atlaspack/types-internal@2.20.6
142
+
143
+ ## 2.13.17
144
+
145
+ ### Patch Changes
146
+
147
+ - Updated dependencies [[`c372f3f`](https://github.com/atlassian-labs/atlaspack/commit/c372f3fd6fce8200d5cf47f41bc7895c6cbb5558)]:
148
+ - @atlaspack/types-internal@2.20.5
149
+
150
+ ## 2.13.16
151
+
152
+ ### Patch Changes
153
+
154
+ - Updated dependencies []:
155
+ - @atlaspack/types-internal@2.20.4
156
+
157
+ ## 2.13.15
158
+
159
+ ### Patch Changes
160
+
161
+ - Updated dependencies []:
162
+ - @atlaspack/types-internal@2.20.3
163
+
164
+ ## 2.13.14
165
+
166
+ ### Patch Changes
167
+
168
+ - Updated dependencies []:
169
+ - @atlaspack/types-internal@2.20.2
170
+
171
+ ## 2.13.13
172
+
173
+ ### Patch Changes
174
+
175
+ - [#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.
176
+
177
+ - Updated dependencies [[`0e7dd5e`](https://github.com/atlassian-labs/atlaspack/commit/0e7dd5ec6fbe05aa9e0bb5775a9d0975f206a922)]:
178
+ - @atlaspack/types-internal@2.20.1
179
+
180
+ ## 2.13.12
181
+
182
+ ### Patch Changes
183
+
184
+ - Updated dependencies [[`eedcbc4`](https://github.com/atlassian-labs/atlaspack/commit/eedcbc408fc1e86a2a8e25f1a41c57146d8529e1), [`f6532d7`](https://github.com/atlassian-labs/atlaspack/commit/f6532d7a4f7f007bd4e5e36af04dd466f0b9f572)]:
185
+ - @atlaspack/types-internal@2.20.0
186
+
187
+ ## 2.13.11
188
+
189
+ ### Patch Changes
190
+
191
+ - Updated dependencies []:
192
+ - @atlaspack/types-internal@2.19.5
193
+
194
+ ## 2.13.10
195
+
196
+ ### Patch Changes
197
+
198
+ - Updated dependencies []:
199
+ - @atlaspack/types-internal@2.19.4
200
+
201
+ ## 2.13.9
202
+
203
+ ### Patch Changes
204
+
205
+ - Updated dependencies []:
206
+ - @atlaspack/types-internal@2.19.3
207
+
208
+ ## 2.13.8
209
+
210
+ ### Patch Changes
211
+
212
+ - [#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
213
+
214
+ - Updated dependencies [[`ee040bb`](https://github.com/atlassian-labs/atlaspack/commit/ee040bb6428f29b57d892ddd8107e29077d08ffd)]:
215
+ - @atlaspack/types-internal@2.19.2
216
+
217
+ ## 2.13.7
218
+
219
+ ### Patch Changes
220
+
221
+ - Updated dependencies []:
222
+ - @atlaspack/types-internal@2.19.1
223
+
224
+ ## 2.13.6
225
+
226
+ ### Patch Changes
227
+
228
+ - Updated dependencies [[`7f5841c`](https://github.com/atlassian-labs/atlaspack/commit/7f5841c39df049f9546cccbeea2a7337e0337b45)]:
229
+ - @atlaspack/types-internal@2.19.0
230
+
231
+ ## 2.13.5
232
+
233
+ ### Patch Changes
234
+
235
+ - [#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
236
+
237
+ ## 2.13.4
238
+
239
+ ### Patch Changes
240
+
241
+ - [#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
242
+
243
+ - [#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
244
+
3
245
  ## 2.13.3
4
246
 
5
247
  ### 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
+ }