1 | 'use strict';
|
2 |
|
3 | const _ = require('lodash');
|
4 | const isKeyframeSelector = require('../../utils/isKeyframeSelector');
|
5 | const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
|
6 | const isStandardSyntaxTypeSelector = require('../../utils/isStandardSyntaxTypeSelector');
|
7 | const optionsMatches = require('../../utils/optionsMatches');
|
8 | const parseSelector = require('../../utils/parseSelector');
|
9 | const report = require('../../utils/report');
|
10 | const ruleMessages = require('../../utils/ruleMessages');
|
11 | const validateOptions = require('../../utils/validateOptions');
|
12 |
|
13 | const ruleName = 'selector-type-case';
|
14 |
|
15 | const messages = ruleMessages(ruleName, {
|
16 | expected: (actual, expected) => `Expected "${actual}" to be "${expected}"`,
|
17 | });
|
18 |
|
19 | function rule(expectation, options, context) {
|
20 | return (root, result) => {
|
21 | const validOptions = validateOptions(
|
22 | result,
|
23 | ruleName,
|
24 | {
|
25 | actual: expectation,
|
26 | possible: ['lower', 'upper'],
|
27 | },
|
28 | {
|
29 | actual: options,
|
30 | possible: {
|
31 | ignoreTypes: [_.isString],
|
32 | },
|
33 | optional: true,
|
34 | },
|
35 | );
|
36 |
|
37 | if (!validOptions) {
|
38 | return;
|
39 | }
|
40 |
|
41 | root.walkRules((rule) => {
|
42 | let hasComments = _.get(rule, 'raws.selector.raw');
|
43 | const selector = hasComments ? hasComments : rule.selector;
|
44 | const selectors = rule.selectors;
|
45 |
|
46 | if (!isStandardSyntaxRule(rule)) {
|
47 | return;
|
48 | }
|
49 |
|
50 | if (selectors.some((s) => isKeyframeSelector(s))) {
|
51 | return;
|
52 | }
|
53 |
|
54 | parseSelector(selector, result, rule, (selectorAST) => {
|
55 | selectorAST.walkTags((tag) => {
|
56 | if (!isStandardSyntaxTypeSelector(tag)) {
|
57 | return;
|
58 | }
|
59 |
|
60 | if (optionsMatches(options, 'ignoreTypes', tag.value)) {
|
61 | return;
|
62 | }
|
63 |
|
64 | const sourceIndex = tag.sourceIndex;
|
65 | const value = tag.value;
|
66 |
|
67 | const expectedValue = expectation === 'lower' ? value.toLowerCase() : value.toUpperCase();
|
68 |
|
69 | if (value === expectedValue) {
|
70 | return;
|
71 | }
|
72 |
|
73 | if (context.fix) {
|
74 | if (hasComments) {
|
75 | hasComments =
|
76 | hasComments.slice(0, sourceIndex) +
|
77 | expectedValue +
|
78 | hasComments.slice(sourceIndex + value.length);
|
79 | _.set(rule, 'raws.selector.raw', hasComments);
|
80 | } else {
|
81 | rule.selector =
|
82 | rule.selector.slice(0, sourceIndex) +
|
83 | expectedValue +
|
84 | rule.selector.slice(sourceIndex + value.length);
|
85 | }
|
86 |
|
87 | return;
|
88 | }
|
89 |
|
90 | report({
|
91 | message: messages.expected(value, expectedValue),
|
92 | node: rule,
|
93 | index: sourceIndex,
|
94 | ruleName,
|
95 | result,
|
96 | });
|
97 | });
|
98 | });
|
99 | });
|
100 | };
|
101 | }
|
102 |
|
103 | rule.ruleName = ruleName;
|
104 | rule.messages = messages;
|
105 | module.exports = rule;
|