All files / packages/sds-stylelint-config/src/plugins design-token-deprecated.ts

95% Statements 19/20
80% Branches 8/10
100% Functions 5/5
100% Lines 17/17

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              1x 1x                 12x 6x   5x 1x 1x                       1x   1x 5x 5x 5x   5x 5x   5x 6x 12x                
import stylelint from 'stylelint';
import valueParser from 'postcss-value-parser';
import type { Declaration } from 'postcss';
import metadata from '../metadata/metadata.js';
import { shouldValidate, formatDeprecatedWarning } from '../utils/index.js';
import type { RuleOptions } from '../types.js';
 
const { report, validateOptions } = stylelint.utils;
const ruleName = 'sds-stylelint-plugin/design-token-deprecated';
 
function processNode(
  node: any,
  decl: Declaration,
  result: stylelint.PostcssResult,
  privatePrefix: string,
  deprecated: Record<string, string>,
): void {
  if (node.type !== 'word') return;
  if (!shouldValidate(node.value, privatePrefix)) return;
 
  for (const [deprecatedKey, replacement] of Object.entries(deprecated)) {
    Eif (node.value.includes(deprecatedKey)) {
      report({
        ruleName,
        result,
        message: formatDeprecatedWarning(node.value, deprecatedKey, replacement),
        node: decl,
      });
    }
  }
}
 
type Rule = Parameters<typeof stylelint.createPlugin>[1];
 
const messages = stylelint.utils.ruleMessages(ruleName, {});
 
const deprecatedTokens = Object.assign(
  (primary: boolean, options?: RuleOptions) => (root: any, result: stylelint.PostcssResult) => {
    const valid = validateOptions(result, ruleName, { actual: primary, possible: [true] });
    Iif (!valid) return;
 
    const privatePrefix = options?.privateSyntax || metadata.privateSyntax[0];
    const deprecated = { ...metadata.deprecated, ...options?.deprecated };
 
    root.walkDecls((decl: Declaration) => {
      valueParser(decl.value).walk((node: any) => {
        processNode(node, decl, result, privatePrefix, deprecated);
      });
    });
  },
  { ruleName, messages },
) satisfies Rule;
 
export default stylelint.createPlugin(ruleName, deprecatedTokens);