65 lines
1.6 KiB
JavaScript
65 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
|
|
const parseSelector = require('../../utils/parseSelector');
|
|
const report = require('../../utils/report');
|
|
const ruleMessages = require('../../utils/ruleMessages');
|
|
const validateOptions = require('../../utils/validateOptions');
|
|
const { isRegExp, isString } = require('../../utils/validateTypes');
|
|
|
|
const ruleName = 'selector-id-pattern';
|
|
|
|
const messages = ruleMessages(ruleName, {
|
|
expected: (selector, pattern) => `Expected "${selector}" to match pattern "${pattern}"`,
|
|
});
|
|
|
|
const meta = {
|
|
url: 'https://stylelint.io/user-guide/rules/selector-id-pattern',
|
|
};
|
|
|
|
/** @type {import('stylelint').Rule<string | RegExp>} */
|
|
const rule = (primary) => {
|
|
return (root, result) => {
|
|
const validOptions = validateOptions(result, ruleName, {
|
|
actual: primary,
|
|
possible: [isRegExp, isString],
|
|
});
|
|
|
|
if (!validOptions) {
|
|
return;
|
|
}
|
|
|
|
const normalizedPattern = isString(primary) ? new RegExp(primary) : primary;
|
|
|
|
root.walkRules(/#/, (ruleNode) => {
|
|
if (!isStandardSyntaxRule(ruleNode)) {
|
|
return;
|
|
}
|
|
|
|
parseSelector(ruleNode.selector, result, ruleNode, (fullSelector) => {
|
|
fullSelector.walkIds((selectorNode) => {
|
|
if (normalizedPattern.test(selectorNode.value)) {
|
|
return;
|
|
}
|
|
|
|
const selector = String(selectorNode);
|
|
|
|
report({
|
|
result,
|
|
ruleName,
|
|
message: messages.expected,
|
|
messageArgs: [selector, primary],
|
|
node: ruleNode,
|
|
word: selector,
|
|
});
|
|
});
|
|
});
|
|
});
|
|
};
|
|
};
|
|
|
|
rule.ruleName = ruleName;
|
|
rule.messages = messages;
|
|
rule.meta = meta;
|
|
module.exports = rule;
|