1 | 'use strict';
|
2 |
|
3 | const valueParser = require('postcss-value-parser');
|
4 |
|
5 | const declarationValueIndex = require('../../utils/declarationValueIndex');
|
6 | const getDeclarationValue = require('../../utils/getDeclarationValue');
|
7 | const isStandardSyntaxColorFunction = require('../../utils/isStandardSyntaxColorFunction');
|
8 | const report = require('../../utils/report');
|
9 | const ruleMessages = require('../../utils/ruleMessages');
|
10 | const setDeclarationValue = require('../../utils/setDeclarationValue');
|
11 | const { isValueFunction } = require('../../utils/typeGuards');
|
12 | const validateOptions = require('../../utils/validateOptions');
|
13 |
|
14 | const ruleName = 'color-function-notation';
|
15 |
|
16 | const messages = ruleMessages(ruleName, {
|
17 | expected: (primary) => `Expected ${primary} color-function notation`,
|
18 | });
|
19 |
|
20 | const meta = {
|
21 | url: 'https://stylelint.io/user-guide/rules/list/color-function-notation',
|
22 | };
|
23 |
|
24 | const LEGACY_FUNCS = new Set(['rgba', 'hsla']);
|
25 | const LEGACY_NOTATION_FUNCS = new Set(['rgb', 'rgba', 'hsl', 'hsla']);
|
26 |
|
27 |
|
28 | const rule = (primary, _secondaryOptions, context) => {
|
29 | return (root, result) => {
|
30 | const validOptions = validateOptions(result, ruleName, {
|
31 | actual: primary,
|
32 | possible: ['modern', 'legacy'],
|
33 | });
|
34 |
|
35 | if (!validOptions) return;
|
36 |
|
37 | root.walkDecls((decl) => {
|
38 | let needsFix = false;
|
39 | const parsedValue = valueParser(getDeclarationValue(decl));
|
40 |
|
41 | parsedValue.walk((node) => {
|
42 | if (!isValueFunction(node)) return;
|
43 |
|
44 | if (!isStandardSyntaxColorFunction(node)) return;
|
45 |
|
46 | const { value, sourceIndex, nodes } = node;
|
47 |
|
48 | if (!LEGACY_NOTATION_FUNCS.has(value.toLowerCase())) return;
|
49 |
|
50 | if (primary === 'modern' && !hasCommas(node)) return;
|
51 |
|
52 | if (primary === 'legacy' && hasCommas(node)) return;
|
53 |
|
54 | if (context.fix && primary === 'modern') {
|
55 | let commaCount = 0;
|
56 |
|
57 |
|
58 | node.nodes = nodes.map((childNode) => {
|
59 | if (isComma(childNode)) {
|
60 |
|
61 | if (commaCount < 2) {
|
62 |
|
63 | childNode.type = 'space';
|
64 | childNode.value = atLeastOneSpace(childNode.after);
|
65 | commaCount++;
|
66 | } else {
|
67 | childNode.value = '/';
|
68 | childNode.before = atLeastOneSpace(childNode.before);
|
69 | childNode.after = atLeastOneSpace(childNode.after);
|
70 | }
|
71 | }
|
72 |
|
73 | return childNode;
|
74 | });
|
75 |
|
76 |
|
77 | if (LEGACY_FUNCS.has(node.value.toLowerCase())) {
|
78 | node.value = node.value.slice(0, -1);
|
79 | }
|
80 |
|
81 | needsFix = true;
|
82 |
|
83 | return;
|
84 | }
|
85 |
|
86 | report({
|
87 | message: messages.expected(primary),
|
88 | node: decl,
|
89 | index: declarationValueIndex(decl) + sourceIndex,
|
90 | result,
|
91 | ruleName,
|
92 | });
|
93 | });
|
94 |
|
95 | if (needsFix) {
|
96 | setDeclarationValue(decl, parsedValue.toString());
|
97 | }
|
98 | });
|
99 | };
|
100 | };
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | function atLeastOneSpace(whitespace) {
|
106 | return whitespace !== '' ? whitespace : ' ';
|
107 | }
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | function isComma(node) {
|
114 | return node.type === 'div' && node.value === ',';
|
115 | }
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | function hasCommas(node) {
|
121 | return node.nodes && node.nodes.some((childNode) => isComma(childNode));
|
122 | }
|
123 |
|
124 | rule.ruleName = ruleName;
|
125 | rule.messages = messages;
|
126 | rule.meta = meta;
|
127 | module.exports = rule;
|