All files / packages/sds-stylelint-config/src/utils/global validateAttribute.js

0% Statements 0/22
0% Branches 0/16
0% Functions 0/3
0% Lines 0/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47                                                                                             
const chalk = require('chalk');
const metadata = require('../../metadata/metadata.js');
const { analyzeFuzzy, removeFromSet } = require('../search.js');
 
/**
 * Validate Attribute
 * @param {Object} obj The object from the first Promise that has valuable information about the custom prop
 * @todo Skip scope validation based on stylelint option
 * @returns {Object} Validation object
 */
const validateGlobalAttribute = (obj) => {
  if (obj.validate.scope.isComponent || obj.validate.scope.isShared) return { skip: true };
  const results = analyzeFuzzy(obj).filter((item) => item.word);
 
  if (obj.keys.attribute) {
    return { valid: true, received: obj.groups[obj.keys.attribute] };
  }
  // We have a previous customMessage, meaning we have already failed a validation
  else if (obj.customMessage.length > 0) {
    return { skip: true };
    // We have unknown keys, lets check if they are optional
  } else if (obj.fuzzyKeys.size > 0 && results.length > 0) {
    let valid;
    results.map((item) => {
      if (obj.keys.context && obj.keys.context + 2 === obj.keys.range) {
        obj.keys.attribute = obj.keys.context + 1;
        obj.fuzzyKeys = removeFromSet(obj.fuzzyKeys, obj.keys.attribute);
        valid = {
          valid: false,
          expected: metadata.global.valid.attribute,
          received: item.word,
        };
      }
      // nothing so far
      else {
        valid = { skip: true };
      }
    });
    return valid;
  } else {
    // This is an optional key so skip
    return { skip: true };
  }
};
 
module.exports = validateGlobalAttribute;