-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New plugin:
new-color-vars-have-fallback
(#376)
* babys first solo stylelint plugin * lint * Create beige-cobras-reflect.md * try adding severity * fix tests
- Loading branch information
1 parent
4bc52ee
commit a31e0d3
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@primer/stylelint-config": minor | ||
--- | ||
|
||
Adds new plugin: `new-color-vars-have-fallback` to check that if new Primitive v8 colors are used, they have a fallback value. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const {ruleName} = require('../plugins/new-color-vars-have-fallback') | ||
|
||
// eslint-disable-next-line no-undef | ||
testRule({ | ||
plugins: ['./plugins/new-color-vars-have-fallback'], | ||
customSyntax: 'postcss-scss', | ||
ruleName, | ||
config: [true], | ||
accept: [ | ||
{ | ||
code: '.x { color: var(--fgColor-default, var(--color-fg-default)); }', | ||
description: 'Variable has fallback', | ||
}, | ||
], | ||
reject: [ | ||
{ | ||
code: '.x { color: var(--fgColor-default); }', | ||
message: | ||
'Expected a fallback value for CSS variable --fgColor-default. New color variables fallbacks, check primer.style/primitives to find the correct value (primer/new-color-vars-have-fallback)', | ||
line: 1, | ||
column: 6, | ||
}, | ||
], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const stylelint = require('stylelint') | ||
|
||
const ruleName = 'primer/new-color-vars-have-fallback' | ||
const messages = stylelint.utils.ruleMessages(ruleName, { | ||
expectedFallback: variable => | ||
`Expected a fallback value for CSS variable ${variable}. New color variables fallbacks, check primer.style/primitives to find the correct value`, | ||
}) | ||
|
||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
const jsonFilePath = 'node_modules/@primer/primitives/tokens-next-private/docs/functional/themes/light.json' | ||
let jsonContent | ||
|
||
try { | ||
const fileContent = fs.readFileSync(path.resolve(jsonFilePath), 'utf8') | ||
jsonContent = JSON.parse(fileContent) | ||
// console.log('JSON content:', jsonContent) // Log to see the content | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error('Error reading JSON file:', error) | ||
} | ||
|
||
module.exports = stylelint.createPlugin(ruleName, enabled => { | ||
if (!enabled) { | ||
return noop | ||
} | ||
|
||
return (root, result) => { | ||
root.walkDecls(decl => { | ||
for (const key of Object.keys(jsonContent)) { | ||
if (decl.value.includes(`var(--${key})`)) { | ||
// Check if the declaration uses a CSS variable from the JSON | ||
const match = decl.value.match(/var\(--\w+,(.*)\)/) | ||
if (!match || match[1].trim() === '') { | ||
stylelint.utils.report({ | ||
ruleName, | ||
result, | ||
node: decl, | ||
message: messages.expectedFallback(`--${key}`), | ||
}) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
}) | ||
|
||
function noop() {} | ||
|
||
module.exports.ruleName = ruleName | ||
module.exports.messages = messages |