@@ -1973,6 +1973,88 @@ assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'");
1973
1973
) ;
1974
1974
} ) ;
1975
1975
1976
+ // Verify that classes are properly inspected.
1977
+ [
1978
+ /* eslint-disable spaced-comment, no-multi-spaces, brace-style */
1979
+ // The whitespace is intentional.
1980
+ [ class { } , '[class (anonymous)]' ] ,
1981
+ [ class extends Error { log ( ) { } } , '[class (anonymous) extends Error]' ] ,
1982
+ [ class A { constructor ( a ) { this . a = a ; } log ( ) { return this . a ; } } ,
1983
+ '[class A]' ] ,
1984
+ [ class
1985
+ // Random { // comments /* */ are part of the toString() result
1986
+ /* eslint-disable-next-line space-before-blocks */
1987
+ äß /**/ extends /*{*/ TypeError { } , '[class äß extends TypeError]' ] ,
1988
+ /* The whitespace and new line is intended! */
1989
+ // Foobar !!!
1990
+ [ class X extends /****/ Error
1991
+ // More comments
1992
+ { } , '[class X extends Error]' ]
1993
+ /* eslint-enable spaced-comment, no-multi-spaces, brace-style */
1994
+ ] . forEach ( ( [ clazz , string ] ) => {
1995
+ const inspected = util . inspect ( clazz ) ;
1996
+ assert . strictEqual ( inspected , string ) ;
1997
+ Object . defineProperty ( clazz , Symbol . toStringTag , {
1998
+ value : 'Woohoo'
1999
+ } ) ;
2000
+ const parts = inspected . slice ( 0 , - 1 ) . split ( ' ' ) ;
2001
+ const [ , name , ...rest ] = parts ;
2002
+ rest . unshift ( '[Woohoo]' ) ;
2003
+ if ( rest . length ) {
2004
+ rest [ rest . length - 1 ] += ']' ;
2005
+ }
2006
+ assert . strictEqual (
2007
+ util . inspect ( clazz ) ,
2008
+ [ '[class' , name , ...rest ] . join ( ' ' )
2009
+ ) ;
2010
+ if ( rest . length ) {
2011
+ rest [ rest . length - 1 ] = rest [ rest . length - 1 ] . slice ( 0 , - 1 ) ;
2012
+ rest . length = 1 ;
2013
+ }
2014
+ Object . setPrototypeOf ( clazz , null ) ;
2015
+ assert . strictEqual (
2016
+ util . inspect ( clazz ) ,
2017
+ [ '[class' , name , ...rest , 'extends [null prototype]]' ] . join ( ' ' )
2018
+ ) ;
2019
+ Object . defineProperty ( clazz , 'name' , { value : 'Foo' } ) ;
2020
+ const res = [ '[class' , 'Foo' , ...rest , 'extends [null prototype]]' ] . join ( ' ' ) ;
2021
+ assert . strictEqual ( util . inspect ( clazz ) , res ) ;
2022
+ clazz . foo = true ;
2023
+ assert . strictEqual ( util . inspect ( clazz ) , `${ res } { foo: true }` ) ;
2024
+ } ) ;
2025
+
2026
+ // "class" properties should not be detected as "class".
2027
+ {
2028
+ // eslint-disable-next-line space-before-function-paren
2029
+ let obj = { class ( ) { } } ;
2030
+ assert . strictEqual (
2031
+ util . inspect ( obj ) ,
2032
+ '{ class: [Function: class] }'
2033
+ ) ;
2034
+ obj = { class : ( ) => { } } ;
2035
+ assert . strictEqual (
2036
+ util . inspect ( obj ) ,
2037
+ '{ class: [Function: class] }'
2038
+ ) ;
2039
+ obj = { [ 'class Foo {}' ] ( ) { } } ;
2040
+ assert . strictEqual (
2041
+ util . inspect ( obj ) ,
2042
+ "{ 'class Foo {}': [Function: class Foo {}] }"
2043
+ ) ;
2044
+ function Foo ( ) { }
2045
+ Object . defineProperty ( Foo , 'toString' , { value : ( ) => 'class Foo {}' } ) ;
2046
+ assert . strictEqual (
2047
+ util . inspect ( Foo ) ,
2048
+ '[Function: Foo]'
2049
+ ) ;
2050
+ function fn ( ) { }
2051
+ Object . defineProperty ( fn , 'name' , { value : 'class Foo {}' } ) ;
2052
+ assert . strictEqual (
2053
+ util . inspect ( fn ) ,
2054
+ '[Function: class Foo {}]'
2055
+ ) ;
2056
+ }
2057
+
1976
2058
// Verify that throwing in valueOf and toString still produces nice results.
1977
2059
[
1978
2060
[ new String ( 55 ) , "[String: '55']" ] ,
0 commit comments