UNPKG

1.69 kBJavaScriptView Raw
1// @ts-nocheck
2
3'use strict';
4
5const declarationValueIndex = require('../../utils/declarationValueIndex');
6const findAnimationName = require('../../utils/findAnimationName');
7const keywordSets = require('../../reference/keywordSets');
8const report = require('../../utils/report');
9const ruleMessages = require('../../utils/ruleMessages');
10const validateOptions = require('../../utils/validateOptions');
11
12const ruleName = 'no-unknown-animations';
13
14const messages = ruleMessages(ruleName, {
15 rejected: (animationName) => `Unexpected unknown animation name "${animationName}"`,
16});
17
18function rule(actual) {
19 return (root, result) => {
20 const validOptions = validateOptions(result, ruleName, { actual });
21
22 if (!validOptions) {
23 return;
24 }
25
26 const declaredAnimations = new Set();
27
28 root.walkAtRules(/(-(moz|webkit)-)?keyframes/i, (atRule) => {
29 declaredAnimations.add(atRule.params);
30 });
31
32 root.walkDecls((decl) => {
33 if (decl.prop.toLowerCase() === 'animation' || decl.prop.toLowerCase() === 'animation-name') {
34 const animationNames = findAnimationName(decl.value);
35
36 if (animationNames.length === 0) {
37 return;
38 }
39
40 animationNames.forEach((animationNameNode) => {
41 if (keywordSets.animationNameKeywords.has(animationNameNode.value.toLowerCase())) {
42 return;
43 }
44
45 if (declaredAnimations.has(animationNameNode.value)) {
46 return;
47 }
48
49 report({
50 result,
51 ruleName,
52 message: messages.rejected(animationNameNode.value),
53 node: decl,
54 index: declarationValueIndex(decl) + animationNameNode.sourceIndex,
55 });
56 });
57 }
58 });
59 };
60}
61
62rule.ruleName = ruleName;
63rule.messages = messages;
64module.exports = rule;