@primer/stylelint-config 12.9.1 → 12.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +1 -0
- package/package.json +1 -1
- package/plugins/no-display-colors.js +53 -0
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
const stylelint = require('stylelint')
|
|
2
|
+
const matchAll = require('string.prototype.matchall')
|
|
3
|
+
|
|
4
|
+
const ruleName = 'primer/no-display-colors'
|
|
5
|
+
const messages = stylelint.utils.ruleMessages(ruleName, {
|
|
6
|
+
rejected: varName => `${varName} is in alpha and should be used with caution with approval from the Primer team`,
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
// Match CSS variable references (e.g var(--display-blue-fgColor))
|
|
10
|
+
// eslint-disable-next-line no-useless-escape
|
|
11
|
+
const variableReferenceRegex = /var\(([^\),]+)(,.*)?\)/g
|
|
12
|
+
|
|
13
|
+
module.exports = stylelint.createPlugin(ruleName, (enabled, options = {}) => {
|
|
14
|
+
if (!enabled) {
|
|
15
|
+
return noop
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const {verbose = false} = options
|
|
19
|
+
// eslint-disable-next-line no-console
|
|
20
|
+
const log = verbose ? (...args) => console.warn(...args) : noop
|
|
21
|
+
|
|
22
|
+
// Keep track of declarations we've already seen
|
|
23
|
+
const seen = new WeakMap()
|
|
24
|
+
|
|
25
|
+
return (root, result) => {
|
|
26
|
+
root.walkRules(rule => {
|
|
27
|
+
rule.walkDecls(decl => {
|
|
28
|
+
if (seen.has(decl)) {
|
|
29
|
+
return
|
|
30
|
+
} else {
|
|
31
|
+
seen.set(decl, true)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
for (const [, variableName] of matchAll(decl.value, variableReferenceRegex)) {
|
|
35
|
+
log(`Found variable reference ${variableName}`)
|
|
36
|
+
if (variableName.match(/^--display-.*/)) {
|
|
37
|
+
stylelint.utils.report({
|
|
38
|
+
message: messages.rejected(variableName),
|
|
39
|
+
node: decl,
|
|
40
|
+
result,
|
|
41
|
+
ruleName,
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
function noop() {}
|
|
51
|
+
|
|
52
|
+
module.exports.ruleName = ruleName
|
|
53
|
+
module.exports.messages = messages
|