-
-
Notifications
You must be signed in to change notification settings - Fork 929
/
index.js
132 lines (106 loc) · 3.41 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
121
122
123
124
125
126
127
128
129
130
131
132
'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, isRegExp } = require('../../utils/validateTypes');
const { mixedCaseSvgTypeSelectors } = require('../../reference/selectors');
const ruleName = 'selector-type-case';
const messages = ruleMessages(ruleName, {
expected: (actual, expected) => `Expected "${actual}" to be "${expected}"`,
});
const meta = {
url: 'https://stylelint.io/user-guide/rules/selector-type-case',
fixable: true,
};
const STARTS_A_TAG_NAME_REGEX = /(?:[^.#[:a-zA-Z-]|^)[a-zA-Z]/;
const ANY_UPPER_CASE_REGEX = /[A-Z]/;
const ANY_LOWER_CASE_REGEX = /[a-z]/;
/** @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: {
ignoreTypes: [isString, isRegExp],
},
optional: true,
},
);
if (!validOptions) {
return;
}
root.walkRules((ruleNode) => {
let hasComments = ruleNode.raws.selector && ruleNode.raws.selector.raw;
const selector = hasComments ? hasComments : ruleNode.selector;
if (!STARTS_A_TAG_NAME_REGEX.test(selector)) return;
if (primary === 'lower' && !ANY_UPPER_CASE_REGEX.test(selector)) return;
if (primary === 'upper' && !ANY_LOWER_CASE_REGEX.test(selector)) return;
if (!isStandardSyntaxRule(ruleNode)) return;
const { selectors } = ruleNode;
if (selectors.some((s) => isKeyframeSelector(s))) {
return;
}
parseSelector(selector, result, ruleNode, (selectorAST) => {
selectorAST.walkTags((tag) => {
if (!isStandardSyntaxTypeSelector(tag)) {
return;
}
if (mixedCaseSvgTypeSelectors.has(tag.value)) {
return;
}
if (optionsMatches(secondaryOptions, 'ignoreTypes', tag.value)) {
return;
}
const sourceIndex = tag.sourceIndex;
const value = tag.value;
const expectedValue = primary === '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);
if (ruleNode.raws.selector == null) {
throw new Error('The `raw` property must be present');
}
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,
messageArgs: [value, expectedValue],
node: ruleNode,
index: sourceIndex,
ruleName,
result,
});
});
});
});
};
};
rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
module.exports = rule;