UNPKG

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