@humanwhocodes/config-array
Advanced tools
Comparing version 0.11.14 to 0.12.1
99
api.js
@@ -160,2 +160,66 @@ 'use strict'; | ||
/** | ||
* Wrapper error for config validation errors that adds a name to the front of the | ||
* error message. | ||
*/ | ||
class ConfigError extends Error { | ||
/** | ||
* Creates a new instance. | ||
* @param {string} name The config object name causing the error. | ||
* @param {number} index The index of the config object in the array. | ||
* @param {Error} source The source error. | ||
*/ | ||
constructor(name, index, source) { | ||
super(`Config ${name}: ${source.message}`, { cause: source }); | ||
// copy over custom properties that aren't represented | ||
for (const key of Object.keys(source)) { | ||
if (!(key in this)) { | ||
this[key] = source[key]; | ||
} | ||
} | ||
/** | ||
* The name of the error. | ||
* @type {string} | ||
* @readonly | ||
*/ | ||
this.name = 'ConfigError'; | ||
/** | ||
* The index of the config object in the array. | ||
* @type {number} | ||
* @readonly | ||
*/ | ||
this.index = index; | ||
} | ||
} | ||
/** | ||
* Gets the name of a config object. | ||
* @param {object} config The config object to get the name of. | ||
* @returns {string} The name of the config object. | ||
*/ | ||
function getConfigName(config) { | ||
if (typeof config.name === 'string' && config.name) { | ||
return `"${config.name}"`; | ||
} | ||
return '(unnamed)'; | ||
} | ||
/** | ||
* Rethrows a config error with additional information about the config object. | ||
* @param {object} config The config object to get the name of. | ||
* @param {number} index The index of the config object in the array. | ||
* @param {Error} error The error to rethrow. | ||
* @throws {ConfigError} When the error is rethrown for a config. | ||
*/ | ||
function rethrowConfigError(config, index, error) { | ||
const configName = getConfigName(config); | ||
throw new ConfigError(configName, index, error); | ||
} | ||
/** | ||
* Shorthand for checking if a value is a string. | ||
@@ -170,19 +234,30 @@ * @param {any} value The value to check. | ||
/** | ||
* Asserts that the files and ignores keys of a config object are valid as per base schema. | ||
* @param {object} config The config object to check. | ||
* Creates a function that asserts that the files and ignores keys | ||
* of a config object are valid as per base schema. | ||
* @param {Object} config The config object to check. | ||
* @param {number} index The index of the config object in the array. | ||
* @returns {void} | ||
* @throws {TypeError} If the files and ignores keys of a config object are not valid. | ||
* @throws {ConfigError} If the files and ignores keys of a config object are not valid. | ||
*/ | ||
function assertValidFilesAndIgnores(config) { | ||
function assertValidFilesAndIgnores(config, index) { | ||
if (!config || typeof config !== 'object') { | ||
return; | ||
} | ||
const validateConfig = { }; | ||
if ('files' in config) { | ||
validateConfig.files = config.files; | ||
} | ||
if ('ignores' in config) { | ||
validateConfig.ignores = config.ignores; | ||
} | ||
FILES_AND_IGNORES_SCHEMA.validate(validateConfig); | ||
try { | ||
FILES_AND_IGNORES_SCHEMA.validate(validateConfig); | ||
} catch (validationError) { | ||
rethrowConfigError(config, index, validationError); | ||
} | ||
} | ||
@@ -516,3 +591,3 @@ | ||
* @property isNormalized | ||
* @type boolean | ||
* @type {boolean} | ||
* @private | ||
@@ -536,3 +611,3 @@ */ | ||
* @property basePath | ||
* @type string | ||
* @type {string} | ||
*/ | ||
@@ -546,3 +621,3 @@ this.basePath = basePath; | ||
* @property configTypes | ||
* @type Array<string> | ||
* @type {Array<string>} | ||
*/ | ||
@@ -554,3 +629,3 @@ this.extraConfigTypes = Object.freeze([...extraConfigTypes]); | ||
* @property configCache | ||
* @type Map | ||
* @type {Map<string, Object>} | ||
* @private | ||
@@ -941,3 +1016,7 @@ */ | ||
finalConfig = matchingConfigIndices.reduce((result, index) => { | ||
return this[ConfigArraySymbol.schema].merge(result, this[index]); | ||
try { | ||
return this[ConfigArraySymbol.schema].merge(result, this[index]); | ||
} catch (validationError) { | ||
rethrowConfigError(this[index], index, validationError); | ||
} | ||
}, {}, this); | ||
@@ -944,0 +1023,0 @@ |
{ | ||
"name": "@humanwhocodes/config-array", | ||
"version": "0.11.14", | ||
"version": "0.12.1", | ||
"description": "Glob-based configuration matching.", | ||
@@ -8,3 +8,5 @@ "author": "Nicholas C. Zakas", | ||
"files": [ | ||
"api.js" | ||
"api.js", | ||
"LICENSE", | ||
"README.md" | ||
], | ||
@@ -46,3 +48,3 @@ "repository": { | ||
"dependencies": { | ||
"@humanwhocodes/object-schema": "^2.0.2", | ||
"@humanwhocodes/object-schema": "^2.0.3", | ||
"debug": "^4.3.1", | ||
@@ -49,0 +51,0 @@ "minimatch": "^3.0.5" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
57666
902