@@ -416,7 +416,7 @@ function getPrefix(constructor, tag) {
416
416
return '' ;
417
417
}
418
418
419
- function formatValue ( ctx , value , recurseTimes , ln ) {
419
+ function formatValue ( ctx , value , recurseTimes ) {
420
420
// Primitive types cannot have properties
421
421
if ( typeof value !== 'object' && typeof value !== 'function' ) {
422
422
return formatPrimitive ( ctx . stylize , value , ctx ) ;
@@ -592,7 +592,7 @@ function formatValue(ctx, value, recurseTimes, ln) {
592
592
return ctx . stylize ( dateToISOString . call ( value ) , 'date' ) ;
593
593
}
594
594
// Make dates with properties first say the date
595
- base = ` ${ dateToISOString . call ( value ) } ` ;
595
+ base = dateToISOString . call ( value ) ;
596
596
} else if ( isError ( value ) ) {
597
597
// Make error with message first say the error
598
598
base = formatError ( value ) ;
@@ -693,7 +693,7 @@ function formatValue(ctx, value, recurseTimes, ln) {
693
693
}
694
694
ctx . seen . pop ( ) ;
695
695
696
- return reduceToSingleString ( ctx , output , base , braces , ln ) ;
696
+ return reduceToSingleString ( ctx , output , base , braces ) ;
697
697
}
698
698
699
699
function formatNumber ( fn , value ) {
@@ -768,7 +768,23 @@ function formatNamespaceObject(ctx, value, recurseTimes, keys) {
768
768
const len = keys . length ;
769
769
const output = new Array ( len ) ;
770
770
for ( var i = 0 ; i < len ; i ++ ) {
771
- output [ i ] = formatNamespaceProperty ( ctx , value , recurseTimes , keys [ i ] ) ;
771
+ try {
772
+ output [ i ] = formatProperty ( ctx , value , recurseTimes , keys [ i ] , 0 ) ;
773
+ } catch ( err ) {
774
+ if ( ! ( err instanceof ReferenceError ) ) {
775
+ throw err ;
776
+ }
777
+ // Use the existing functionality. This makes sure the indentation and
778
+ // line breaks are always correct. Otherwise it is very difficult to keep
779
+ // this aligned, even though this is a hacky way of dealing with this.
780
+ const tmp = { [ keys [ i ] ] : '' } ;
781
+ output [ i ] = formatProperty ( ctx , tmp , recurseTimes , keys [ i ] , 0 ) ;
782
+ const pos = output [ i ] . lastIndexOf ( ' ' ) ;
783
+ // We have to find the last whitespace and have to replace that value as
784
+ // it will be visualized as a regular string.
785
+ output [ i ] = output [ i ] . slice ( 0 , pos + 1 ) +
786
+ ctx . stylize ( '<uninitialized>' , 'special' ) ;
787
+ }
772
788
}
773
789
return output ;
774
790
}
@@ -996,42 +1012,21 @@ function formatPromise(ctx, value, recurseTimes, keys) {
996
1012
return output ;
997
1013
}
998
1014
999
- function formatKey ( ctx , key , enumerable ) {
1000
- if ( typeof key === 'symbol' ) {
1001
- return `[${ ctx . stylize ( key . toString ( ) , 'symbol' ) } ]` ;
1002
- }
1003
- if ( enumerable === false ) {
1004
- return `[${ key } ]` ;
1005
- }
1006
- if ( keyStrRegExp . test ( key ) ) {
1007
- return ctx . stylize ( key , 'name' ) ;
1008
- }
1009
- return ctx . stylize ( strEscape ( key ) , 'string' ) ;
1010
- }
1011
-
1012
- function formatNamespaceProperty ( ctx , ns , recurseTimes , key ) {
1013
- let value ;
1014
- try {
1015
- value = formatValue ( ctx , ns [ key ] , recurseTimes , true ) ;
1016
- } catch ( err ) {
1017
- if ( err instanceof ReferenceError ) {
1018
- value = ctx . stylize ( '<uninitialized>' , 'special' ) ;
1019
- } else {
1020
- throw err ;
1021
- }
1022
- }
1023
-
1024
- return `${ formatKey ( ctx , key ) } : ${ value } ` ;
1025
- }
1026
-
1027
1015
function formatProperty ( ctx , value , recurseTimes , key , array ) {
1028
- let str ;
1016
+ let name , str ;
1017
+ let extra = ' ' ;
1029
1018
const desc = Object . getOwnPropertyDescriptor ( value , key ) ||
1030
1019
{ value : value [ key ] , enumerable : true } ;
1031
1020
if ( desc . value !== undefined ) {
1032
1021
const diff = array !== 0 || ctx . compact === false ? 2 : 3 ;
1033
1022
ctx . indentationLvl += diff ;
1034
- str = formatValue ( ctx , desc . value , recurseTimes , array === 0 ) ;
1023
+ str = formatValue ( ctx , desc . value , recurseTimes ) ;
1024
+ if ( diff === 3 ) {
1025
+ const len = ctx . colors ? removeColors ( str ) . length : str . length ;
1026
+ if ( ctx . breakLength < len ) {
1027
+ extra = `\n${ ' ' . repeat ( ctx . indentationLvl ) } ` ;
1028
+ }
1029
+ }
1035
1030
ctx . indentationLvl -= diff ;
1036
1031
} else if ( desc . get !== undefined ) {
1037
1032
if ( desc . set !== undefined ) {
@@ -1047,11 +1042,19 @@ function formatProperty(ctx, value, recurseTimes, key, array) {
1047
1042
if ( array === 1 ) {
1048
1043
return str ;
1049
1044
}
1050
-
1051
- return `${ formatKey ( ctx , key , desc . enumerable ) } : ${ str } ` ;
1045
+ if ( typeof key === 'symbol' ) {
1046
+ name = `[${ ctx . stylize ( key . toString ( ) , 'symbol' ) } ]` ;
1047
+ } else if ( desc . enumerable === false ) {
1048
+ name = `[${ key } ]` ;
1049
+ } else if ( keyStrRegExp . test ( key ) ) {
1050
+ name = ctx . stylize ( key , 'name' ) ;
1051
+ } else {
1052
+ name = ctx . stylize ( strEscape ( key ) , 'string' ) ;
1053
+ }
1054
+ return `${ name } :${ extra } ${ str } ` ;
1052
1055
}
1053
1056
1054
- function reduceToSingleString ( ctx , output , base , braces , addLn ) {
1057
+ function reduceToSingleString ( ctx , output , base , braces ) {
1055
1058
const breakLength = ctx . breakLength ;
1056
1059
let i = 0 ;
1057
1060
if ( ctx . compact === false ) {
@@ -1080,11 +1083,10 @@ function reduceToSingleString(ctx, output, base, braces, addLn) {
1080
1083
// we need to force the first item to be on the next line or the
1081
1084
// items will not line up correctly.
1082
1085
const indentation = ' ' . repeat ( ctx . indentationLvl ) ;
1083
- const extraLn = addLn === true ? `\n${ indentation } ` : '' ;
1084
1086
const ln = base === '' && braces [ 0 ] . length === 1 ?
1085
- ' ' : `${ base ? ` ${ base } ` : base } \n${ indentation } ` ;
1087
+ ' ' : `${ base ? ` ${ base } ` : '' } \n${ indentation } ` ;
1086
1088
const str = join ( output , `,\n${ indentation } ` ) ;
1087
- return `${ extraLn } ${ braces [ 0 ] } ${ ln } ${ str } ${ braces [ 1 ] } ` ;
1089
+ return `${ braces [ 0 ] } ${ ln } ${ str } ${ braces [ 1 ] } ` ;
1088
1090
}
1089
1091
1090
1092
function isBoolean ( arg ) {
0 commit comments