All files / stylelint-plugin-craftsmanlint/src/rules/no-hardcoded-values index.ts

96.42% Statements 27/28
66.66% Branches 4/6
100% Functions 7/7
96.29% Lines 26/27

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                2x                 2x 2x   7x 7x 7x     2x                     35x     2x 6x 6x       6x       6x     6x 6x 8x     6x 27x 27x   27x 7x 7x                     2x 2x 2x   2x    
/**
 * Copyright (c) 2022-present, Matti Bar-Zeev.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */
 
/* eslint-disable @pedalboard/craftsmanlint/no-namespace-imports */
import stylelint from 'stylelint';
import type * as PostCSS from 'postcss';
 
type ValueToTokenMap = Record<string, string | string[]>;
 
type PrimaryOption = {
    valueToToken: ValueToTokenMap;
};
 
const ruleName = 'stylelint-plugin-craftsmanlint/no-hardcoded-values';
const messages = stylelint.utils.ruleMessages(ruleName, {
    expected: (...args) => {
        const [value, token] = args;
        const tokenDisplay = Array.isArray(token) ? token.join(' or ') : token;
        return `Hard-coded value '${value}' detected. Replace it with the token ${tokenDisplay}.`;
    },
});
const meta = {
    url: 'https://github.com/mbarzeev/pedalboard/blob/master/packages/stylelint-plugin-craftsmanlint/README.md',
};
 
/**
 * Normalizes a CSS value for comparison by:
 * - Trimming whitespace
 * - Converting to lowercase
 * - Removing quotes if present
 */
function normalizeValue(value: string): string {
    return value.trim().toLowerCase().replace(/^['"]|['"]$/g, '');
}
 
const ruleFunction = (primaryOption: PrimaryOption) => {
    return (postcssRoot: PostCSS.Root, postcssResult: stylelint.PostcssResult) => {
        const validOptions = stylelint.utils.validateOptions(postcssResult, ruleName, {
            actual: null,
        });
 
        Iif (!validOptions) {
            return;
        }
 
        const {valueToToken} = primaryOption;
 
        // Build a normalized map for faster lookups
        const normalizedMap = new Map<string, {original: string; token: string | string[]}>();
        Object.entries(valueToToken).forEach(([value, token]) => {
            normalizedMap.set(normalizeValue(value), {original: value, token});
        });
 
        postcssRoot.walkDecls((decl: PostCSS.Declaration) => {
            const normalizedPart = normalizeValue(decl.value);
            const match = normalizedMap.get(normalizedPart);
 
            if (match) {
                const tokenDisplay = Array.isArray(match.token) ? match.token.join(' or ') : match.token;
                stylelint.utils.report({
                    ruleName,
                    result: postcssResult,
                    message: messages.expected(match.original, tokenDisplay),
                    node: decl,
                });
            }
        });
    };
};
 
ruleFunction.ruleName = ruleName;
ruleFunction.messages = messages;
ruleFunction.meta = meta;
 
export default ruleFunction;