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

0% Statements 0/22
0% Branches 0/22
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 48 49 50 51                                                                                                     
const chalk = require('chalk');
const { analyzeFuzzy, removeFromSet } = require('../search.js');
 
// These are outliers that might be used as the last key in the group
const allowList = ['base', 'monospace', 'circle', 'pill'];
 
/**
 * Validate range
 * @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 or definition to skip if not found
 */
const validateGlobalRange = (obj) => {
  if (obj.validate.scope.isComponent || obj.validate.scope.isShared) return { skip: true };
  const results = analyzeFuzzy(obj).filter((item) => item.word);
 
  if (obj.fuzzyKeys.size > 0 && results.length > 0) {
    let valid;
    results.map((item) => {
      // Check if string parses to a number and its the last key in the group
      if (!isNaN(item.word) && item.lastKey) {
        obj.keys.range = item.key;
        obj.fuzzyKeys = removeFromSet(obj.fuzzyKeys, obj.keys.range);
        valid = { valid: true, received: obj.groups[item.key] };
      } else if (isNaN(item.word) && item.lastKey && !allowList.includes(item.word)) {
        // Check if string does not parse to a number and its the last key in the group, range is required to be the last key
        valid = { valid: false, received: obj.groups[item.key] };
        obj.customMessage.push(
          `We received "${chalk.bold(
            obj.groups[item.key],
          )}" and have determined you are not setting a range. Possible values are a ${chalk.bold(
            'numerical range',
          )}, starting at 1. The range value is required to be the last value in the custom property. If you are trying to pair a category and property, you may use a ${chalk.bold(
            'context',
          )} to define the semantics, such as ${chalk.bold(allowList)}, e.g., ${chalk.bold(
            '--slds-g-font-size-base',
          )} or ${chalk.bold('--slds-g-radius-border-circle')}.`,
        );
      } else if (isNaN(item.word) && item.lastKey && allowList.includes(item.word)) {
        // Check if string does not parse to a number and its the last key in the group, but if value is in allowList, then lets skip
        valid = { skip: true };
      }
    });
    return valid;
  } else {
    return { skip: true };
  }
};
 
module.exports = validateGlobalRange;