index.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
  3. const report = require('../../utils/report');
  4. const ruleMessages = require('../../utils/ruleMessages');
  5. const validateOptions = require('../../utils/validateOptions');
  6. const { isRegExp, isString } = require('../../utils/validateTypes');
  7. const ruleName = 'selector-nested-pattern';
  8. const messages = ruleMessages(ruleName, {
  9. expected: (selector, pattern) =>
  10. `Expected nested selector "${selector}" to match pattern "${pattern}"`,
  11. });
  12. const meta = {
  13. url: 'https://stylelint.io/user-guide/rules/list/selector-nested-pattern',
  14. };
  15. /** @type {import('stylelint').Rule} */
  16. const rule = (primary) => {
  17. return (root, result) => {
  18. const validOptions = validateOptions(result, ruleName, {
  19. actual: primary,
  20. possible: [isRegExp, isString],
  21. });
  22. if (!validOptions) {
  23. return;
  24. }
  25. const normalizedPattern = isString(primary) ? new RegExp(primary) : primary;
  26. root.walkRules((ruleNode) => {
  27. if (ruleNode.parent && ruleNode.parent.type !== 'rule') {
  28. return;
  29. }
  30. if (!isStandardSyntaxRule(ruleNode)) {
  31. return;
  32. }
  33. const selector = ruleNode.selector;
  34. if (normalizedPattern.test(selector)) {
  35. return;
  36. }
  37. report({
  38. result,
  39. ruleName,
  40. message: messages.expected(selector, primary),
  41. node: ruleNode,
  42. });
  43. });
  44. };
  45. };
  46. rule.ruleName = ruleName;
  47. rule.messages = messages;
  48. rule.meta = meta;
  49. module.exports = rule;