-
Notifications
You must be signed in to change notification settings - Fork 22
/
colors.js
168 lines (145 loc) · 5.28 KB
/
colors.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import stylelint from 'stylelint'
import {declarationValueIndex} from 'stylelint/lib/utils/nodeFieldIndices.cjs'
import {primitivesVariables, hasValidColor} from './lib/utils.js'
import valueParser from 'postcss-value-parser'
const {
createPlugin,
utils: {report, ruleMessages, validateOptions},
} = stylelint
export const ruleName = 'primer/colors'
export const messages = ruleMessages(ruleName, {
rejected: (value, type) => {
if (type === 'fg') {
return `Please use a Primer foreground color variable instead of '${value}'. https://primer.style/foundations/primitives/color#foreground`
} else if (type === 'bg') {
return `Please use a Primer background color variable instead of '${value}'. https://primer.style/foundations/primitives/color#background`
} else if (type === 'border') {
return `Please use a Primer border color variable instead of '${value}'. https://primer.style/foundations/primitives/color#border`
}
return `Please use with a Primer color variable instead of '${value}'. https://primer.style/foundations/primitives/color`
},
})
let variables = primitivesVariables('colors')
const validProps = {
'^color$': ['fgColor', 'iconColor'],
'^background(-color)?$': ['bgColor'],
'^border(-top|-right|-bottom|-left|-inline|-block)*(-color)?$': ['borderColor'],
'^fill$': ['fgColor', 'iconColor', 'bgColor'],
'^stroke$': ['fgColor', 'iconColor', 'bgColor', 'borderColor'],
}
const validValues = [
'none',
'currentcolor',
'inherit',
'initial',
'unset',
'revert',
'revert-layer',
'transparent',
'0',
]
const propType = prop => {
if (/^color/.test(prop)) {
return 'fg'
} else if (/^background(-color)?$/.test(prop)) {
return 'bg'
} else if (/^border(-top|-right|-bottom|-left|-inline|-block)*(-color)?$/.test(prop)) {
return 'border'
} else if (/^fill$/.test(prop)) {
return 'fg'
} else if (/^stroke$/.test(prop)) {
return 'fg'
}
return undefined
}
variables = variables.filter(variable => {
const name = variable['name']
// remove shadow and boxShadow variables
return !(name.includes('shadow') || name.includes('boxShadow'))
})
/** @type {import('stylelint').Rule} */
const ruleFunction = primary => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: primary,
possible: [true],
})
if (!validOptions) return
const valueIsCorrectType = (value, types) => types.some(type => value.includes(type))
root.walkDecls(declNode => {
const {prop, value} = declNode
// Skip if prop is not a valid color prop
if (!Object.keys(validProps).some(validProp => new RegExp(validProp).test(prop))) return
// Get the valid types for the prop
const types = validProps[Object.keys(validProps).find(re => new RegExp(re).test(prop))]
// Walk the value split
valueParser(value).walk(valueNode => {
// Skip if value is not a word or function
if (valueNode.type !== 'word' && valueNode.type !== 'function') return
// Skip if value is a valid value
if (validValues.includes(valueNode.value)) return
if (hasValidColor(valueNode.value) || /^\$/.test(valueNode.value)) {
const rejectedValue =
valueNode.type === 'function'
? `${valueNode.value}(${valueParser.stringify(valueNode.nodes)})`
: valueNode.value
report({
index: declarationValueIndex(declNode) + valueNode.sourceIndex,
endIndex: declarationValueIndex(declNode) + valueNode.sourceEndIndex,
message: messages.rejected(rejectedValue, propType(prop)),
node: declNode,
result,
ruleName,
})
return
}
// Skip functions
if (valueNode.type === 'function') {
return
}
// Variable exists and is the correct type (fg, bg, border)
if (
variables.some(variable => new RegExp(variable['name']).test(valueNode.value)) &&
valueIsCorrectType(valueNode.value, types)
) {
return
}
// Value doesn't start with variable --
if (!valueNode.value.startsWith('--')) {
return
}
// Ignore old system colors --color-*
if (
[
/^--color-(?:[a-zA-Z0-9-]+-)*text(?:-[a-zA-Z0-9-]+)*$/,
/^--color-(?:[a-zA-Z0-9-](?!-))*-fg(?:-[a-zA-Z0-9-]+)*$/,
/^--color-[^)]+$/,
].some(oldSysRe => oldSysRe.test(valueNode.value))
) {
return
}
// Property is shortand and value doesn't include color
if (
(/^border(-top|-right|-bottom|-left|-inline|-block)*$/.test(prop) || /^background$/.test(prop)) &&
!valueNode.value.toLowerCase().includes('color')
) {
return
}
report({
index: declarationValueIndex(declNode) + valueNode.sourceIndex,
endIndex: declarationValueIndex(declNode) + valueNode.sourceEndIndex,
message: messages.rejected(`var(${valueNode.value})`, propType(prop)),
node: declNode,
result,
ruleName,
})
})
})
}
}
ruleFunction.ruleName = ruleName
ruleFunction.messages = messages
ruleFunction.meta = {
fixable: false,
}
export default createPlugin(ruleName, ruleFunction)