fix code-frame marker with highlighting (#10211)
This commit is contained in:
parent
7dc5fdb390
commit
eb3230f9db
@ -76,7 +76,7 @@ function getMarkerLines(
|
|||||||
} else if (i === 0) {
|
} else if (i === 0) {
|
||||||
const sourceLength = source[lineNumber - 1].length;
|
const sourceLength = source[lineNumber - 1].length;
|
||||||
|
|
||||||
markerLines[lineNumber] = [startColumn, sourceLength - startColumn];
|
markerLines[lineNumber] = [startColumn, sourceLength - startColumn + 1];
|
||||||
} else if (i === lineDiff) {
|
} else if (i === lineDiff) {
|
||||||
markerLines[lineNumber] = [0, endColumn];
|
markerLines[lineNumber] = [0, endColumn];
|
||||||
} else {
|
} else {
|
||||||
@ -112,15 +112,16 @@ export function codeFrameColumns(
|
|||||||
const maybeHighlight = (chalkFn, string) => {
|
const maybeHighlight = (chalkFn, string) => {
|
||||||
return highlighted ? chalkFn(string) : string;
|
return highlighted ? chalkFn(string) : string;
|
||||||
};
|
};
|
||||||
if (highlighted) rawLines = highlight(rawLines, opts);
|
|
||||||
|
|
||||||
const lines = rawLines.split(NEWLINE);
|
const lines = rawLines.split(NEWLINE);
|
||||||
const { start, end, markerLines } = getMarkerLines(loc, lines, opts);
|
const { start, end, markerLines } = getMarkerLines(loc, lines, opts);
|
||||||
const hasColumns = loc.start && typeof loc.start.column === "number";
|
const hasColumns = loc.start && typeof loc.start.column === "number";
|
||||||
|
|
||||||
const numberMaxWidth = String(end).length;
|
const numberMaxWidth = String(end).length;
|
||||||
|
|
||||||
let frame = lines
|
const highlightedLines = highlighted ? highlight(rawLines, opts) : rawLines;
|
||||||
|
|
||||||
|
let frame = highlightedLines
|
||||||
|
.split(NEWLINE)
|
||||||
.slice(start, end)
|
.slice(start, end)
|
||||||
.map((line, index) => {
|
.map((line, index) => {
|
||||||
const number = start + 1 + index;
|
const number = start + 1 + index;
|
||||||
|
|||||||
@ -102,6 +102,45 @@ describe("@babel/code-frame", function() {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("opts.highlightCode with multiple columns and lines", function() {
|
||||||
|
// prettier-ignore
|
||||||
|
const rawLines = [
|
||||||
|
"function a(b, c) {",
|
||||||
|
" return b + c;",
|
||||||
|
"}"
|
||||||
|
].join("\n");
|
||||||
|
|
||||||
|
const result = codeFrameColumns(
|
||||||
|
rawLines,
|
||||||
|
{
|
||||||
|
start: {
|
||||||
|
line: 1,
|
||||||
|
column: 1,
|
||||||
|
},
|
||||||
|
end: {
|
||||||
|
line: 3,
|
||||||
|
column: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
highlightCode: true,
|
||||||
|
message: "Message about things",
|
||||||
|
},
|
||||||
|
);
|
||||||
|
const stripped = stripAnsi(result);
|
||||||
|
expect(stripped).toEqual(
|
||||||
|
// prettier-ignore
|
||||||
|
[
|
||||||
|
"> 1 | function a(b, c) {",
|
||||||
|
" | ^^^^^^^^^^^^^^^^^^",
|
||||||
|
"> 2 | return b + c;",
|
||||||
|
" | ^^^^^^^^^^^^^^^",
|
||||||
|
"> 3 | }",
|
||||||
|
" | ^ Message about things",
|
||||||
|
].join('\n'),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
test("opts.linesAbove", function() {
|
test("opts.linesAbove", function() {
|
||||||
const rawLines = [
|
const rawLines = [
|
||||||
"/**",
|
"/**",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user