-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
92 lines (74 loc) · 2.25 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
const { createPlugin } = require('stylelint')
const isCustomProperty = require('stylelint/lib/utils/isCustomProperty')
const isStandardSyntaxProperty = require('stylelint/lib/utils/isStandardSyntaxProperty')
const report = require('stylelint/lib/utils/report')
const ruleMessages = require('stylelint/lib/utils/ruleMessages')
const validateOptions = require('stylelint/lib/utils/validateOptions')
const optionsMatches = require('stylelint/lib/utils/optionsMatches')
const { isRegExp, isString } = require('stylelint/lib/utils/validateTypes')
const { isRule } = require('stylelint/lib/utils/typeGuards')
const ruleName = 'stylistic/property-case'
const messages = ruleMessages(ruleName, {
expected: (actual, expected) => `Expected "${actual}" to be "${expected}"`,
})
const meta = {
url: 'https://github.com/elirasza/stylelint-stylistic/tree/main/lib/rules/property-case',
fixable: true,
}
/** @type {import('stylelint').Rule} */
const rule = (primary, secondaryOptions, context) => (root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{
actual: primary,
possible: ['lower', 'upper'],
},
{
actual: secondaryOptions,
possible: {
ignoreSelectors: [isString, isRegExp],
},
optional: true,
},
)
if (!validOptions) {
return
}
root.walkDecls((decl) => {
const { prop } = decl
if (!isStandardSyntaxProperty(prop)) {
return
}
if (isCustomProperty(prop)) {
return
}
const { parent } = decl
if (!parent) throw new Error('A parent node must be present')
if (isRule(parent)) {
const { selector } = parent
if (selector && optionsMatches(secondaryOptions, 'ignoreSelectors', selector)) {
return
}
}
const expectedProp = primary === 'lower' ? prop.toLowerCase() : prop.toUpperCase()
if (prop === expectedProp) {
return
}
if (context.fix) {
decl.prop = expectedProp
return
}
report({
message: messages.expected(prop, expectedProp),
word: prop,
node: decl,
ruleName,
result,
})
})
}
rule.ruleName = ruleName
rule.messages = messages
rule.meta = meta
module.exports = { messages, meta, plugin: createPlugin(ruleName, rule), rule, ruleName }