Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@
"eslint-plugin-import": "^2.32.0",
"esm-utils": "^4.4.2",
"globals": "^17.4.0",
"index-to-position": "^1.2.0",
"jest": "^30.3.0",
"jest-light-runner": "^0.7.11",
"jest-snapshot-serializer-ansi": "^2.2.1",
"jest-snapshot-serializer-raw": "^2.0.0",
"jest-watch-typeahead": "^3.0.1",
"knip": "^5.86.0",
"lines-and-columns": "^2.0.4",
"prettier": "^3.8.1",
"solc": "^0.8.34",
"ts-loader": "^9.5.4",
Expand Down
2 changes: 1 addition & 1 deletion tests/config/run-format-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import getPlugins from "./get-plugins.js";
import compileContract from "./utils/compile-contract.js";
import consistentEndOfLine from "./utils/consistent-end-of-line.js";
import createSnapshot from "./utils/create-snapshot.js";
import stringifyOptionsForTitle from "./utils/stringify-options-for-title.js";
import { stringifyOptionsForTitle } from "./utils/stringify-options-for-title.js";
import visualizeEndOfLine from "./utils/visualize-end-of-line.js";

const { __dirname } = createEsmUtils(import.meta);
Expand Down
2 changes: 1 addition & 1 deletion tests/config/utils/consistent-end-of-line.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function consistentEndOfLine(text) {
let firstEndOfLine;
return text.replace(/\r\n?|\n/gu, (endOfLine) => {
return text.replace(/\r\n?|\n/g, (endOfLine) => {
firstEndOfLine = firstEndOfLine ?? endOfLine;
return firstEndOfLine;
});
Expand Down
93 changes: 68 additions & 25 deletions tests/config/utils/create-snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@ import { wrap as raw } from "jest-snapshot-serializer-raw";
import visualizeEndOfLine from "./visualize-end-of-line.js";
import visualizeRange from "./visualize-range.js";

const SEPARATOR_WIDTH = 80;
const CURSOR_PLACEHOLDER = "<|>";

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) {
return value === Number.POSITIVE_INFINITY
? "Infinity"
: Array.isArray(value)
? `[${value.map((v) => JSON.stringify(v)).join(", ")}]`
: JSON.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) {
Expand All @@ -33,23 +40,41 @@ function printOptions(options) {
.join("\n");
}

function printWidthIndicator(printWidth, offset) {
if (!Number.isFinite(printWidth) || printWidth < 1) {
return "";
function makeWidthIndicator(printWidth) {
const text =
printWidth === undefined
? `printWidth: ${DEFAULT_PRINT_WIDTH} (default)`
: `printWidth: ${printWidth}`;

if (printWidth === undefined) {
printWidth = DEFAULT_PRINT_WIDTH;
}

let before = "";
if (offset) {
before = " ".repeat(offset - 1) + "|";
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 "";
}

return `${before}${" ".repeat(printWidth)}| printWidth`;
const widthIndicator =
printWidth === undefined
? defaultWidthIndicator
: makeWidthIndicator(printWidth);

return widthIndicator;
}

function createSnapshot(
formatResult,
{ parsers, formatOptions, CURSOR_PLACEHOLDER },
) {
function createSnapshot(formatResult, { parsers, formatOptions }) {
let {
inputWithCursor: input,
outputWithCursor: output,
Expand All @@ -69,7 +94,7 @@ function createSnapshot(
}

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

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

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;
22 changes: 13 additions & 9 deletions tests/config/utils/stringify-options-for-title.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
function stringifyOptions(options) {
const string = JSON.stringify(options || {}, (key, value) =>
key === "plugins" || key === "errors"
? undefined
: value === Number.POSITIVE_INFINITY
? "Infinity"
: value,
);
function stringifyOptionsForTitle(options) {
const string = JSON.stringify(options || {}, (key, value) => {
if (key === "plugins" || key === "errors") {
return;
}

if (value === Number.POSITIVE_INFINITY) {
return "Infinity";
}

return value;
});

return string === "{}" ? "" : string;
}

export default stringifyOptions;
export { stringifyOptionsForTitle };
5 changes: 4 additions & 1 deletion tests/config/utils/visualize-end-of-line.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/**
@param {string} text
*/
function visualizeEndOfLine(text) {
return text.replace(/\r\n?|\n/gu, (endOfLine) => {
return text.replace(/\r\n?|\n/g, (endOfLine) => {
switch (endOfLine) {
case "\n":
return "<LF>\n";
Expand Down
27 changes: 9 additions & 18 deletions tests/config/utils/visualize-range.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
import indexToPosition from "index-to-position";
import { codeFrameColumns } from "@babel/code-frame";
import { LinesAndColumns } from "lines-and-columns";
const codeFrameColumnsOptions = {
linesAbove: Number.POSITIVE_INFINITY,
linesBelow: Number.POSITIVE_INFINITY,
};

const locationForRange = (text, rangeStart, rangeEnd) => {
if (rangeStart > rangeEnd) {
[rangeStart, rangeEnd] = [rangeEnd, rangeStart];
}
const lines = new LinesAndColumns(text);
const start = lines.locationForIndex(rangeStart);
const end = lines.locationForIndex(rangeEnd);
const locationForRange = (text, range) => {
const [start, end] = [...range]
.sort((indexA, indexB) => indexA - indexB)
.map((index) => indexToPosition(text, index, { oneBased: true }));

start.line += 1;
start.column += 1;
end.line += 1;
if (start.line === end.line) {
end.column += 1;
if (start.line !== end.line) {
end.column -= 1;
}

return {
start,
end,
};
return { start, end };
};

const visualizeRange = (text, { rangeStart = 0, rangeEnd = text.length }) =>
codeFrameColumns(
text,
locationForRange(text, rangeStart, rangeEnd),
locationForRange(text, [rangeStart, rangeEnd]),
rangeStart > rangeEnd
? { ...codeFrameColumnsOptions, message: "[Reversed range]" }
: codeFrameColumnsOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`AddressPayable.sol format 1`] = `
====================================options=====================================
parsers: ["slang"]
printWidth: 80
| printWidth
printWidth: 80 |
=====================================input======================================
pragma solidity ^0.5.2;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`AllSolidityFeatures.sol format 1`] = `
====================================options=====================================
parsers: ["slang"]
printWidth: 80
| printWidth
printWidth: 80 |
=====================================input======================================
// Examples taken from the Solidity documentation online.
pragma experimental ABIEncoderV2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`AllSolidityFeatures.sol - {"compiler":"0.4.26"} format 1`] = `
compiler: "0.4.26"
parsers: ["slang"]
printWidth: 80
| printWidth
printWidth: 80 |
=====================================input======================================
contract c {
function c()
Expand Down
2 changes: 1 addition & 1 deletion tests/format/Arrays/__snapshots__/format.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`Arrays.sol format 1`] = `
====================================options=====================================
parsers: ["slang"]
printWidth: 80
| printWidth
printWidth: 80 |
=====================================input======================================
pragma solidity ^0.5.2;

Expand Down
2 changes: 1 addition & 1 deletion tests/format/Assembly/__snapshots__/format.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`Assembly.sol format 1`] = `
====================================options=====================================
parsers: ["slang"]
printWidth: 80
| printWidth
printWidth: 80 |
=====================================input======================================
contract Assembly {
function ifAssembly() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`Assembly.sol format 1`] = `
====================================options=====================================
parsers: ["slang"]
printWidth: 80
| printWidth
printWidth: 80 |
=====================================input======================================
pragma solidity ^0.4.26;

Expand Down
2 changes: 1 addition & 1 deletion tests/format/Assignments/__snapshots__/format.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`Assignments.sol format 1`] = `
====================================options=====================================
parsers: ["slang"]
printWidth: 80
| printWidth
printWidth: 80 |
=====================================input======================================
contract Assignments {
address payable public inParentheses = (contractPointer.functionCall(data.data1,data.data2,IERC20(data.token).decimals(),currency));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`BasicIterator.sol - {"compiler":"0.4.26"} format 1`] = `
compiler: "0.4.26"
parsers: ["slang"]
printWidth: 80
| printWidth
printWidth: 80 |
=====================================input======================================
/*
This is a very simple demonstration of a while loops. Same as JS/c.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`Group.sol format 1`] = `
====================================options=====================================
parsers: ["slang"]
printWidth: 80
| printWidth
printWidth: 80 |
=====================================input======================================
// SPDX-License-Identifier: MIT
pragma solidity "0.8.28";
Expand Down Expand Up @@ -1301,7 +1301,7 @@ exports[`Indent.sol format 1`] = `
====================================options=====================================
parsers: ["slang"]
printWidth: 80
| printWidth
printWidth: 80 |
=====================================input======================================
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
Expand Down
Loading