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

0% Statements 0/23
0% Branches 0/11
0% Functions 0/4
0% Lines 0/22

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                                                                                                   
const stylelint = require('stylelint');
const valueParser = require('postcss-value-parser');
const chalk = require('chalk');
const metadata = require('../metadata/metadata.js');
const { isValidCustomProperty } = require('../utils');
 
const ruleName = 'sds-stylelint-plugin/styling-hooks-deprecated';
 
const deprecatedHooks = (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 && options.privateSyntax) || metadata.privateSyntax;
 
      parsedValue.walk(async (node) => {
        if (node.type !== 'word') return;
        if (isValidCustomProperty({ value: node.value, privateSyntax })) {
          // Throw report if deprecated items show up
          for (const deprecated of Object.keys(metadata.deprecated)) {
            if (node.value.includes(deprecated)) {
              stylelint.utils.report({
                ruleName,
                result,
                message: `${chalk.black.yellowBright('Deprecated Warning')} The custom property ${chalk.cyan(
                  node.value,
                )} includes a deprecated value of "${chalk.bold(
                  deprecated,
                )}", please use "${chalk.greenBright(metadata.deprecated[deprecated])}" instead.`,
                node: decl,
              });
            }
          }
        }
      });
    });
  };
};
 
deprecatedHooks.ruleName = ruleName;
 
module.exports = stylelint.createPlugin(ruleName, deprecatedHooks);