diff --git a/tests/config/format-test/create-snapshot.js b/tests/config/format-test/create-snapshot.js index a33d881d8..e51293aef 100644 --- a/tests/config/format-test/create-snapshot.js +++ b/tests/config/format-test/create-snapshot.js @@ -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; diff --git a/tests/config/format-test/run-format-test.js b/tests/config/format-test/run-format-test.js index 9b3ca212f..cbf3da15b 100644 --- a/tests/config/format-test/run-format-test.js +++ b/tests/config/format-test/run-format-test.js @@ -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 diff --git a/tests/format/angular/angular/__snapshots__/format.test.js.snap b/tests/format/angular/angular/__snapshots__/format.test.js.snap index 7156e9d70..eefc2db08 100644 --- a/tests/format/angular/angular/__snapshots__/format.test.js.snap +++ b/tests/format/angular/angular/__snapshots__/format.test.js.snap @@ -4,8 +4,7 @@ exports[`angularjs.html - {"bracketSpacing":false} format 1`] = ` ====================================options===================================== bracketSpacing: false parsers: ["angular"] -printWidth: 80 - | printWidth + printWidth: 80 (default) | =====================================input======================================