Test: Update printWidth indicator (#10293)

This commit is contained in:
fisker Cheung authored and GitHub committed 2026-01-31 03:45:41 +08:00
1 parent 6f458f46d0
commit 5017d66ea7
1328 files changed
+10117 -18807

No files matched your search

+55 -15
View File
@@ -3,7 +3,8 @@ import { CURSOR_PLACEHOLDER } from "./constants.js";
import visualizeEndOfLine from "./visualize-end-of-line.js";
import visualizeRange from "./visualize-range.js";
const SEPARATOR_WIDTH = 80;
const DEFAULT_PRINT_WIDTH = 80;
const SEPARATOR_WIDTH = DEFAULT_PRINT_WIDTH;
function printSeparator(description = "") {
const leftLength = Math.floor((SEPARATOR_WIDTH - description.length) / 2);
const rightLength = SEPARATOR_WIDTH - leftLength - description.length;
@@ -38,17 +39,38 @@ function printOptions(options) {
.join("\n");
}
function printWidthIndicator(printWidth, offset) {
if (!Number.isFinite(printWidth) || printWidth < 1) {
function makeWidthIndicator(printWidth) {
const text =
printWidth === undefined
? `printWidth: ${DEFAULT_PRINT_WIDTH} (default)`
: `printWidth: ${printWidth}`;
if (printWidth === undefined) {
printWidth = DEFAULT_PRINT_WIDTH;
}
return printWidth >= text.length + 2
? (text + " |").padStart(printWidth, " ")
: " ".repeat(printWidth) + "| " + text;
}
const defaultWidthIndicator = makeWidthIndicator();
function printWidthIndicator(printWidth) {
if (
!(
printWidth === undefined ||
(Number.isSafeInteger(printWidth) && printWidth > 0)
)
) {
return "";
}
let before = "";
if (offset) {
before = " ".repeat(offset - 1) + "|";
}
const widthIndicator =
printWidth === undefined
? defaultWidthIndicator
: makeWidthIndicator(printWidth);
return `${before}${" ".repeat(printWidth)}| printWidth`;
return widthIndicator;
}
function createSnapshot(formatResult, { parsers, formatOptions }) {
@@ -83,16 +105,34 @@ function createSnapshot(formatResult, { parsers, formatOptions }) {
return raw(
[
printSeparator("options"),
printOptions({ ...options, parsers }),
...(widthIndicator ? [widthIndicator] : []),
printSeparator("input"),
addOffset(
[
printSeparator("options"),
printOptions({ ...options, parsers }),
...(widthIndicator ? [widthIndicator] : []),
printSeparator("input"),
].join("\n"),
codeOffset,
),
input,
printSeparator("output"),
output,
printSeparator(),
addOffset(
[printSeparator("output"), output, printSeparator()].join("\n"),
codeOffset,
),
].join("\n"),
);
}
function addOffset(text, offset) {
if (!offset) {
return text;
}
const prefix = " ".repeat(offset - 2) + ":";
return text
.split("\n")
.map((line) => `${prefix}${line ? ` ${line}` : line}`)
.join("\n");
}
export default createSnapshot;
+1 -4
View File
@@ -51,10 +51,7 @@ function runFormatTest(rawFixtures, explicitParsers, rawOptions) {
path.dirname(url.fileURLToPath(importMeta.url)),
);
let options = {
printWidth: 80,
...rawOptions,
};
let options = { ...rawOptions };
// `IS_ERROR_TEST` mean to watch errors like:
// - syntax parser hasn't supported yet