index.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use strict';
  2. const declarationValueIndex = require('../../utils/declarationValueIndex');
  3. const findAnimationName = require('../../utils/findAnimationName');
  4. const { animationNameKeywords } = require('../../reference/keywords');
  5. const report = require('../../utils/report');
  6. const ruleMessages = require('../../utils/ruleMessages');
  7. const validateOptions = require('../../utils/validateOptions');
  8. const ruleName = 'no-unknown-animations';
  9. const messages = ruleMessages(ruleName, {
  10. rejected: (animationName) => `Unexpected unknown animation name "${animationName}"`,
  11. });
  12. const meta = {
  13. url: 'https://stylelint.io/user-guide/rules/no-unknown-animations',
  14. };
  15. /** @type {import('stylelint').Rule} */
  16. const rule = (primary) => {
  17. return (root, result) => {
  18. const validOptions = validateOptions(result, ruleName, { actual: primary });
  19. if (!validOptions) {
  20. return;
  21. }
  22. const declaredAnimations = new Set();
  23. root.walkAtRules(/(-(moz|webkit)-)?keyframes/i, (atRule) => {
  24. declaredAnimations.add(atRule.params);
  25. });
  26. root.walkDecls((decl) => {
  27. if (decl.prop.toLowerCase() === 'animation' || decl.prop.toLowerCase() === 'animation-name') {
  28. const animationNames = findAnimationName(decl.value);
  29. if (animationNames.length === 0) {
  30. return;
  31. }
  32. for (const animationNameNode of animationNames) {
  33. if (animationNameKeywords.has(animationNameNode.value.toLowerCase())) {
  34. continue;
  35. }
  36. if (declaredAnimations.has(animationNameNode.value)) {
  37. continue;
  38. }
  39. const begin = declarationValueIndex(decl);
  40. report({
  41. result,
  42. ruleName,
  43. message: messages.rejected(animationNameNode.value),
  44. node: decl,
  45. index: begin + animationNameNode.sourceIndex,
  46. endIndex: begin + animationNameNode.sourceEndIndex,
  47. });
  48. }
  49. }
  50. });
  51. };
  52. };
  53. rule.ruleName = ruleName;
  54. rule.messages = messages;
  55. rule.meta = meta;
  56. module.exports = rule;