fix(code-frame): do not pad gutter of empty lines (#12567)

* fix(code-frame): do not pad gutter of empty lines

* Add workaround for expected Jest e2e test failure

Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
Simen Bekkhus 2020-12-29 15:26:13 +01:00 committed by GitHub
parent fbef603c43
commit 2d35f5a8f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 15 deletions

View File

@ -152,7 +152,7 @@ export function codeFrameColumns(
.map((line, index) => { .map((line, index) => {
const number = start + 1 + index; const number = start + 1 + index;
const paddedNumber = ` ${number}`.slice(-numberMaxWidth); const paddedNumber = ` ${number}`.slice(-numberMaxWidth);
const gutter = ` ${paddedNumber} | `; const gutter = ` ${paddedNumber} |`;
const hasMarker = markerLines[number]; const hasMarker = markerLines[number];
const lastMarkerLine = !markerLines[number + 1]; const lastMarkerLine = !markerLines[number + 1];
if (hasMarker) { if (hasMarker) {
@ -166,6 +166,7 @@ export function codeFrameColumns(
markerLine = [ markerLine = [
"\n ", "\n ",
maybeHighlight(defs.gutter, gutter.replace(/\d/g, " ")), maybeHighlight(defs.gutter, gutter.replace(/\d/g, " ")),
" ",
markerSpacing, markerSpacing,
maybeHighlight(defs.marker, "^").repeat(numberOfMarkers), maybeHighlight(defs.marker, "^").repeat(numberOfMarkers),
].join(""); ].join("");
@ -177,11 +178,13 @@ export function codeFrameColumns(
return [ return [
maybeHighlight(defs.marker, ">"), maybeHighlight(defs.marker, ">"),
maybeHighlight(defs.gutter, gutter), maybeHighlight(defs.gutter, gutter),
line, line.length > 0 ? ` ${line}` : "",
markerLine, markerLine,
].join(""); ].join("");
} else { } else {
return ` ${maybeHighlight(defs.gutter, gutter)}${line}`; return ` ${maybeHighlight(defs.gutter, gutter)}${
line.length > 0 ? ` ${line}` : ""
}`;
} }
}) })
.join("\n"); .join("\n");

View File

@ -42,7 +42,7 @@ describe("@babel/code-frame", function () {
" 6 | * @returns Number", " 6 | * @returns Number",
"> 7 | */", "> 7 | */",
" | ^", " | ^",
" 8 | ", " 8 |",
" 9 | function sum(a, b) {", " 9 | function sum(a, b) {",
" 10 | return a + b", " 10 | return a + b",
].join("\n"), ].join("\n"),
@ -70,7 +70,7 @@ describe("@babel/code-frame", function () {
"> 6 | * @returns Number", "> 6 | * @returns Number",
" | ^", " | ^",
" 7 | */", " 7 | */",
" 8 | ", " 8 |",
" 9 | function sum(a, b) {", " 9 | function sum(a, b) {",
].join("\n"), ].join("\n"),
); );
@ -105,8 +105,8 @@ describe("@babel/code-frame", function () {
test("opts.highlightCode with multiple columns and lines", function () { test("opts.highlightCode with multiple columns and lines", function () {
// prettier-ignore // prettier-ignore
const rawLines = [ const rawLines = [
"function a(b, c) {", "function a(b, c) {",
" return b + c;", " return b + c;",
"}" "}"
].join("\n"); ].join("\n");
@ -160,7 +160,7 @@ describe("@babel/code-frame", function () {
" 6 | * @returns Number", " 6 | * @returns Number",
"> 7 | */", "> 7 | */",
" | ^", " | ^",
" 8 | ", " 8 |",
" 9 | function sum(a, b) {", " 9 | function sum(a, b) {",
" 10 | return a + b", " 10 | return a + b",
].join("\n"), ].join("\n"),
@ -187,7 +187,7 @@ describe("@babel/code-frame", function () {
" 6 | * @returns Number", " 6 | * @returns Number",
"> 7 | */", "> 7 | */",
" | ^", " | ^",
" 8 | ", " 8 |",
].join("\n"), ].join("\n"),
); );
}); });
@ -207,9 +207,7 @@ describe("@babel/code-frame", function () {
"}", "}",
].join("\n"); ].join("\n");
expect(codeFrame(rawLines, 7, 2, { linesAbove: 1, linesBelow: 1 })).toEqual( expect(codeFrame(rawLines, 7, 2, { linesAbove: 1, linesBelow: 1 })).toEqual(
[" 6 | * @returns Number", "> 7 | */", " | ^", " 8 | "].join( [" 6 | * @returns Number", "> 7 | */", " | ^", " 8 |"].join("\n"),
"\n",
),
); );
}); });
@ -277,9 +275,9 @@ describe("@babel/code-frame", function () {
).toEqual( ).toEqual(
chalk.reset( chalk.reset(
[ [
" " + gutter(" 2 | "), " " + gutter(" 2 |"),
marker(">") + gutter(" 3 | "), marker(">") + gutter(" 3 |"),
" " + gutter(" 4 | "), " " + gutter(" 4 |"),
].join("\n"), ].join("\n"),
), ),
); );

View File

@ -42,6 +42,13 @@ yarn install
yarn dedupe '@babel/*' yarn dedupe '@babel/*'
yarn build yarn build
# Workaround for https://github.com/babel/babel/pull/12567
node -e '
let snapshots = fs.readFileSync("packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap", "utf8");
snapshots = snapshots.replace(/(?<!^<dim>.*)\| <\/>/gm, "|<\/> ");
fs.writeFileSync("packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap", snapshots);
'
# The full test suite takes about 20mins on CircleCI. We run only a few of them # The full test suite takes about 20mins on CircleCI. We run only a few of them
# to speed it up. # to speed it up.
# The goals of this e2e test are: # The goals of this e2e test are: