-
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.
Merge pull request #72 from primer/mkt/additional-css-color-var-checks
Add additional CSS variable/color mode checks
- Loading branch information
Showing
8 changed files
with
160 additions
and
10 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
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,8 @@ | ||
:root { | ||
@include color-mode-var('my-first-feature', var(--color-scale-blue-5), var(--color-scale-blue-3)); | ||
@include color-mode-var( | ||
'my-second-feature', | ||
var(--color-scale-green-5), | ||
var(--color-scale-red-3) | ||
); | ||
} |
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,33 @@ | ||
const path = require('path') | ||
const {messages, ruleName} = require('../plugins/no-scale-colors') | ||
|
||
testRule({ | ||
plugins: ['./plugins/no-scale-colors.js'], | ||
ruleName, | ||
config: [ | ||
true, | ||
{ | ||
files: [path.join(__dirname, '__fixtures__/color-vars.scss')] | ||
} | ||
], | ||
|
||
accept: [ | ||
{code: '.x { color: var(--color-text-primary); }'}, | ||
{code: '@include color-mode-var(my-feature, var(--color-scale-blue-1), var(--color-scale-blue-5))'} | ||
], | ||
|
||
reject: [ | ||
{ | ||
code: '.x { color: var(--color-scale-blue-1); }', | ||
message: messages.rejected('--color-scale-blue-1'), | ||
line: 1, | ||
column: 6 | ||
}, | ||
{ | ||
code: '.x { color: var(--color-auto-blue-1); }', | ||
message: messages.rejected('--color-auto-blue-1'), | ||
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
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,54 @@ | ||
const stylelint = require('stylelint') | ||
const matchAll = require('string.prototype.matchall') | ||
|
||
const ruleName = 'primer/no-scale-colors' | ||
const messages = stylelint.utils.ruleMessages(ruleName, { | ||
rejected: varName => | ||
`${varName} is a non-functional scale color and cannot be used without being wrapped in the color-mode-var mixin` | ||
}) | ||
|
||
// Match CSS variable references (e.g var(--color-text-primary)) | ||
// eslint-disable-next-line no-useless-escape | ||
const variableReferenceRegex = /var\(([^\),]+)(,.*)?\)/g | ||
|
||
module.exports = stylelint.createPlugin(ruleName, (enabled, options = {}) => { | ||
if (!enabled) { | ||
return noop | ||
} | ||
|
||
const {verbose = true} = 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(/^--color-(scale|auto)-/)) { | ||
stylelint.utils.report({ | ||
message: messages.rejected(variableName), | ||
node: decl, | ||
result, | ||
ruleName | ||
}) | ||
} | ||
} | ||
}) | ||
}) | ||
} | ||
}) | ||
|
||
function noop() {} | ||
|
||
module.exports.ruleName = ruleName | ||
module.exports.messages = messages |
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