index.mjs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { isRegExp, isString } from '../../utils/validateTypes.mjs';
  2. import report from '../../utils/report.mjs';
  3. import ruleMessages from '../../utils/ruleMessages.mjs';
  4. import validateOptions from '../../utils/validateOptions.mjs';
  5. const ruleName = 'comment-pattern';
  6. const messages = ruleMessages(ruleName, {
  7. expected: (pattern) => `Expected comment to match pattern "${pattern}"`,
  8. });
  9. const meta = {
  10. url: 'https://stylelint.io/user-guide/rules/comment-pattern',
  11. };
  12. /** @type {import('stylelint').CoreRules[ruleName]} */
  13. const rule = (primary) => {
  14. return (root, result) => {
  15. const validOptions = validateOptions(result, ruleName, {
  16. actual: primary,
  17. possible: [isRegExp, isString],
  18. });
  19. if (!validOptions) {
  20. return;
  21. }
  22. const normalizedPattern = isString(primary) ? new RegExp(primary) : primary;
  23. root.walkComments((comment) => {
  24. const text = comment.text;
  25. if (normalizedPattern.test(text)) {
  26. return;
  27. }
  28. report({
  29. message: messages.expected,
  30. messageArgs: [primary],
  31. node: comment,
  32. result,
  33. ruleName,
  34. });
  35. });
  36. };
  37. };
  38. rule.ruleName = ruleName;
  39. rule.messages = messages;
  40. rule.meta = meta;
  41. export default rule;