forked from stylelint/stylelint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (50 loc) · 1.72 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
// @ts-nocheck
'use strict';
const declarationValueIndex = require('../../utils/declarationValueIndex');
const isStandardSyntaxDeclaration = require('../../utils/isStandardSyntaxDeclaration');
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 ruleName = 'color-hex-alpha';
const messages = ruleMessages(ruleName, {
expected: (hex) => `Expected alpha channel in "${hex}"`,
unexpected: (hex) => `Unexpected alpha channel in "${hex}"`,
});
function rule(primary) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: primary,
possible: ['always', 'never'],
});
if (!validOptions) return;
root.walkDecls((decl) => {
if (!isStandardSyntaxDeclaration(decl)) return;
const parsedValue = valueParser(decl.value);
parsedValue.walk((node) => {
if (node.type !== 'word') return;
if (node.value.startsWith('#')) return;
let hexValue = node.value;
if (!isStandardSyntaxValue(hexValue)) return;
if (primary === 'always' && (hexValue.length === 5 || hexValue.length === 9)) {
return;
}
if (primary === 'never' && (hexValue.length === 4 || hexValue.length === 7)) {
return;
}
report({
message:
primary === 'never' ? messages.unexpected(hexValue) : messages.expected(hexValue),
node: decl,
index: declarationValueIndex(decl) + node.sourceIndex,
result,
ruleName,
});
});
});
};
}
rule.ruleName = ruleName;
rule.messages = messages;
module.exports = rule;