-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathcreate-snapshot.js
More file actions
138 lines (116 loc) · 3.43 KB
/
create-snapshot.js
File metadata and controls
138 lines (116 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { wrap as raw } from "jest-snapshot-serializer-raw";
import { CURSOR_PLACEHOLDER } from "../constants.js";
import visualizeEndOfLine from "./visualize-end-of-line.js";
import visualizeRange from "./visualize-range.js";
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;
return "=".repeat(leftLength) + description + "=".repeat(rightLength);
}
function stringify(value) {
if (value === Number.POSITIVE_INFINITY) {
return "Infinity";
}
if (Array.isArray(value)) {
return `[${value.map((v) => JSON.stringify(v)).join(", ")}]`;
}
return JSON.stringify(value);
}
function printOptions(options) {
const {
plugins,
filepath,
errors,
parser,
...snapshotOptions
} = options;
const keys = Object.keys(snapshotOptions).sort();
return keys
.map((key) => `${key}: ${stringify(snapshotOptions[key])}`)
.join("\n");
}
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 "";
}
const widthIndicator =
printWidth === undefined
? defaultWidthIndicator
: makeWidthIndicator(printWidth);
return widthIndicator;
}
function createSnapshot(formatResult, { parsers, formatOptions }) {
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 });
codeOffset = input.match(/^>?\s+1 \|/)[0].length + 1;
}
if ("endOfLine" in formatOptions) {
input = visualizeEndOfLine(input);
output = visualizeEndOfLine(output);
}
const widthIndicator = printWidthIndicator(printWidth, codeOffset);
return raw(
[
addOffset(
[
printSeparator("options"),
printOptions({ ...options, parsers }),
...(widthIndicator ? [widthIndicator] : []),
printSeparator("input"),
].join("\n"),
codeOffset,
),
input,
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;