2022-01-04 17:00:25 +08:00
|
|
|
import { wrap as raw } from "jest-snapshot-serializer-raw";
|
2026-01-25 20:48:49 +08:00
|
|
|
import { CURSOR_PLACEHOLDER } from "./constants.js";
|
2022-01-04 17:00:25 +08:00
|
|
|
import visualizeEndOfLine from "./visualize-end-of-line.js";
|
2023-12-17 19:34:12 +00:00
|
|
|
import visualizeRange from "./visualize-range.js";
|
2020-05-28 09:29:21 +08:00
|
|
|
|
2026-01-31 03:45:41 +08:00
|
|
|
const DEFAULT_PRINT_WIDTH = 80;
|
|
|
|
|
const SEPARATOR_WIDTH = DEFAULT_PRINT_WIDTH;
|
2021-02-07 08:59:30 +08:00
|
|
|
function printSeparator(description = "") {
|
|
|
|
|
const leftLength = Math.floor((SEPARATOR_WIDTH - description.length) / 2);
|
|
|
|
|
const rightLength = SEPARATOR_WIDTH - leftLength - description.length;
|
2020-05-28 09:29:21 +08:00
|
|
|
return "=".repeat(leftLength) + description + "=".repeat(rightLength);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function stringify(value) {
|
2025-12-21 18:07:47 +08:00
|
|
|
if (value === Number.POSITIVE_INFINITY) {
|
|
|
|
|
return "Infinity";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
|
return `[${value.map((v) => JSON.stringify(v)).join(", ")}]`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return JSON.stringify(value);
|
2020-05-28 09:29:21 +08:00
|
|
|
}
|
2021-02-07 08:59:30 +08:00
|
|
|
|
2020-05-28 09:29:21 +08:00
|
|
|
function printOptions(options) {
|
2021-02-07 08:59:30 +08:00
|
|
|
const {
|
|
|
|
|
plugins,
|
|
|
|
|
filepath,
|
|
|
|
|
errors,
|
|
|
|
|
parser,
|
|
|
|
|
|
|
|
|
|
...snapshotOptions
|
|
|
|
|
} = options;
|
|
|
|
|
|
|
|
|
|
const keys = Object.keys(snapshotOptions).sort();
|
|
|
|
|
return keys
|
|
|
|
|
.map((key) => `${key}: ${stringify(snapshotOptions[key])}`)
|
|
|
|
|
.join("\n");
|
2020-05-28 09:29:21 +08:00
|
|
|
}
|
|
|
|
|
|
2026-01-31 03:45:41 +08:00
|
|
|
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)
|
|
|
|
|
)
|
|
|
|
|
) {
|
2021-02-07 08:59:30 +08:00
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-31 03:45:41 +08:00
|
|
|
const widthIndicator =
|
|
|
|
|
printWidth === undefined
|
|
|
|
|
? defaultWidthIndicator
|
|
|
|
|
: makeWidthIndicator(printWidth);
|
2021-02-07 08:59:30 +08:00
|
|
|
|
2026-01-31 03:45:41 +08:00
|
|
|
return widthIndicator;
|
2021-02-07 08:59:30 +08:00
|
|
|
}
|
|
|
|
|
|
2026-01-25 20:48:49 +08:00
|
|
|
function createSnapshot(formatResult, { parsers, formatOptions }) {
|
2021-02-07 08:59:30 +08:00
|
|
|
let {
|
|
|
|
|
inputWithCursor: input,
|
|
|
|
|
outputWithCursor: output,
|
|
|
|
|
options,
|
|
|
|
|
} = formatResult;
|
|
|
|
|
let { rangeStart, rangeEnd, cursorOffset, printWidth } = options;
|
|
|
|
|
|
|
|
|
|
let codeOffset = 0;
|
|
|
|
|
if (typeof rangeStart === "number" || typeof rangeEnd === "number") {
|
|
|
|
|
if (typeof cursorOffset === "number") {
|
|
|
|
|
if (typeof rangeStart === "number" && rangeStart > cursorOffset) {
|
|
|
|
|
rangeStart += CURSOR_PLACEHOLDER.length;
|
|
|
|
|
}
|
|
|
|
|
if (typeof rangeEnd === "number" && rangeEnd > cursorOffset) {
|
|
|
|
|
rangeEnd += CURSOR_PLACEHOLDER.length;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
input = visualizeRange(input, { rangeStart, rangeEnd });
|
2025-12-21 23:44:08 +08:00
|
|
|
codeOffset = input.match(/^>?\s+1 \|/)[0].length + 1;
|
2021-02-07 08:59:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ("endOfLine" in formatOptions) {
|
|
|
|
|
input = visualizeEndOfLine(input);
|
|
|
|
|
output = visualizeEndOfLine(output);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const widthIndicator = printWidthIndicator(printWidth, codeOffset);
|
|
|
|
|
|
2020-05-28 09:29:21 +08:00
|
|
|
return raw(
|
2021-01-26 09:46:14 +08:00
|
|
|
[
|
2026-01-31 03:45:41 +08:00
|
|
|
addOffset(
|
|
|
|
|
[
|
|
|
|
|
printSeparator("options"),
|
|
|
|
|
printOptions({ ...options, parsers }),
|
|
|
|
|
...(widthIndicator ? [widthIndicator] : []),
|
|
|
|
|
printSeparator("input"),
|
|
|
|
|
].join("\n"),
|
|
|
|
|
codeOffset,
|
|
|
|
|
),
|
2021-01-26 09:46:14 +08:00
|
|
|
input,
|
2026-01-31 03:45:41 +08:00
|
|
|
addOffset(
|
|
|
|
|
[printSeparator("output"), output, printSeparator()].join("\n"),
|
|
|
|
|
codeOffset,
|
|
|
|
|
),
|
2021-01-26 09:46:14 +08:00
|
|
|
].join("\n"),
|
2020-05-28 09:29:21 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-31 03:45:41 +08:00
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 17:00:25 +08:00
|
|
|
export default createSnapshot;
|