All files / packages/sds-stylelint-config/src/plugins styling-hooks-reference-pattern.js

0% Statements 0/43
0% Branches 0/24
0% Functions 0/6
0% Lines 0/39

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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107                                                                                                                                                                                                                     
const stylelint = require('stylelint');
const { report, ruleMessages, validateOptions } = stylelint.utils;
const valueParser = require('postcss-value-parser');
const chalk = require('chalk');
const metadata = require('../metadata/metadata.js');
const { validateNs, isValidCustomProperty } = require('../utils');
 
const ruleName = 'sds-stylelint-plugin/styling-hooks-reference-pattern';
 
// Custom message for report
const messages = ruleMessages(ruleName, {
  expected: (prop, errors) => {
    const errorList = errors
      .map((error, index) => {
        return `${index + 1}.) ${error}`;
      })
      .join(' ');
    return `${errors.length} error(s) found on ${chalk.cyan(prop)} fails naming pattern. ${errorList}`;
  },
});
 
const referenceHooks = (primary, options) => {
  return (root, result) => {
    const validOptions = stylelint.utils.validateOptions(result, ruleName, {
      actual: primary,
      possible: [true],
    });
    if (!validOptions) return;
 
    root.walkDecls((decl) => {
      const parsedValue = valueParser(decl.value);
      const privateSyntax = options?.privateSyntax || metadata.privateSyntax;
 
      parsedValue.walk((node) => {
        if (node.type !== 'word') return;
        if (!isValidCustomProperty({ value: node.value, privateSyntax })) return;
 
        // Break words of custom prop into groups
        const groups = node.value.match(/\b[_a-z0-9]+\b/g);
 
        // Only process reference scope tokens
        if (groups[1] !== 'r') return;
 
        const errors = [];
 
        // Validate namespace
        const nsValidation = validateNs(groups[0], privateSyntax);
        if (!nsValidation.valid) {
          errors.push(
            `Expected ${chalk.bold('namespace')} value(s) of "${chalk.bold(
              nsValidation.expected,
            )}" but received "${chalk.redBright(groups[0])}".`,
          );
        }
 
        // Validate category (should be 'color')
        if (!metadata.reference.valid.category.includes(groups[2])) {
          errors.push(
            `Expected ${chalk.bold('category')} value(s) of "${chalk.bold(
              metadata.reference.valid.category.join(','),
            )}" but received "${chalk.redBright(groups[2])}".`,
          );
        }
 
        // Validate role (should be 'brand')
        if (!metadata.reference.valid.role.includes(groups[3])) {
          errors.push(
            `Expected ${chalk.bold('role')} value(s) of "${chalk.bold(
              metadata.reference.valid.role.join(','),
            )}" but received "${chalk.redBright(groups[3])}".`,
          );
        }
 
        // Validate range (should be a number)
        if (groups[4] && isNaN(groups[4])) {
          errors.push(
            `Expected ${chalk.bold('range')} to be a numerical value but received "${chalk.redBright(
              groups[4],
            )}".`,
          );
        }
 
        // Validate structure (should have exactly 5 parts)
        if (groups.length !== 5) {
          errors.push(
            `Expected reference token to have 5 parts (namespace-scope-category-role-range) but received ${groups.length} parts.`,
          );
        }
 
        // Report errors if any
        if (errors.length > 0) {
          report({
            ruleName,
            result,
            message: messages.expected(node.value, errors),
            node: decl,
          });
        }
      });
    });
  };
};
 
referenceHooks.ruleName = ruleName;
 
module.exports = stylelint.createPlugin(ruleName, referenceHooks);