function valueValidator()

in plugins/lib/decl-validator.js [83:151]


  function valueValidator({expects, values, replacements, singular = false}) {
    const matches = anymatch(values)
    return function validate({prop, value}, nested) {
      if (matches(value)) {
        return {
          valid: true,
          errors: [],
          fixable: false,
          replacement: undefined
        }
      } else if (replacements[value]) {
        let replacement = value
        do {
          replacement = replacements[replacement]
        } while (replacements[replacement])
        return {
          valid: false,
          errors: [{expects, prop, value, replacement}],
          fixable: true,
          replacement
        }
      } else {
        if (nested || singular) {
          return {
            valid: false,
            errors: [{expects, prop, value}],
            fixable: false,
            replacement: undefined
          }
        }

        const parsed = valueParser(value)
        const validations = parsed.nodes
          .map((node, index) => Object.assign(node, {index}))
          .filter(node => !SKIP_VALUE_NODE_TYPES.has(node.type))
          .map(node => {
            const validation = validate({prop, value: valueParser.stringify(node)}, true)
            validation.index = node.index
            return validation
          })

        const valid = validations.every(v => v.valid)
        if (valid) {
          return {valid, errors: [], fixable: false, replacement: undefined}
        }

        const fixable = validations.some(v => v.fixable)
        const errors = validations.reduce((list, v) => list.concat(v.errors), [])

        let replacement = undefined
        for (const validation of validations) {
          if (fixable && validation.replacement) {
            parsed.nodes[validation.index] = {type: 'word', value: validation.replacement}
          }
        }

        if (fixable) {
          replacement = valueParser.stringify(parsed)
        }

        return {
          valid,
          fixable,
          errors,
          replacement
        }
      }
    }
  }