index.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict';
  2. const atRuleParamIndex = require('../../utils/atRuleParamIndex');
  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 = 'keyframes-name-pattern';
  8. const messages = ruleMessages(ruleName, {
  9. expected: (keyframeName, pattern) =>
  10. `Expected keyframe name "${keyframeName}" to match pattern "${pattern}"`,
  11. });
  12. const meta = {
  13. url: 'https://stylelint.io/user-guide/rules/list/keyframes-name-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 regex = isString(primary) ? new RegExp(primary) : primary;
  26. root.walkAtRules(/keyframes/i, (keyframesNode) => {
  27. const value = keyframesNode.params;
  28. if (regex.test(value)) {
  29. return;
  30. }
  31. report({
  32. index: atRuleParamIndex(keyframesNode),
  33. message: messages.expected(value, primary),
  34. node: keyframesNode,
  35. ruleName,
  36. result,
  37. });
  38. });
  39. };
  40. };
  41. rule.ruleName = ruleName;
  42. rule.messages = messages;
  43. rule.meta = meta;
  44. module.exports = rule;