Skip to content

Commit 248797f

Browse files
authored
updating tests/config/utils to the latest matching prettier (#1453)
* updating `tests/config/utils` to the latest matching prettier * updating new test
1 parent fe32e08 commit 248797f

71 files changed

Lines changed: 222 additions & 178 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

package-lock.json

Lines changed: 14 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@
9999
"eslint-plugin-import": "^2.32.0",
100100
"esm-utils": "^4.4.2",
101101
"globals": "^17.4.0",
102+
"index-to-position": "^1.2.0",
102103
"jest": "^30.3.0",
103104
"jest-light-runner": "^0.7.11",
104105
"jest-snapshot-serializer-ansi": "^2.2.1",
105106
"jest-snapshot-serializer-raw": "^2.0.0",
106107
"jest-watch-typeahead": "^3.0.1",
107108
"knip": "^5.86.0",
108-
"lines-and-columns": "^2.0.4",
109109
"prettier": "^3.8.1",
110110
"solc": "^0.8.34",
111111
"ts-loader": "^9.5.4",

tests/config/run-format-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import getPlugins from "./get-plugins.js";
99
import compileContract from "./utils/compile-contract.js";
1010
import consistentEndOfLine from "./utils/consistent-end-of-line.js";
1111
import createSnapshot from "./utils/create-snapshot.js";
12-
import stringifyOptionsForTitle from "./utils/stringify-options-for-title.js";
12+
import { stringifyOptionsForTitle } from "./utils/stringify-options-for-title.js";
1313
import visualizeEndOfLine from "./utils/visualize-end-of-line.js";
1414

1515
const { __dirname } = createEsmUtils(import.meta);

tests/config/utils/consistent-end-of-line.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function consistentEndOfLine(text) {
22
let firstEndOfLine;
3-
return text.replace(/\r\n?|\n/gu, (endOfLine) => {
3+
return text.replace(/\r\n?|\n/g, (endOfLine) => {
44
firstEndOfLine = firstEndOfLine ?? endOfLine;
55
return firstEndOfLine;
66
});

tests/config/utils/create-snapshot.js

Lines changed: 68 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,26 @@ import { wrap as raw } from "jest-snapshot-serializer-raw";
22
import visualizeEndOfLine from "./visualize-end-of-line.js";
33
import visualizeRange from "./visualize-range.js";
44

5-
const SEPARATOR_WIDTH = 80;
5+
const CURSOR_PLACEHOLDER = "<|>";
6+
7+
const DEFAULT_PRINT_WIDTH = 80;
8+
const SEPARATOR_WIDTH = DEFAULT_PRINT_WIDTH;
69
function printSeparator(description = "") {
710
const leftLength = Math.floor((SEPARATOR_WIDTH - description.length) / 2);
811
const rightLength = SEPARATOR_WIDTH - leftLength - description.length;
912
return "=".repeat(leftLength) + description + "=".repeat(rightLength);
1013
}
1114

1215
function stringify(value) {
13-
return value === Number.POSITIVE_INFINITY
14-
? "Infinity"
15-
: Array.isArray(value)
16-
? `[${value.map((v) => JSON.stringify(v)).join(", ")}]`
17-
: JSON.stringify(value);
16+
if (value === Number.POSITIVE_INFINITY) {
17+
return "Infinity";
18+
}
19+
20+
if (Array.isArray(value)) {
21+
return `[${value.map((v) => JSON.stringify(v)).join(", ")}]`;
22+
}
23+
24+
return JSON.stringify(value);
1825
}
1926

2027
function printOptions(options) {
@@ -33,23 +40,41 @@ function printOptions(options) {
3340
.join("\n");
3441
}
3542

36-
function printWidthIndicator(printWidth, offset) {
37-
if (!Number.isFinite(printWidth) || printWidth < 1) {
38-
return "";
43+
function makeWidthIndicator(printWidth) {
44+
const text =
45+
printWidth === undefined
46+
? `printWidth: ${DEFAULT_PRINT_WIDTH} (default)`
47+
: `printWidth: ${printWidth}`;
48+
49+
if (printWidth === undefined) {
50+
printWidth = DEFAULT_PRINT_WIDTH;
3951
}
4052

41-
let before = "";
42-
if (offset) {
43-
before = " ".repeat(offset - 1) + "|";
53+
return printWidth >= text.length + 2
54+
? (text + " |").padStart(printWidth, " ")
55+
: " ".repeat(printWidth) + "| " + text;
56+
}
57+
58+
const defaultWidthIndicator = makeWidthIndicator();
59+
function printWidthIndicator(printWidth) {
60+
if (
61+
!(
62+
printWidth === undefined ||
63+
(Number.isSafeInteger(printWidth) && printWidth > 0)
64+
)
65+
) {
66+
return "";
4467
}
4568

46-
return `${before}${" ".repeat(printWidth)}| printWidth`;
69+
const widthIndicator =
70+
printWidth === undefined
71+
? defaultWidthIndicator
72+
: makeWidthIndicator(printWidth);
73+
74+
return widthIndicator;
4775
}
4876

49-
function createSnapshot(
50-
formatResult,
51-
{ parsers, formatOptions, CURSOR_PLACEHOLDER },
52-
) {
77+
function createSnapshot(formatResult, { parsers, formatOptions }) {
5378
let {
5479
inputWithCursor: input,
5580
outputWithCursor: output,
@@ -69,7 +94,7 @@ function createSnapshot(
6994
}
7095

7196
input = visualizeRange(input, { rangeStart, rangeEnd });
72-
codeOffset = input.match(/^>?\s+1 \|/u)[0].length + 1;
97+
codeOffset = input.match(/^>?\s+1 \|/)[0].length + 1;
7398
}
7499

75100
if ("endOfLine" in formatOptions) {
@@ -81,16 +106,34 @@ function createSnapshot(
81106

82107
return raw(
83108
[
84-
printSeparator("options"),
85-
printOptions({ ...options, parsers }),
86-
...(widthIndicator ? [widthIndicator] : []),
87-
printSeparator("input"),
109+
addOffset(
110+
[
111+
printSeparator("options"),
112+
printOptions({ ...options, parsers }),
113+
...(widthIndicator ? [widthIndicator] : []),
114+
printSeparator("input"),
115+
].join("\n"),
116+
codeOffset,
117+
),
88118
input,
89-
printSeparator("output"),
90-
output,
91-
printSeparator(),
119+
addOffset(
120+
[printSeparator("output"), output, printSeparator()].join("\n"),
121+
codeOffset,
122+
),
92123
].join("\n"),
93124
);
94125
}
95126

127+
function addOffset(text, offset) {
128+
if (!offset) {
129+
return text;
130+
}
131+
132+
const prefix = " ".repeat(offset - 2) + ":";
133+
return text
134+
.split("\n")
135+
.map((line) => `${prefix}${line ? ` ${line}` : line}`)
136+
.join("\n");
137+
}
138+
96139
export default createSnapshot;
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
function stringifyOptions(options) {
2-
const string = JSON.stringify(options || {}, (key, value) =>
3-
key === "plugins" || key === "errors"
4-
? undefined
5-
: value === Number.POSITIVE_INFINITY
6-
? "Infinity"
7-
: value,
8-
);
1+
function stringifyOptionsForTitle(options) {
2+
const string = JSON.stringify(options || {}, (key, value) => {
3+
if (key === "plugins" || key === "errors") {
4+
return;
5+
}
6+
7+
if (value === Number.POSITIVE_INFINITY) {
8+
return "Infinity";
9+
}
10+
11+
return value;
12+
});
913

1014
return string === "{}" ? "" : string;
1115
}
1216

13-
export default stringifyOptions;
17+
export { stringifyOptionsForTitle };

tests/config/utils/visualize-end-of-line.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
/**
2+
@param {string} text
3+
*/
14
function visualizeEndOfLine(text) {
2-
return text.replace(/\r\n?|\n/gu, (endOfLine) => {
5+
return text.replace(/\r\n?|\n/g, (endOfLine) => {
36
switch (endOfLine) {
47
case "\n":
58
return "<LF>\n";

tests/config/utils/visualize-range.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,26 @@
1+
import indexToPosition from "index-to-position";
12
import { codeFrameColumns } from "@babel/code-frame";
2-
import { LinesAndColumns } from "lines-and-columns";
33
const codeFrameColumnsOptions = {
44
linesAbove: Number.POSITIVE_INFINITY,
55
linesBelow: Number.POSITIVE_INFINITY,
66
};
77

8-
const locationForRange = (text, rangeStart, rangeEnd) => {
9-
if (rangeStart > rangeEnd) {
10-
[rangeStart, rangeEnd] = [rangeEnd, rangeStart];
11-
}
12-
const lines = new LinesAndColumns(text);
13-
const start = lines.locationForIndex(rangeStart);
14-
const end = lines.locationForIndex(rangeEnd);
8+
const locationForRange = (text, range) => {
9+
const [start, end] = [...range]
10+
.sort((indexA, indexB) => indexA - indexB)
11+
.map((index) => indexToPosition(text, index, { oneBased: true }));
1512

16-
start.line += 1;
17-
start.column += 1;
18-
end.line += 1;
19-
if (start.line === end.line) {
20-
end.column += 1;
13+
if (start.line !== end.line) {
14+
end.column -= 1;
2115
}
2216

23-
return {
24-
start,
25-
end,
26-
};
17+
return { start, end };
2718
};
2819

2920
const visualizeRange = (text, { rangeStart = 0, rangeEnd = text.length }) =>
3021
codeFrameColumns(
3122
text,
32-
locationForRange(text, rangeStart, rangeEnd),
23+
locationForRange(text, [rangeStart, rangeEnd]),
3324
rangeStart > rangeEnd
3425
? { ...codeFrameColumnsOptions, message: "[Reversed range]" }
3526
: codeFrameColumnsOptions,

tests/format/AddressPayable/__snapshots__/format.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`AddressPayable.sol format 1`] = `
44
====================================options=====================================
55
parsers: ["slang"]
66
printWidth: 80
7-
| printWidth
7+
printWidth: 80 |
88
=====================================input======================================
99
pragma solidity ^0.5.2;
1010

tests/format/AllSolidityFeatures/__snapshots__/format.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`AllSolidityFeatures.sol format 1`] = `
44
====================================options=====================================
55
parsers: ["slang"]
66
printWidth: 80
7-
| printWidth
7+
printWidth: 80 |
88
=====================================input======================================
99
// Examples taken from the Solidity documentation online.
1010
pragma experimental ABIEncoderV2;

0 commit comments

Comments
 (0)