-
-
Notifications
You must be signed in to change notification settings - Fork 929
/
index.js
140 lines (111 loc) · 3.87 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
'use strict';
const declarationValueIndex = require('../../utils/declarationValueIndex');
const functionArgumentsSearch = require('../../utils/functionArgumentsSearch');
const isStandardSyntaxValue = require('../../utils/isStandardSyntaxValue');
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 = 'function-linear-gradient-no-nonstandard-direction';
const messages = ruleMessages(ruleName, {
rejected: 'Unexpected nonstandard direction',
});
const meta = {
url: 'https://stylelint.io/user-guide/rules/function-linear-gradient-no-nonstandard-direction',
};
const LINEAR_GRADIENT_FUNCTION = '(?:-webkit-|-moz-|-o-|-ms-)?linear-gradient';
const LINEAR_GRADIENT_FUNCTION_CALL = new RegExp(`${LINEAR_GRADIENT_FUNCTION}\\(`, 'i');
const LINEAR_GRADIENT_FUNCTION_NAME = new RegExp(`^${LINEAR_GRADIENT_FUNCTION}$`, 'i');
const DIRECTION = /top|left|bottom|right/i;
const DIRECTION_WITH_TO = new RegExp(`^to (${DIRECTION.source})(?: (${DIRECTION.source}))?$`, 'i');
const DIRECTION_WITHOUT_TO = new RegExp(`^(${DIRECTION.source})(?: (${DIRECTION.source}))?$`, 'i');
const DIGIT = /[\d.]/;
const ANGLE = /^[\d.]+(?:deg|grad|rad|turn)$/;
const IN_KEYWORD = /\bin\b/i;
/**
* @param {string} source
* @param {boolean} withToPrefix
*/
function isStandardDirection(source, withToPrefix) {
const regexp = withToPrefix ? DIRECTION_WITH_TO : DIRECTION_WITHOUT_TO;
const matches = source.match(regexp);
if (!matches) {
return false;
}
if (matches.length === 2) {
return true;
}
// Cannot repeat side-or-corner, e.g. "to top top"
if (matches.length === 3 && matches[1] !== matches[2]) {
return true;
}
return false;
}
/** @type {import('stylelint').Rule} */
const rule = (primary) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, { actual: primary });
if (!validOptions) {
return;
}
root.walkDecls((decl) => {
if (!LINEAR_GRADIENT_FUNCTION_CALL.test(decl.value)) return;
valueParser(decl.value).walk((valueNode) => {
if (valueNode.type !== 'function') {
return;
}
functionArgumentsSearch(
valueParser.stringify(valueNode).toLowerCase(),
LINEAR_GRADIENT_FUNCTION_NAME,
(expression, expressionIndex) => {
const args = expression.split(',');
const firstArg = (args[0] || '').trim();
// If the first arg is not standard, return early
if (!isStandardSyntaxValue(firstArg)) {
return;
}
// Ignore gradients with modern syntax that have color space interpolation arguments
if (IN_KEYWORD.test(firstArg)) {
return;
}
// If the first character is a number, we can assume the user intends an angle
if (DIGIT.test(firstArg.charAt(0))) {
if (ANGLE.test(firstArg)) {
return;
}
complain();
return;
}
// The first argument may not be a direction: it may be an angle,
// or a color stop (in which case user gets default direction, "to bottom")
// cf. https://drafts.csswg.org/css-images-3/#linear-gradient-syntax
if (!DIRECTION.test(firstArg)) {
return;
}
const withToPrefix = !vendor.prefix(valueNode.value);
if (!isStandardDirection(firstArg, withToPrefix)) {
complain();
}
function complain() {
const index = declarationValueIndex(decl) + valueNode.sourceIndex + expressionIndex;
const endIndex = index + (args[0] || '').trimEnd().length;
report({
message: messages.rejected,
node: decl,
index,
endIndex,
result,
ruleName,
});
}
},
);
});
});
};
};
rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
module.exports = rule;