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