@@ -2,19 +2,26 @@ import { wrap as raw } from "jest-snapshot-serializer-raw";
22import visualizeEndOfLine from "./visualize-end-of-line.js" ;
33import 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 ;
69function 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
1215function 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
2027function 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+
96139export default createSnapshot ;
0 commit comments