Skip to content

Commit

Permalink
feat: validate var und func options for optional hash
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyOGo committed Sep 6, 2020
1 parent 05dbacd commit 873ab5d
Showing 1 changed file with 52 additions and 13 deletions.
65 changes: 52 additions & 13 deletions src/lib/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,32 @@ function validProperties(actual) {
|| (Array.isArray(actual) && actual.every((item) => isNumberOrString(item)))
}

/**
* Validate optional hash keyword config.
*
* @param {object} actual - A keyword config.
*
* @returns {boolean} - Returns `true` if hash keyword config is valid, else `false`.
*/
function validHash(actual) {
if (typeof actual !== 'object') return false

return Object.keys(actual).every((key) => validProperties(actual[key]))
}

/**
* Validate optional boolean hash variable/function config.
*
* @param {object} actual - A variable/function config.
*
* @returns {boolean} - Returns `true` if hash variable/function config is valid, else `false`.
*/
function validBooleanHash(actual) {
if (typeof actual !== 'object') return false

return Object.keys(actual).every((key) => typeof actual[key] === 'boolean')
}

/**
* Validate optional secondary options of stylelint plugin config.
*
Expand All @@ -42,10 +68,12 @@ function validOptions(actual) {

if ('ignoreVariables' in actual
&& typeof actual.ignoreVariables !== 'boolean'
&& !validBooleanHash(actual.ignoreVariables)
&& actual.ignoreVariables !== null) return false

if ('ignoreFunctions' in actual
&& typeof actual.ignoreFunctions !== 'boolean'
&& !validBooleanHash(actual.ignoreFunctions)
&& actual.ignoreFunctions !== null) return false

if ('severity' in actual
Expand Down Expand Up @@ -84,19 +112,6 @@ function validOptions(actual) {
return true
}

/**
* Validate optional hash keyword config.
*
* @param {object} actual - A keyword config.
*
* @returns {boolean} - Returns `true` if hash keyword config is valid, else `false`.
*/
function validHash(actual) {
if (typeof actual !== 'object') return false

return Object.keys(actual).every((key) => validProperties(actual[key]))
}

/**
* Build expected message for stylelint report.
*
Expand Down Expand Up @@ -162,6 +177,29 @@ function getTypes(config, property) {
return types
}

/**
* Get the correct ignored variable or function for a specific CSS declaration's property
* out of a complex `ignoreVariablesOrFunctions` config hash or boolean.
*
* @param {boolean|object} ignoreVariablesOrFunctions - The variables or functions to ignore.
* @param {string} property - The specific CSS declaration's property of the current iteration.
*
* @returns {boolean} - Returns ignored variable or function for a specific CSS property.
*/
function getIgnoredVariablesOrFunctions(ignoreVariablesOrFunctions, property) {
const type = typeof ignoreVariablesOrFunctions

if (type === 'boolean') {
return ignoreVariablesOrFunctions
}

if (type === 'object' && ignoreVariablesOrFunctions && {}.hasOwnProperty.call(ignoreVariablesOrFunctions, property)) {
return ignoreVariablesOrFunctions[property]
}

return !!ignoreVariablesOrFunctions
}

/**
* Get the correct ignored keywords for a specific CSS declaration's property
* out of a complex `ignoreKeywords` config hash or array.
Expand Down Expand Up @@ -242,6 +280,7 @@ export {
validOptions,
expected,
getTypes,
getIgnoredVariablesOrFunctions,
getIgnoredKeywords,
getIgnoredValues,
getAutoFixFunc,
Expand Down

0 comments on commit 873ab5d

Please sign in to comment.