-
-
Notifications
You must be signed in to change notification settings - Fork 929
/
index.js
142 lines (116 loc) · 3.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// @ts-nocheck
'use strict';
const _ = require('lodash');
const declarationValueIndex = require('../../utils/declarationValueIndex');
const keywordSets = require('../../reference/keywordSets');
const optionsMatches = require('../../utils/optionsMatches');
const postcss = require('postcss');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const valueParser = require('postcss-value-parser');
const vendor = require('../../utils/vendor');
const ruleName = 'time-min-milliseconds';
const messages = ruleMessages(ruleName, {
expected: (time) => `Expected a minimum of ${time} milliseconds`,
});
const DELAY_PROPERTIES = new Set(['animation-delay', 'transition-delay']);
function rule(minimum, options) {
return (root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{
actual: minimum,
possible: _.isNumber,
},
{
actual: options,
possible: {
ignore: ['delay'],
},
optional: true,
},
);
if (!validOptions) {
return;
}
root.walkDecls((decl) => {
const propertyName = vendor.unprefixed(decl.prop.toLowerCase());
if (
keywordSets.longhandTimeProperties.has(propertyName) &&
!isIgnoredProperty(propertyName) &&
!isAcceptableTime(decl.value)
) {
complain(decl);
}
if (keywordSets.shorthandTimeProperties.has(propertyName)) {
const valueListList = postcss.list.comma(decl.value);
for (const valueListString of valueListList) {
const valueList = postcss.list.space(valueListString);
if (optionsMatches(options, 'ignore', 'delay')) {
// Check only duration time values
const duration = getDuration(valueList);
if (duration && !isAcceptableTime(duration)) {
complain(decl, decl.value.indexOf(duration));
}
} else {
// Check all time values
for (const value of valueList) {
if (!isAcceptableTime(value)) {
complain(decl, decl.value.indexOf(value));
}
}
}
}
}
});
/**
* Get the duration within an `animation` or `transition` shorthand property value.
*
* @param {Node[]} valueList
*
* @returns {Node}
*/
function getDuration(valueList) {
for (const value of valueList) {
const parsedTime = valueParser.unit(value);
if (!parsedTime) continue;
// The first numeric value in an animation shorthand is the duration.
return value;
}
}
function isIgnoredProperty(propertyName) {
if (optionsMatches(options, 'ignore', 'delay') && DELAY_PROPERTIES.has(propertyName)) {
return true;
}
return false;
}
function isAcceptableTime(time) {
const parsedTime = valueParser.unit(time);
if (!parsedTime) return true;
if (parsedTime.number <= 0) {
return true;
}
if (parsedTime.unit.toLowerCase() === 'ms' && parsedTime.number < minimum) {
return false;
}
if (parsedTime.unit.toLowerCase() === 's' && parsedTime.number * 1000 < minimum) {
return false;
}
return true;
}
function complain(decl, offset = 0) {
report({
result,
ruleName,
message: messages.expected(minimum),
index: declarationValueIndex(decl) + offset,
node: decl,
});
}
};
}
rule.ruleName = ruleName;
rule.messages = messages;
module.exports = rule;