forked from stylelint/stylelint
/
index.js
112 lines (93 loc) · 2.76 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
// @ts-nocheck
'use strict';
const isKeyframeSelector = require('../../utils/isKeyframeSelector');
const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
const isStandardSyntaxTypeSelector = require('../../utils/isStandardSyntaxTypeSelector');
const optionsMatches = require('../../utils/optionsMatches');
const parseSelector = require('../../utils/parseSelector');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const { isString } = require('../../utils/validateTypes');
const keywordSets = require('../../reference/keywordSets');
const ruleName = 'selector-type-case';
const messages = ruleMessages(ruleName, {
expected: (actual, expected) => `Expected "${actual}" to be "${expected}"`,
});
function rule(expectation, options, context) {
return (root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{
actual: expectation,
possible: ['lower', 'upper'],
},
{
actual: options,
possible: {
ignoreTypes: [isString],
},
optional: true,
},
);
if (!validOptions) {
return;
}
root.walkRules((ruleNode) => {
let hasComments = ruleNode.raws.selector && ruleNode.raws.selector.raw;
const selector = hasComments ? hasComments : ruleNode.selector;
const selectors = ruleNode.selectors;
if (!isStandardSyntaxRule(ruleNode)) {
return;
}
if (selectors.some((s) => isKeyframeSelector(s))) {
return;
}
parseSelector(selector, result, ruleNode, (selectorAST) => {
selectorAST.walkTags((tag) => {
if (!isStandardSyntaxTypeSelector(tag)) {
return;
}
if (keywordSets.validMixedCaseSvgElements.has(tag.value)) {
return;
}
if (optionsMatches(options, 'ignoreTypes', tag.value)) {
return;
}
const sourceIndex = tag.sourceIndex;
const value = tag.value;
const expectedValue = expectation === 'lower' ? value.toLowerCase() : value.toUpperCase();
if (value === expectedValue) {
return;
}
if (context.fix) {
if (hasComments) {
hasComments =
hasComments.slice(0, sourceIndex) +
expectedValue +
hasComments.slice(sourceIndex + value.length);
ruleNode.raws.selector.raw = hasComments;
} else {
ruleNode.selector =
ruleNode.selector.slice(0, sourceIndex) +
expectedValue +
ruleNode.selector.slice(sourceIndex + value.length);
}
return;
}
report({
message: messages.expected(value, expectedValue),
node: ruleNode,
index: sourceIndex,
ruleName,
result,
});
});
});
});
};
}
rule.ruleName = ruleName;
rule.messages = messages;
module.exports = rule;