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

0% Statements 0/37
0% Branches 0/35
0% Functions 0/3
0% Lines 0/35

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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78                                                                                                                                                           
const chalk = require('chalk');
const metadata = require('../metadata/metadata.js');
const { analyzeFuzzy, removeFromSet } = require('./search');
 
/**
 * Validate Category
 * @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 validateCategory = (obj) => {
  if (obj.validate.scope.isGlobal || obj.validate.scope.isShared) return { skip: true };
  const results = analyzeFuzzy(obj).filter((item) => item.word);
 
  if (obj.keys.category) {
    return { valid: true, received: obj.groups[obj.keys.category] };
  } else if (obj.fuzzyKeys.size > 0) {
    let valid;
    results.map((item) => {
      if (!item.lastKey && obj.keys.context === 2 && obj.keys.property === 4) {
        obj.keys.category = obj.keys.context + 1;
        obj.fuzzyKeys = removeFromSet(obj.fuzzyKeys, obj.keys.category);
        valid = {
          valid: false,
          expected: metadata.component.valid.category,
          received: obj.groups[obj.keys.category],
        };
      } else if (item.lastKey && results.length === 1) {
        obj.keys.category = obj.keys.context + 1;
        obj.fuzzyKeys = removeFromSet(obj.fuzzyKeys, obj.keys.category);
        valid = {
          valid: false,
          expected: metadata.component.valid.category,
          received: obj.groups[obj.keys.category],
        };
      }
      // check for element or variant
      else if (!obj.keys.category && item.lastKey && results.length === 2 && item.key !== 3) {
        obj.keys.category = obj.keys.context + 2;
        obj.fuzzyKeys = removeFromSet(obj.fuzzyKeys, obj.keys.category);
        valid = { valid: false, received: obj.groups[item.key] };
        obj.customMessage.push(
          `We received "${chalk.bold(
            obj.groups[item.key],
          )}" and have low confidence in determining the category. Possible values are ${chalk.bold(
            metadata.component.valid.category,
          )}.`,
        );
      } else if (!obj.keys.category && !item.lastKey && results.length === 2 && item.key !== 3) {
        obj.keys.category = obj.keys.context + 2;
        obj.fuzzyKeys = removeFromSet(obj.fuzzyKeys, obj.keys.category);
        valid = { valid: false, received: obj.groups[item.key] };
        obj.customMessage.push(
          `We received "${chalk.bold(
            obj.groups[item.key],
          )}" and have low confidence in determining the category. Possible values are ${chalk.bold(
            metadata.component.valid.category,
          )}.`,
        );
      } else if (!obj.keys.category && results.length >= 3 && item.key !== 3 && item.key !== 4) {
        obj.keys.category = obj.keys.context + 3;
        obj.fuzzyKeys = removeFromSet(obj.fuzzyKeys, obj.keys.category);
        valid = {
          valid: false,
          expected: metadata.component.valid.category,
          received: obj.groups[obj.keys.category],
        };
      }
    });
    return valid;
  } else {
    // This is a required key so throw an error if undefined
    return { valid: false, expected: metadata.component.valid.category };
  }
};
 
module.exports = validateCategory;