-
-
Notifications
You must be signed in to change notification settings - Fork 930
/
index.js
88 lines (66 loc) · 2.06 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
'use strict';
const valueParser = require('postcss-value-parser');
const declarationValueIndex = require('../../utils/declarationValueIndex');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const ruleName = 'custom-property-no-missing-var-function';
const messages = ruleMessages(ruleName, {
rejected: (customProperty) => `Unexpected missing var function for "${customProperty}"`,
});
const meta = {
url: 'https://stylelint.io/user-guide/rules/custom-property-no-missing-var-function',
};
/** @type {import('stylelint').Rule} */
const rule = (primary) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, { actual: primary });
if (!validOptions) return;
/** @type {Set<string>} */
const knownCustomProperties = new Set();
root.walkAtRules(/^property$/i, (atRule) => {
knownCustomProperties.add(atRule.params);
});
root.walkDecls(/^--/, ({ prop }) => {
knownCustomProperties.add(prop);
});
root.walkDecls((decl) => {
const { value } = decl;
if (!value.includes('--')) return;
const parsedValue = valueParser(value);
parsedValue.walk((node) => {
if (isVarFunction(node)) return false;
if (!isDashedIdent(node)) return;
if (!knownCustomProperties.has(node.value)) return;
const index = declarationValueIndex(decl) + node.sourceIndex;
const endIndex = index + node.value.length;
report({
message: messages.rejected,
messageArgs: [node.value],
node: decl,
index,
endIndex,
result,
ruleName,
});
return false;
});
});
};
};
/**
* @param {import('postcss-value-parser').Node} node
*/
function isDashedIdent({ type, value }) {
return type === 'word' && value.startsWith('--');
}
/**
* @param {import('postcss-value-parser').Node} node
*/
function isVarFunction({ type, value }) {
return type === 'function' && value === 'var';
}
rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
module.exports = rule;