-
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 rule: safegaurd
display
color tokens (#388)
* add temp rule to safegaurd display colors * add a test for typography * Create cyan-bugs-cross.md
- Loading branch information
1 parent
e08fb07
commit 43b1066
Showing
5 changed files
with
96 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": patch | ||
--- | ||
|
||
New rule: safegaurd alpha `display` color tokens |
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,34 @@ | ||
const path = require('path') | ||
const {messages, ruleName} = require('../plugins/no-display-colors') | ||
|
||
// eslint-disable-next-line no-undef | ||
testRule({ | ||
plugins: ['./plugins/no-display-colors.js'], | ||
ruleName, | ||
config: [ | ||
true, | ||
{ | ||
files: [path.join(__dirname, '__fixtures__/color-vars.scss')], | ||
}, | ||
], | ||
|
||
accept: [ | ||
{code: '.x { color: var(--fgColor-accent); }'}, | ||
{code: '.x { line-height: var(--text-display-lineHeight); }'}, | ||
], | ||
|
||
reject: [ | ||
{ | ||
code: '.x { color: var(--display-blue-fgColor); }', | ||
message: messages.rejected('--display-blue-fgColor'), | ||
line: 1, | ||
column: 6, | ||
}, | ||
{ | ||
code: '.x { color: var(--display-yellow-bgColor-emphasis); }', | ||
message: messages.rejected('--display-yellow-bgColor-emphasis'), | ||
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,53 @@ | ||
const stylelint = require('stylelint') | ||
const matchAll = require('string.prototype.matchall') | ||
|
||
const ruleName = 'primer/no-display-colors' | ||
const messages = stylelint.utils.ruleMessages(ruleName, { | ||
rejected: varName => `${varName} is in alpha and should be used with caution with approval from the Primer team`, | ||
}) | ||
|
||
// Match CSS variable references (e.g var(--display-blue-fgColor)) | ||
// eslint-disable-next-line no-useless-escape | ||
const variableReferenceRegex = /var\(([^\),]+)(,.*)?\)/g | ||
|
||
module.exports = stylelint.createPlugin(ruleName, (enabled, options = {}) => { | ||
if (!enabled) { | ||
return noop | ||
} | ||
|
||
const {verbose = false} = options | ||
// eslint-disable-next-line no-console | ||
const log = verbose ? (...args) => console.warn(...args) : noop | ||
|
||
// Keep track of declarations we've already seen | ||
const seen = new WeakMap() | ||
|
||
return (root, result) => { | ||
root.walkRules(rule => { | ||
rule.walkDecls(decl => { | ||
if (seen.has(decl)) { | ||
return | ||
} else { | ||
seen.set(decl, true) | ||
} | ||
|
||
for (const [, variableName] of matchAll(decl.value, variableReferenceRegex)) { | ||
log(`Found variable reference ${variableName}`) | ||
if (variableName.match(/^--display-.*/)) { | ||
stylelint.utils.report({ | ||
message: messages.rejected(variableName), | ||
node: decl, | ||
result, | ||
ruleName, | ||
}) | ||
} | ||
} | ||
}) | ||
}) | ||
} | ||
}) | ||
|
||
function noop() {} | ||
|
||
module.exports.ruleName = ruleName | ||
module.exports.messages = messages |