index.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. 'use strict';
  2. const declarationValueIndex = require('../../utils/declarationValueIndex');
  3. const findAnimationName = require('../../utils/findAnimationName');
  4. const keywordSets = require('../../reference/keywordSets');
  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/list/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 (keywordSets.animationNameKeywords.has(animationNameNode.value.toLowerCase())) {
  34. continue;
  35. }
  36. if (declaredAnimations.has(animationNameNode.value)) {
  37. continue;
  38. }
  39. report({
  40. result,
  41. ruleName,
  42. message: messages.rejected(animationNameNode.value),
  43. node: decl,
  44. index: declarationValueIndex(decl) + animationNameNode.sourceIndex,
  45. });
  46. }
  47. }
  48. });
  49. };
  50. };
  51. rule.ruleName = ruleName;
  52. rule.messages = messages;
  53. rule.meta = meta;
  54. module.exports = rule;