@primer/stylelint-config 12.9.2 → 13.0.0-rc.0a1e1bd

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.
@@ -1,96 +0,0 @@
1
- const TapMap = require('tap-map')
2
- const globby = require('globby')
3
- const matchAll = require('string.prototype.matchall')
4
- const stylelint = require('stylelint')
5
- const {readFileSync} = require('fs')
6
-
7
- const ruleName = 'primer/no-unused-vars'
8
-
9
- const cwd = process.cwd()
10
- const COLON = ':'
11
- const SCSS_VARIABLE_PATTERN = /(\$[-\w]+)/g
12
-
13
- const messages = stylelint.utils.ruleMessages(ruleName, {
14
- rejected: name => `The variable "${name}" is not referenced.`,
15
- })
16
-
17
- const cache = new TapMap()
18
-
19
- module.exports = 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
- }