index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. 'use strict';
  2. const report = require('../../utils/report');
  3. const ruleMessages = require('../../utils/ruleMessages');
  4. const validateOptions = require('../../utils/validateOptions');
  5. const { isRegExp, isString } = require('../../utils/validateTypes');
  6. const ruleName = 'comment-pattern';
  7. const messages = ruleMessages(ruleName, {
  8. expected: (pattern) => `Expected comment to match pattern "${pattern}"`,
  9. });
  10. const meta = {
  11. url: 'https://stylelint.io/user-guide/rules/list/comment-pattern',
  12. };
  13. /** @type {import('stylelint').Rule} */
  14. const rule = (primary) => {
  15. return (root, result) => {
  16. const validOptions = validateOptions(result, ruleName, {
  17. actual: primary,
  18. possible: [isRegExp, isString],
  19. });
  20. if (!validOptions) {
  21. return;
  22. }
  23. const normalizedPattern = isString(primary) ? new RegExp(primary) : primary;
  24. root.walkComments((comment) => {
  25. const text = comment.text;
  26. if (normalizedPattern.test(text)) {
  27. return;
  28. }
  29. report({
  30. message: messages.expected(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. module.exports = rule;