123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- '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: (selectorValue, pattern) =>
- `Expected ID selector "#${selectorValue}" to match pattern "${pattern}"`,
- });
- const meta = {
- url: 'https://stylelint.io/user-guide/rules/list/selector-id-pattern',
- };
- /** @type {import('stylelint').Rule} */
- const rule = (primary) => {
- return (root, result) => {
- const validOptions = validateOptions(result, ruleName, {
- actual: primary,
- possible: [isRegExp, isString],
- });
- if (!validOptions) {
- return;
- }
- /** @type {RegExp} */
- const normalizedPattern = isString(primary) ? new RegExp(primary) : primary;
- root.walkRules((ruleNode) => {
- if (!isStandardSyntaxRule(ruleNode)) {
- return;
- }
- const selector = ruleNode.selector;
- parseSelector(selector, result, ruleNode, (fullSelector) => {
- fullSelector.walk((selectorNode) => {
- if (selectorNode.type !== 'id') {
- return;
- }
- const value = selectorNode.value;
- const sourceIndex = selectorNode.sourceIndex;
- if (normalizedPattern.test(value)) {
- return;
- }
- report({
- result,
- ruleName,
- message: messages.expected(value, primary),
- node: ruleNode,
- index: sourceIndex,
- });
- });
- });
- });
- };
- };
- rule.ruleName = ruleName;
- rule.messages = messages;
- rule.meta = meta;
- module.exports = rule;
|