@primer/stylelint-config 12.9.2-rc.46fe2e1 → 13.0.0-rc.0068ca3

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,39 +0,0 @@
1
- const stylelint = require('stylelint')
2
-
3
- const ruleName = 'primer/new-color-vars-have-fallback'
4
- const messages = stylelint.utils.ruleMessages(ruleName, {
5
- expectedFallback: variable =>
6
- `Expected a fallback value for CSS variable ${variable}. New color variables fallbacks, check primer.style/primitives to find the correct value`,
7
- })
8
-
9
- module.exports = stylelint.createPlugin(ruleName, enabled => {
10
- const variables = require('./lib/new-color-css-vars-map.json')
11
-
12
- if (!enabled) {
13
- return noop
14
- }
15
-
16
- return (root, result) => {
17
- root.walkDecls(node => {
18
- for (const variable of variables) {
19
- if (node.value.includes(`var(${variable})`)) {
20
- // Check if the declaration uses a CSS variable from the JSON
21
- const match = node.value.match(new RegExp(`var\\(${variable},(.*)\\)`))
22
- if (!match || match[1].trim() === '') {
23
- stylelint.utils.report({
24
- ruleName,
25
- result,
26
- node,
27
- message: messages.expectedFallback(variable),
28
- })
29
- }
30
- }
31
- }
32
- })
33
- }
34
- })
35
-
36
- function noop() {}
37
-
38
- module.exports.ruleName = ruleName
39
- module.exports.messages = messages
@@ -1,103 +0,0 @@
1
- const stylelint = require('stylelint')
2
- const matchAll = require('string.prototype.matchall')
3
-
4
- const ruleName = 'primer/no-deprecated-colors'
5
- const messages = stylelint.utils.ruleMessages(ruleName, {
6
- rejected: (varName, replacement, property) => {
7
- if (replacement === null) {
8
- return `Variable ${varName} is deprecated for property ${property}. Please consult the primer color docs for a replacement. https://primer.style/primitives/storybook/?path=/story/migration-tables`
9
- }
10
-
11
- return `Variable ${varName} is deprecated for property ${property}. Please use the replacement ${replacement}.`
12
- },
13
- })
14
-
15
- // Match CSS variable references (e.g var(--color-text-primary))
16
- // eslint-disable-next-line no-useless-escape
17
- const variableReferenceRegex = /var\(([^\),]+)(,.*)?\)/g
18
-
19
- const replacedVars = {}
20
- const newVars = {}
21
-
22
- module.exports = stylelint.createPlugin(ruleName, (enabled, options = {}, context) => {
23
- const {inlineFallback = false} = options
24
-
25
- if (!enabled) {
26
- return noop
27
- }
28
-
29
- const {verbose = false} = options
30
- // eslint-disable-next-line no-console
31
- const log = verbose ? (...args) => console.warn(...args) : noop
32
-
33
- // Keep track of declarations we've already seen
34
- const seen = new WeakMap()
35
-
36
- // eslint-disable-next-line import/no-dynamic-require
37
- const variableChecks = require(options.deprecatedFile || './lib/primitives-v8.json')
38
-
39
- const lintResult = (root, result) => {
40
- // Walk all declarations
41
- root.walk(node => {
42
- if (seen.has(node)) {
43
- return
44
- } else {
45
- seen.set(node, true)
46
- }
47
-
48
- // walk these nodes
49
- if (node.type !== 'decl') {
50
- return
51
- }
52
-
53
- for (const [, variableName] of matchAll(node.value, variableReferenceRegex)) {
54
- if (variableName in variableChecks) {
55
- let replacement = variableChecks[variableName]
56
- if (typeof replacement === 'object') {
57
- if (node.prop) {
58
- for (const prop of replacement) {
59
- // Check if node.prop starts with one of the props array elements
60
- if (prop['props'].some(p => node.prop.startsWith(p))) {
61
- replacement = prop['replacement']
62
- break
63
- }
64
- }
65
- }
66
- if (typeof replacement === 'object') {
67
- replacement = null
68
- }
69
- }
70
-
71
- if (context.fix && replacement !== null) {
72
- replacement = `${replacement}${inlineFallback ? `, var(${variableName})` : ''}`
73
- replacedVars[variableName] = true
74
- newVars[replacement] = true
75
- if (node.type === 'atrule') {
76
- node.params = node.params.replace(variableName, replacement)
77
- } else {
78
- node.value = node.value.replace(variableName, replacement)
79
- }
80
- continue
81
- }
82
-
83
- stylelint.utils.report({
84
- message: messages.rejected(variableName, replacement, node.prop),
85
- node,
86
- ruleName,
87
- result,
88
- })
89
- }
90
- }
91
- })
92
- }
93
-
94
- log(
95
- `${Object.keys(replacedVars).length} deprecated variables replaced with ${Object.keys(newVars).length} variables.`,
96
- )
97
- return lintResult
98
- })
99
-
100
- function noop() {}
101
-
102
- module.exports.ruleName = ruleName
103
- module.exports.messages = messages
@@ -1,47 +0,0 @@
1
- const stylelint = require('stylelint')
2
- const declarationValueIndex = require('stylelint/lib/utils/declarationValueIndex')
3
-
4
- const ruleName = 'primer/no-experimental-vars'
5
- const messages = stylelint.utils.ruleMessages(ruleName, {
6
- rejected: value => {
7
- return `Do not use experimental variable \`var(--${value})\`. Experimental variables are undergoing testing, see https://github.com/github/primer/issues/889 for more details.`
8
- },
9
- })
10
-
11
- // eslint-disable-next-line no-unused-vars
12
- module.exports = stylelint.createPlugin(ruleName, (enabled, options = {}, context) => {
13
- if (!enabled) {
14
- return noop
15
- }
16
-
17
- // eslint-disable-next-line import/no-dynamic-require
18
- const designTokens = require(options.designTokens)
19
-
20
- let experimentalVars = null
21
- for (const tokenType of Object.keys(designTokens)) {
22
- experimentalVars = new Set(designTokens[tokenType].map(t => t['name']))
23
- }
24
-
25
- const lintResult = (root, result) => {
26
- root.walkDecls(decl => {
27
- for (const expVar of experimentalVars) {
28
- if (decl.value.includes(`var(--${expVar})`)) {
29
- stylelint.utils.report({
30
- index: declarationValueIndex(decl),
31
- message: messages.rejected(expVar),
32
- node: decl,
33
- result,
34
- ruleName,
35
- })
36
- }
37
- }
38
- })
39
- }
40
-
41
- return lintResult
42
- })
43
-
44
- function noop() {}
45
-
46
- module.exports.ruleName = ruleName
47
- module.exports.messages = messages
@@ -1,120 +0,0 @@
1
- const stylelint = require('stylelint')
2
- const {requirePrimerFile} = require('./lib/primer')
3
-
4
- const ruleName = 'primer/no-override'
5
- const CLASS_PATTERN = /(\.[-\w]+)/
6
- const CLASS_PATTERN_ALL = new RegExp(CLASS_PATTERN, 'g')
7
- const CLASS_PATTERN_ONLY = /^\.[-\w]+(:{1,2}[-\w]+)?$/
8
-
9
- module.exports = stylelint.createPlugin(ruleName, (enabled, options = {}) => {
10
- if (!enabled) {
11
- return noop
12
- }
13
-
14
- const {bundles = ['utilities'], ignoreSelectors = []} = options
15
-
16
- const isSelectorIgnored =
17
- typeof ignoreSelectors === 'function'
18
- ? ignoreSelectors
19
- : selector => {
20
- return ignoreSelectors.some(pattern => {
21
- return pattern instanceof RegExp ? pattern.test(selector) : selector.includes(pattern)
22
- })
23
- }
24
-
25
- const primerMeta = requirePrimerFile('dist/meta.json')
26
- const availableBundles = Object.keys(primerMeta.bundles)
27
-
28
- // These map selectors to the bundle in which they're defined.
29
- // If there's no entry for a given selector, it means that it's not defined
30
- // in one of the *specified* bundles, since we're iterating over the list of
31
- // bundle names in the options.
32
- const immutableSelectors = new Map()
33
- const immutableClassSelectors = new Map()
34
-
35
- for (const bundle of bundles) {
36
- if (!availableBundles.includes(bundle)) {
37
- continue
38
- }
39
- const stats = requirePrimerFile(`dist/stats/${bundle}.json`)
40
- const selectors = stats.selectors.values
41
- for (const selector of selectors) {
42
- immutableSelectors.set(selector, bundle)
43
- for (const classSelector of getClassSelectors(selector)) {
44
- immutableClassSelectors.set(classSelector, bundle)
45
- }
46
- }
47
- }
48
-
49
- const messages = stylelint.utils.ruleMessages(ruleName, {
50
- rejected: ({rule, selector, bundle}) => {
51
- const definedIn = ` (defined in @primer/css/${bundle})`
52
- const ruleSelector = collapseWhitespace(rule.selector)
53
- const context = selector === rule.selector ? '' : ` in "${ruleSelector}"`
54
- return `"${collapseWhitespace(selector)}" should not be overridden${context}${definedIn}.`
55
- },
56
- })
57
-
58
- return (root, result) => {
59
- if (!Array.isArray(bundles) || bundles.some(bundle => !availableBundles.includes(bundle))) {
60
- const invalidBundles = Array.isArray(bundles)
61
- ? `"${bundles.filter(bundle => !availableBundles.includes(bundle)).join('", "')}"`
62
- : '(not an array)'
63
- result.warn(`The "bundles" option must be an array of valid bundles; got: ${invalidBundles}`, {
64
- stylelintType: 'invalidOption',
65
- stylelintReference: 'https://github.com/primer/stylelint-config#options',
66
- })
67
- }
68
-
69
- const report = subject =>
70
- stylelint.utils.report({
71
- message: messages.rejected(subject),
72
- node: subject.rule,
73
- result,
74
- ruleName,
75
- })
76
-
77
- root.walkRules(rule => {
78
- const {selector} = rule
79
- if (immutableSelectors.has(selector)) {
80
- if (isClassSelector(selector)) {
81
- if (!isSelectorIgnored(selector)) {
82
- return report({
83
- rule,
84
- bundle: immutableSelectors.get(selector),
85
- selector,
86
- })
87
- }
88
- } else {
89
- // console.log(`not a class selector: "${selector}"`)
90
- }
91
- }
92
- for (const classSelector of getClassSelectors(selector)) {
93
- if (immutableClassSelectors.has(classSelector)) {
94
- if (!isSelectorIgnored(classSelector)) {
95
- return report({
96
- rule,
97
- bundle: immutableClassSelectors.get(classSelector),
98
- selector: classSelector,
99
- })
100
- }
101
- }
102
- }
103
- })
104
- }
105
- })
106
-
107
- function getClassSelectors(selector) {
108
- const match = selector.match(CLASS_PATTERN_ALL)
109
- return match ? [...match] : []
110
- }
111
-
112
- function isClassSelector(selector) {
113
- return CLASS_PATTERN_ONLY.test(selector)
114
- }
115
-
116
- function collapseWhitespace(str) {
117
- return str.trim().replace(/\s+/g, ' ')
118
- }
119
-
120
- function noop() {}
@@ -1,54 +0,0 @@
1
- const stylelint = require('stylelint')
2
- const matchAll = require('string.prototype.matchall')
3
-
4
- const ruleName = 'primer/no-scale-colors'
5
- const messages = stylelint.utils.ruleMessages(ruleName, {
6
- rejected: varName =>
7
- `${varName} is a non-functional scale color and cannot be used without being wrapped in the color-variables mixin`,
8
- })
9
-
10
- // Match CSS variable references (e.g var(--color-text-primary))
11
- // eslint-disable-next-line no-useless-escape
12
- const variableReferenceRegex = /var\(([^\),]+)(,.*)?\)/g
13
-
14
- module.exports = stylelint.createPlugin(ruleName, (enabled, options = {}) => {
15
- if (!enabled) {
16
- return noop
17
- }
18
-
19
- const {verbose = false} = options
20
- // eslint-disable-next-line no-console
21
- const log = verbose ? (...args) => console.warn(...args) : noop
22
-
23
- // Keep track of declarations we've already seen
24
- const seen = new WeakMap()
25
-
26
- return (root, result) => {
27
- root.walkRules(rule => {
28
- rule.walkDecls(decl => {
29
- if (seen.has(decl)) {
30
- return
31
- } else {
32
- seen.set(decl, true)
33
- }
34
-
35
- for (const [, variableName] of matchAll(decl.value, variableReferenceRegex)) {
36
- log(`Found variable reference ${variableName}`)
37
- if (variableName.match(/^--color-(scale|auto)-/)) {
38
- stylelint.utils.report({
39
- message: messages.rejected(variableName),
40
- node: decl,
41
- result,
42
- ruleName,
43
- })
44
- }
45
- }
46
- })
47
- })
48
- }
49
- })
50
-
51
- function noop() {}
52
-
53
- module.exports.ruleName = ruleName
54
- module.exports.messages = messages
@@ -1,121 +0,0 @@
1
- const fs = require('fs')
2
- const stylelint = require('stylelint')
3
- const matchAll = require('string.prototype.matchall')
4
- const globby = require('globby')
5
- const TapMap = require('tap-map')
6
-
7
- const ruleName = 'primer/no-undefined-vars'
8
- const messages = stylelint.utils.ruleMessages(ruleName, {
9
- rejected: varName => `${varName} is not defined`,
10
- })
11
-
12
- // Match CSS variable definitions (e.g. --color-text-primary:)
13
- const variableDefinitionRegex = /^\s*(--[\w|-]+):/gm
14
-
15
- // Match CSS variables defined with the color-variables mixin
16
- const colorModeVariableDefinitionRegex = /^[^/\n]*\(["']?([^'"\s,]+)["']?,\s*\(light|dark:/gm
17
-
18
- // Match CSS variable references (e.g var(--color-text-primary))
19
- // eslint-disable-next-line no-useless-escape
20
- const variableReferenceRegex = /var\(([^\),]+)(,.*)?\)/g
21
-
22
- module.exports = stylelint.createPlugin(ruleName, (enabled, options = {}) => {
23
- if (!enabled) {
24
- return noop
25
- }
26
-
27
- const {files = ['**/*.scss', '!node_modules'], verbose = false} = options
28
- // eslint-disable-next-line no-console
29
- const log = verbose ? (...args) => console.warn(...args) : noop
30
- const globalDefinedVariables = getDefinedVariables(files, log)
31
- // Keep track of declarations we've already seen
32
- const seen = new WeakMap()
33
-
34
- return (root, result) => {
35
- const fileDefinedVariables = new Set()
36
-
37
- function checkVariable(variableName, node, allowed) {
38
- if (!allowed.has(variableName)) {
39
- stylelint.utils.report({
40
- message: messages.rejected(variableName),
41
- node,
42
- result,
43
- ruleName,
44
- })
45
- }
46
- }
47
-
48
- root.walkAtRules(rule => {
49
- if (rule.name === 'include' && rule.params.startsWith('color-variables')) {
50
- const innerMatch = [...matchAll(rule.params, variableReferenceRegex)]
51
- if (!innerMatch.length) {
52
- return
53
- }
54
-
55
- for (const [, variableName] of innerMatch) {
56
- checkVariable(variableName, rule, new Set([...globalDefinedVariables, ...fileDefinedVariables]))
57
- }
58
- }
59
- })
60
-
61
- root.walkRules(rule => {
62
- const scopeDefinedVaribles = new Set()
63
-
64
- rule.walkDecls(decl => {
65
- // Add CSS variable declarations within the source text to the list of allowed variables
66
- if (decl.prop.startsWith('--')) {
67
- scopeDefinedVaribles.add(decl.prop)
68
- if (decl.parent.selector === ':root' || decl.parent.selector === ':host') {
69
- fileDefinedVariables.add(decl.prop)
70
- }
71
- }
72
-
73
- if (seen.has(decl)) {
74
- return
75
- } else {
76
- seen.set(decl, true)
77
- }
78
-
79
- for (const [, variableName] of matchAll(decl.value, variableReferenceRegex)) {
80
- checkVariable(
81
- variableName,
82
- decl,
83
- new Set([...globalDefinedVariables, ...fileDefinedVariables, ...scopeDefinedVaribles]),
84
- )
85
- }
86
- })
87
- })
88
- }
89
- })
90
-
91
- const cwd = process.cwd()
92
- const cache = new TapMap()
93
-
94
- function getDefinedVariables(globs, log) {
95
- const cacheKey = JSON.stringify({globs, cwd})
96
- return cache.tap(cacheKey, () => {
97
- const definedVariables = new Set()
98
-
99
- const files = globby.sync(globs)
100
- log(`Scanning ${files.length} SCSS files for CSS variables`)
101
- for (const file of files) {
102
- log(`==========\nLooking for CSS variable definitions in ${file}`)
103
- const css = fs.readFileSync(file, 'utf-8')
104
- for (const [, variableName] of matchAll(css, variableDefinitionRegex)) {
105
- log(`${variableName} defined in ${file}`)
106
- definedVariables.add(variableName)
107
- }
108
- for (const [, variableName] of matchAll(css, colorModeVariableDefinitionRegex)) {
109
- log(`--color-${variableName} defined via color-variables in ${file}`)
110
- definedVariables.add(`--color-${variableName}`)
111
- }
112
- }
113
-
114
- return definedVariables
115
- })
116
- }
117
-
118
- function noop() {}
119
-
120
- module.exports.ruleName = ruleName
121
- module.exports.messages = messages
@@ -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
- }