@@ -50,13 +50,19 @@ class ConfigError extends Error {
5050 * @param {number } index The index of the config object in the array.
5151 * @param {Error } source The source error.
5252 */
53- constructor ( name , index , source ) {
54- super ( `Config ${ name } : ${ source . message } ` , { cause : source } ) ;
53+ constructor ( name , index , { cause, message } ) {
54+
55+
56+ const finalMessage = message || cause . message ;
57+
58+ super ( `Config ${ name } : ${ finalMessage } ` , { cause } ) ;
5559
5660 // copy over custom properties that aren't represented
57- for ( const key of Object . keys ( source ) ) {
58- if ( ! ( key in this ) ) {
59- this [ key ] = source [ key ] ;
61+ if ( cause ) {
62+ for ( const key of Object . keys ( cause ) ) {
63+ if ( ! ( key in this ) ) {
64+ this [ key ] = cause [ key ] ;
65+ }
6066 }
6167 }
6268
@@ -82,14 +88,13 @@ class ConfigError extends Error {
8288 * @returns {string } The name of the config object.
8389 */
8490function getConfigName ( config ) {
85- if ( typeof config . name === 'string' && config . name ) {
91+ if ( config && typeof config . name === 'string' && config . name ) {
8692 return `"${ config . name } "` ;
8793 }
8894
8995 return '(unnamed)' ;
9096}
9197
92-
9398/**
9499 * Rethrows a config error with additional information about the config object.
95100 * @param {object } config The config object to get the name of.
@@ -112,19 +117,28 @@ function isString(value) {
112117}
113118
114119/**
115- * Creates a function that asserts that the files and ignores keys
116- * of a config object are valid as per base schema.
120+ * Creates a function that asserts that the config is valid
121+ * during normalization. This checks that the config is not nullish
122+ * and that files and ignores keys of a config object are valid as per base schema.
117123 * @param {Object } config The config object to check.
118124 * @param {number } index The index of the config object in the array.
119125 * @returns {void }
120126 * @throws {ConfigError } If the files and ignores keys of a config object are not valid.
121127 */
122- function assertValidFilesAndIgnores ( config , index ) {
128+ function assertValidBaseConfig ( config , index ) {
123129
124- if ( ! config || typeof config !== 'object' ) {
125- return ;
130+ if ( config === null ) {
131+ throw new ConfigError ( getConfigName ( config ) , index , { message : 'Unexpected null config.' } ) ;
126132 }
127-
133+
134+ if ( config === undefined ) {
135+ throw new ConfigError ( getConfigName ( config ) , index , { message : 'Unexpected undefined config.' } ) ;
136+ }
137+
138+ if ( typeof config !== 'object' ) {
139+ throw new ConfigError ( getConfigName ( config ) , index , { message : 'Unexpected non-object config.' } ) ;
140+ }
141+
128142 const validateConfig = { } ;
129143
130144 if ( 'files' in config ) {
@@ -138,7 +152,7 @@ function assertValidFilesAndIgnores(config, index) {
138152 try {
139153 FILES_AND_IGNORES_SCHEMA . validate ( validateConfig ) ;
140154 } catch ( validationError ) {
141- rethrowConfigError ( config , index , validationError ) ;
155+ rethrowConfigError ( config , index , { cause : validationError } ) ;
142156 }
143157}
144158
@@ -634,7 +648,7 @@ export class ConfigArray extends Array {
634648 const normalizedConfigs = await normalize ( this , context , this . extraConfigTypes ) ;
635649 this . length = 0 ;
636650 this . push ( ...normalizedConfigs . map ( this [ ConfigArraySymbol . preprocessConfig ] . bind ( this ) ) ) ;
637- this . forEach ( assertValidFilesAndIgnores ) ;
651+ this . forEach ( assertValidBaseConfig ) ;
638652 this [ ConfigArraySymbol . isNormalized ] = true ;
639653
640654 // prevent further changes
@@ -656,7 +670,7 @@ export class ConfigArray extends Array {
656670 const normalizedConfigs = normalizeSync ( this , context , this . extraConfigTypes ) ;
657671 this . length = 0 ;
658672 this . push ( ...normalizedConfigs . map ( this [ ConfigArraySymbol . preprocessConfig ] . bind ( this ) ) ) ;
659- this . forEach ( assertValidFilesAndIgnores ) ;
673+ this . forEach ( assertValidBaseConfig ) ;
660674 this [ ConfigArraySymbol . isNormalized ] = true ;
661675
662676 // prevent further changes
@@ -892,7 +906,7 @@ export class ConfigArray extends Array {
892906 try {
893907 return this [ ConfigArraySymbol . schema ] . merge ( result , this [ index ] ) ;
894908 } catch ( validationError ) {
895- rethrowConfigError ( this [ index ] , index , validationError ) ;
909+ rethrowConfigError ( this [ index ] , index , { cause : validationError } ) ;
896910 }
897911 } , { } , this ) ;
898912
0 commit comments