-
-
Notifications
You must be signed in to change notification settings - Fork 930
/
index.js
120 lines (98 loc) · 3.13 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
'use strict';
const declarationValueIndex = require('../../utils/declarationValueIndex');
const getDeclarationValue = require('../../utils/getDeclarationValue');
const isStandardSyntaxFunction = require('../../utils/isStandardSyntaxFunction');
const { camelCaseFunctions } = require('../../reference/functions');
const optionsMatches = require('../../utils/optionsMatches');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const setDeclarationValue = require('../../utils/setDeclarationValue');
const validateOptions = require('../../utils/validateOptions');
const valueParser = require('postcss-value-parser');
const { isRegExp, isString } = require('../../utils/validateTypes');
const isStandardSyntaxValue = require('../../utils/isStandardSyntaxValue');
const ruleName = 'function-name-case';
const messages = ruleMessages(ruleName, {
expected: (actual, expected) => `Expected "${actual}" to be "${expected}"`,
});
const meta = {
url: 'https://stylelint.io/user-guide/rules/function-name-case',
fixable: true,
};
const mapLowercaseFunctionNamesToCamelCase = new Map();
for (const func of camelCaseFunctions) {
mapLowercaseFunctionNamesToCamelCase.set(func.toLowerCase(), func);
}
/** @type {import('stylelint').Rule} */
const rule = (primary, secondaryOptions, context) => {
return (root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{
actual: primary,
possible: ['lower', 'upper'],
},
{
actual: secondaryOptions,
possible: {
ignoreFunctions: [isString, isRegExp],
},
optional: true,
},
);
if (!validOptions) {
return;
}
root.walkDecls((decl) => {
if (!decl.value.includes('(')) return;
if (!isStandardSyntaxValue(decl.value)) return;
let needFix = false;
const parsed = valueParser(getDeclarationValue(decl));
parsed.walk((node) => {
if (node.type !== 'function' || !isStandardSyntaxFunction(node)) {
return;
}
const functionName = node.value;
const functionNameLowerCase = functionName.toLowerCase();
if (optionsMatches(secondaryOptions, 'ignoreFunctions', functionName)) {
return;
}
let expectedFunctionName = null;
if (
primary === 'lower' &&
mapLowercaseFunctionNamesToCamelCase.has(functionNameLowerCase)
) {
expectedFunctionName = mapLowercaseFunctionNamesToCamelCase.get(functionNameLowerCase);
} else if (primary === 'lower') {
expectedFunctionName = functionNameLowerCase;
} else {
expectedFunctionName = functionName.toUpperCase();
}
if (functionName === expectedFunctionName) {
return;
}
if (context.fix) {
needFix = true;
node.value = expectedFunctionName;
return;
}
report({
message: messages.expected,
messageArgs: [functionName, expectedFunctionName],
node: decl,
index: declarationValueIndex(decl) + node.sourceIndex,
result,
ruleName,
});
});
if (context.fix && needFix) {
setDeclarationValue(decl, parsed.toString());
}
});
};
};
rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
module.exports = rule;