@parcel/codeframe 2.0.0-beta.3.1 → 2.0.0-dev.1514
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/lib/codeframe.js +35983 -173
- package/lib/codeframe.js.map +1 -0
- package/package.json +12 -6
- package/src/codeframe.js +24 -8
- package/test/codeframe.test.js +23 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parcel/codeframe",
|
|
3
|
-
"version": "2.0.0-
|
|
3
|
+
"version": "2.0.0-dev.1514+023281607",
|
|
4
4
|
"description": "Blazing fast, zero configuration web application bundler",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"publishConfig": {
|
|
@@ -19,14 +19,20 @@
|
|
|
19
19
|
"engines": {
|
|
20
20
|
"node": ">= 12.0.0"
|
|
21
21
|
},
|
|
22
|
+
"targets": {
|
|
23
|
+
"main": {
|
|
24
|
+
"includeNodeModules": {
|
|
25
|
+
"chalk": false
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
},
|
|
22
29
|
"dependencies": {
|
|
23
|
-
"chalk": "^4.1.0"
|
|
30
|
+
"chalk": "^4.1.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
24
33
|
"emphasize": "^4.2.0",
|
|
25
34
|
"slice-ansi": "^4.0.0",
|
|
26
35
|
"string-width": "^4.2.0"
|
|
27
36
|
},
|
|
28
|
-
"
|
|
29
|
-
"strip-ansi": "^6.0.0"
|
|
30
|
-
},
|
|
31
|
-
"gitHead": "daece49d003ba804bbdaa3a7ed3d6aaf446f166d"
|
|
37
|
+
"gitHead": "023281607649691b36120ce9b885985a1f059803"
|
|
32
38
|
}
|
package/src/codeframe.js
CHANGED
|
@@ -84,6 +84,7 @@ export default function codeFrame(
|
|
|
84
84
|
};
|
|
85
85
|
|
|
86
86
|
// Make columns/lines start at 1
|
|
87
|
+
let originalHighlights = highlights;
|
|
87
88
|
highlights = highlights.map(h => {
|
|
88
89
|
return {
|
|
89
90
|
start: {
|
|
@@ -112,18 +113,24 @@ export default function codeFrame(
|
|
|
112
113
|
let startLine = firstHighlight.start.line - opts.padding.before;
|
|
113
114
|
startLine = startLine < 0 ? 0 : startLine;
|
|
114
115
|
let endLineIndex = lastHighlight.end.line + opts.padding.after;
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
116
|
+
let tail;
|
|
117
|
+
if (endLineIndex - startLine > opts.maxLines) {
|
|
118
|
+
let maxLine = startLine + opts.maxLines - 1;
|
|
119
|
+
highlights = highlights.filter(h => h.start.line < maxLine);
|
|
120
|
+
lastHighlight = highlights[0];
|
|
121
|
+
endLineIndex = Math.min(
|
|
122
|
+
maxLine,
|
|
123
|
+
lastHighlight.end.line + opts.padding.after,
|
|
124
|
+
);
|
|
125
|
+
tail = originalHighlights.filter(h => h.start.line > endLineIndex);
|
|
126
|
+
}
|
|
119
127
|
|
|
120
128
|
let lineNumberLength = (endLineIndex + 1).toString(10).length;
|
|
121
129
|
|
|
122
130
|
// Split input into lines and highlight syntax
|
|
123
131
|
let lines = code.split(NEWLINE);
|
|
124
|
-
let syntaxHighlightedLines = (
|
|
125
|
-
? highlightSyntax(code, opts.language)
|
|
126
|
-
: code
|
|
132
|
+
let syntaxHighlightedLines = (
|
|
133
|
+
opts.syntaxHighlighting ? highlightSyntax(code, opts.language) : code
|
|
127
134
|
)
|
|
128
135
|
.replace(TAB_REPLACE_REGEX, TAB_REPLACEMENT)
|
|
129
136
|
.split(NEWLINE);
|
|
@@ -253,6 +260,9 @@ export default function codeFrame(
|
|
|
253
260
|
characters += startCol - lastCol;
|
|
254
261
|
}
|
|
255
262
|
|
|
263
|
+
// Don't crash (and swallow the original message) if the diagnostic is malformed (end is before start).
|
|
264
|
+
characters = Math.max(1, characters);
|
|
265
|
+
|
|
256
266
|
// Append the highlight indicators
|
|
257
267
|
highlightLine += highlighter('^'.repeat(characters));
|
|
258
268
|
|
|
@@ -282,5 +292,11 @@ export default function codeFrame(
|
|
|
282
292
|
}
|
|
283
293
|
}
|
|
284
294
|
|
|
285
|
-
|
|
295
|
+
let result = resultLines.join('\n');
|
|
296
|
+
|
|
297
|
+
if (tail && tail.length > 0) {
|
|
298
|
+
result += '\n\n' + codeFrame(code, tail, inputOpts);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
return result;
|
|
286
302
|
}
|
package/test/codeframe.test.js
CHANGED
|
@@ -796,4 +796,27 @@ describe('codeframe', () => {
|
|
|
796
796
|
assert.equal(lines[3], ' 9 | ');
|
|
797
797
|
assert.equal(lines[4], ' 10 | /**');
|
|
798
798
|
});
|
|
799
|
+
|
|
800
|
+
it('should still generate a codeframe when end is before start', () => {
|
|
801
|
+
let codeframeString = codeframe(
|
|
802
|
+
'hello world',
|
|
803
|
+
[
|
|
804
|
+
{
|
|
805
|
+
start: {
|
|
806
|
+
column: 5,
|
|
807
|
+
line: 1,
|
|
808
|
+
},
|
|
809
|
+
end: {
|
|
810
|
+
column: 1,
|
|
811
|
+
line: 1,
|
|
812
|
+
},
|
|
813
|
+
},
|
|
814
|
+
],
|
|
815
|
+
{useColor: false},
|
|
816
|
+
);
|
|
817
|
+
|
|
818
|
+
let lines = codeframeString.split(LINE_END);
|
|
819
|
+
assert.equal(lines[0], '> 1 | hello world');
|
|
820
|
+
assert.equal(lines[1], '> | ^');
|
|
821
|
+
});
|
|
799
822
|
});
|