function rule()

in uui-build/linting/stylelintCustomRules/rules/themeTokensValidation.js [104:180]


function rule(primaryOptions, secondaryOptions) {
    return async (root, result) => {
        const isRuleConfigValid = validateOptions(
            result,
            ruleName,
            { actual: primaryOptions },
            {
                actual: secondaryOptions,
                possible: {
                    ignoredUnknownVars: [isString],
                    ignoredRedeclaredVars: [isString],
                },
                optional: true,
            },
        );
        if (!isRuleConfigValid) {
            return;
        }

        const srcFullPath = result.opts.from;
        let info;
        try {
            info = await getCustomPropsInfo(srcFullPath);
        } catch (err) {
            report({
                message: messages.reportCantCompileScss(srcFullPath, err?.message),
                node: root,
                result,
                ruleName,
            });
        }

        if (info) {
            root.walkDecls((decl) => {
                if (decl.prop.startsWith('--')) {
                    const isIgnored = isIgnoredRedeclaredVar({ secondaryOptions, varToCheck: decl.prop });
                    if (isIgnored) {
                        return;
                    }
                    if (info.declaredInRootThemeSelectorMoreThanOnce.has(decl.prop)) {
                        if (!isDeclInRootThemeMixinScope(decl)) {
                            // it's OK to redeclare anywhere but directly in root selector scope
                            return;
                        }
                        report({
                            message: messages.reportDoubleDeclarationOfCssProp(decl.prop),
                            node: decl,
                            word: decl.prop,
                            result,
                            ruleName,
                        });
                    }
                }

                const usedCustomProps = getReferencedCustomPropsFromDecl(decl);
                if (usedCustomProps.size) {
                    usedCustomProps.forEach((p) => {
                        const isIgnored = isIgnoredUnknownVar({ secondaryOptions, varToCheck: p });
                        if (isIgnored) {
                            return;
                        }

                        if (info.usedButNotDeclared.has(p)) {
                            report({
                                message: messages.reportUnknownVar(p),
                                node: decl,
                                word: p,
                                result,
                                ruleName,
                            });
                        }
                    });
                }
            });
        }
    };
}