@primer/stylelint-config 13.0.0-rc.35215da → 13.0.0-rc.3fc3c9a
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/README.md +0 -4
- package/dist/index.cjs +1698 -0
- package/dist/index.mjs +1695 -0
- package/package.json +13 -8
- package/plugins/README.md +1 -131
- package/plugins/new-color-vars-have-fallback.js +0 -35
- package/plugins/no-deprecated-colors.js +0 -97
- package/plugins/no-override.js +0 -98
- package/plugins/no-scale-colors.js +0 -51
- package/plugins/no-undefined-vars.js +0 -118
- package/plugins/no-unused-vars.js +0 -96
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import TapMap from 'tap-map'
|
|
2
|
-
import globby from 'globby'
|
|
3
|
-
import matchAll from 'string.prototype.matchall'
|
|
4
|
-
import stylelint from 'stylelint'
|
|
5
|
-
import {readFileSync} from 'fs'
|
|
6
|
-
|
|
7
|
-
export const ruleName = 'primer/no-unused-vars'
|
|
8
|
-
|
|
9
|
-
const cwd = process.cwd()
|
|
10
|
-
const COLON = ':'
|
|
11
|
-
const SCSS_VARIABLE_PATTERN = /(\$[-\w]+)/g
|
|
12
|
-
|
|
13
|
-
export const messages = stylelint.utils.ruleMessages(ruleName, {
|
|
14
|
-
rejected: name => `The variable "${name}" is not referenced.`,
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
const cache = new TapMap()
|
|
18
|
-
|
|
19
|
-
export default stylelint.createPlugin(ruleName, (enabled, options = {}) => {
|
|
20
|
-
if (!enabled) {
|
|
21
|
-
return noop
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const {files = ['**/*.scss', '!node_modules'], variablePattern = SCSS_VARIABLE_PATTERN, verbose = false} = options
|
|
25
|
-
// eslint-disable-next-line no-console
|
|
26
|
-
const log = verbose ? (...args) => console.warn(...args) : noop
|
|
27
|
-
const cacheOptions = {files, variablePattern, cwd}
|
|
28
|
-
const {refs} = getCachedVariables(cacheOptions, log)
|
|
29
|
-
|
|
30
|
-
return (root, result) => {
|
|
31
|
-
root.walkDecls(decl => {
|
|
32
|
-
for (const [name] of matchAll(decl.prop, variablePattern)) {
|
|
33
|
-
if (!refs.has(name)) {
|
|
34
|
-
stylelint.utils.report({
|
|
35
|
-
message: messages.rejected(name),
|
|
36
|
-
node: decl,
|
|
37
|
-
result,
|
|
38
|
-
ruleName,
|
|
39
|
-
})
|
|
40
|
-
} else {
|
|
41
|
-
const path = stripCwd(decl.source.input.file)
|
|
42
|
-
log(`${name} declared in ${path} ref'd in ${pluralize(refs.get(name).size, 'file')}`)
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
})
|
|
46
|
-
}
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
function getCachedVariables(options, log) {
|
|
50
|
-
const key = JSON.stringify(options)
|
|
51
|
-
return cache.tap(key, () => {
|
|
52
|
-
const {files, variablePattern} = options
|
|
53
|
-
const decls = new TapMap()
|
|
54
|
-
const refs = new TapMap()
|
|
55
|
-
|
|
56
|
-
log(`Looking for variables in ${files} ...`)
|
|
57
|
-
for (const file of globby.sync(files)) {
|
|
58
|
-
const css = readFileSync(file, 'utf8')
|
|
59
|
-
for (const match of matchAll(css, variablePattern)) {
|
|
60
|
-
const after = css.substr(match.index + match[0].length)
|
|
61
|
-
const name = match[0]
|
|
62
|
-
if (after.startsWith(COLON)) {
|
|
63
|
-
decls.tap(name, set).add(file)
|
|
64
|
-
} else {
|
|
65
|
-
refs.tap(name, set).add(file)
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
log(`Found ${decls.size} declarations, ${pluralize(refs.size, 'reference')}.`)
|
|
70
|
-
|
|
71
|
-
for (const [name, filesList] of decls.entries()) {
|
|
72
|
-
const fileRefs = refs.get(name)
|
|
73
|
-
if (fileRefs) {
|
|
74
|
-
log(`variable "${name}" declared in ${pluralize(filesList.size, 'file')}, ref'd in ${fileRefs.size}`)
|
|
75
|
-
} else {
|
|
76
|
-
log(`[!] variable "${name}" declared in ${Array.from(filesList)[0]} is not referenced`)
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return {decls, refs}
|
|
81
|
-
})
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function noop() {}
|
|
85
|
-
|
|
86
|
-
function set() {
|
|
87
|
-
return new Set()
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function stripCwd(path) {
|
|
91
|
-
return path.startsWith(cwd) ? path.substr(cwd.length + 1) : path
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function pluralize(num, str, plural = `${str}s`) {
|
|
95
|
-
return num === 1 ? `${num} ${str}` : `${num} ${plural}`
|
|
96
|
-
}
|