index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 'use strict';
  2. const atRuleParamIndex = require('../../utils/atRuleParamIndex');
  3. const isStandardSyntaxKeyframesName = require('../../utils/isStandardSyntaxKeyframesName');
  4. const report = require('../../utils/report');
  5. const ruleMessages = require('../../utils/ruleMessages');
  6. const validateOptions = require('../../utils/validateOptions');
  7. const { isRegExp, isString } = require('../../utils/validateTypes');
  8. const ruleName = 'keyframes-name-pattern';
  9. const messages = ruleMessages(ruleName, {
  10. expected: (keyframeName, pattern) => `Expected "${keyframeName}" to match pattern "${pattern}"`,
  11. });
  12. const meta = {
  13. url: 'https://stylelint.io/user-guide/rules/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 (!isStandardSyntaxKeyframesName(value)) {
  29. return;
  30. }
  31. if (regex.test(value)) {
  32. return;
  33. }
  34. const index = atRuleParamIndex(keyframesNode);
  35. const endIndex = index + value.length;
  36. report({
  37. index,
  38. endIndex,
  39. message: messages.expected,
  40. messageArgs: [value, primary],
  41. node: keyframesNode,
  42. ruleName,
  43. result,
  44. });
  45. });
  46. };
  47. };
  48. rule.ruleName = ruleName;
  49. rule.messages = messages;
  50. rule.meta = meta;
  51. module.exports = rule;