Skip to content

Commit

Permalink
Updating no-deprecated-colors plugin for edge cases (#132)
Browse files Browse the repository at this point in the history
* Checking sass variables

* Make sure we replace multiple in same line

* Create famous-ladybugs-report.md

* Accounting for mixins also

* 5.0

* don't replace all
  • Loading branch information
jonrohan authored Oct 1, 2021
1 parent d87fdb6 commit b672367
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/famous-ladybugs-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/stylelint-config": patch
---

Updating no-deprecated-colors plugin for edge cases
4 changes: 3 additions & 1 deletion __tests__/__fixtures__/deprecations.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"text.primary": "fg.default",
"text.secondary": ["fg.one", "fg.two"],
"text.white": null
"text.white": null,
"border.primary": "border.default",
"border.secondary": "border.subtle"
}
82 changes: 82 additions & 0 deletions __tests__/no-deprecated-colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,88 @@ testRule({
line: 1,
column: 6
},
{
code: '@mixin double-caret($border: var(--color-border-primary)) { }',
fixed: '@mixin double-caret($border: var(--color-border-default)) { }',
message: `--color-border-primary is a deprecated color variable. Please use the replacement --color-border-default. (primer/no-deprecated-colors)`,
line: 1,
column: 1
},
{
code: '@mixin double-caret() { border-color: var(--color-border-primary); }',
fixed: '@mixin double-caret() { border-color: var(--color-border-default); }',
message: `--color-border-primary is a deprecated color variable. Please use the replacement --color-border-default. (primer/no-deprecated-colors)`,
line: 1,
column: 25
},
{
code: '.x { border: 1px solid var(--color-text-primary); .foo { color: var(--color-text-primary); } }',
fixed: '.x { border: 1px solid var(--color-fg-default); .foo { color: var(--color-fg-default); } }',
warnings: [
{
message:
'--color-text-primary is a deprecated color variable. Please use the replacement --color-fg-default. (primer/no-deprecated-colors)',
line: 1,
column: 6
},
{
message:
'--color-text-primary is a deprecated color variable. Please use the replacement --color-fg-default. (primer/no-deprecated-colors)',
line: 1,
column: 58
}
]
},
{
code: '.x { border-color: var(--color-border-primary) var(--color-border-primary); }',
fixed: '.x { border-color: var(--color-border-default) var(--color-border-default); }',
warnings: [
{
message:
'--color-border-primary is a deprecated color variable. Please use the replacement --color-border-default. (primer/no-deprecated-colors)',
line: 1,
column: 6
},
{
message:
'--color-border-primary is a deprecated color variable. Please use the replacement --color-border-default. (primer/no-deprecated-colors)',
line: 1,
column: 6
}
]
},
{
code: '.x { border-color: var(--color-border-primary) var(--color-border-secondary); }',
fixed: '.x { border-color: var(--color-border-default) var(--color-border-subtle); }',
warnings: [
{
message:
'--color-border-primary is a deprecated color variable. Please use the replacement --color-border-default. (primer/no-deprecated-colors)',
line: 1,
column: 6
},
{
message:
'--color-border-secondary is a deprecated color variable. Please use the replacement --color-border-subtle. (primer/no-deprecated-colors)',
line: 1,
column: 6
}
]
},
{
code: '$border: $border-width $border-style var(--color-border-primary) !default;',
fixed: '$border: $border-width $border-style var(--color-border-default) !default;',
message: `--color-border-primary is a deprecated color variable. Please use the replacement --color-border-default. (primer/no-deprecated-colors)`,
line: 1,
column: 1
},
{
code: '.x { border: $border-width $border-style var(--color-border-primary); }',
fixed: '.x { border: $border-width $border-style var(--color-border-default); }',
message: `--color-border-primary is a deprecated color variable. Please use the replacement --color-border-default. (primer/no-deprecated-colors)`,
line: 1,
column: 6
},
{
code: '.x { background-color: var(--color-bg-canvas); }',
fixed: '.x { background-color: var(--color-canvas-default); }',
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@
},
"peerDependencies": {
"@primer/css": "*",
"@primer/primitives": "^5.0.0-rc.8de08c0"
"@primer/primitives": "^5.0.0"
},
"devDependencies": {
"@changesets/changelog-github": "0.4.1",
"@changesets/cli": "2.17.0",
"@github/prettier-config": "0.0.4",
"@primer/css": "^13.2.0",
"@primer/primitives": "^5.0.0-rc.8de08c0",
"@primer/primitives": "^5.0.0",
"dedent": "0.7.0",
"eslint": "7.32.0",
"eslint-plugin-github": "4.3.0",
Expand Down
62 changes: 36 additions & 26 deletions plugins/no-deprecated-colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,45 @@ module.exports = stylelint.createPlugin(ruleName, (enabled, options = {}, contex
}, {})

const lintResult = (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)) {
if (variableName in convertedCSSVars) {
let replacement = convertedCSSVars[variableName]

if (context.fix && replacement !== null && !Array.isArray(replacement)) {
replacement = `--color-${kebabCase(replacement)}`
replacedVars[variableName] = true
newVars[replacement] = true
decl.value = decl.value.replace(variableName, replacement)
return
// Walk all declarartions
root.walk(node => {
if (seen.has(node)) {
return
} else {
seen.set(node, true)
}
// walk these nodes
if (!(node.type === 'decl' || node.type === 'atrule')) {
return
}

for (const [, variableName] of matchAll(
node.type === 'atrule' ? node.params : node.value,
variableReferenceRegex
)) {
if (variableName in convertedCSSVars) {
let replacement = convertedCSSVars[variableName]

if (context.fix && replacement !== null && !Array.isArray(replacement)) {
replacement = `--color-${kebabCase(replacement)}`
replacedVars[variableName] = true
newVars[replacement] = true
if (node.type === 'atrule') {
node.params = node.params.replace(variableName, replacement)
} else {
node.value = node.value.replace(variableName, replacement)
}

stylelint.utils.report({
message: messages.rejected(variableName, replacement),
node: decl,
ruleName,
result
})
continue
}

stylelint.utils.report({
message: messages.rejected(variableName, replacement),
node,
ruleName,
result
})
}
})
}
})
}

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -792,10 +792,10 @@
dependencies:
object-assign "^4.1.1"

"@primer/primitives@^5.0.0-rc.8de08c0":
version "5.0.0-rc.8de08c0"
resolved "https://registry.yarnpkg.com/@primer/primitives/-/primitives-5.0.0-rc.8de08c0.tgz#b85825eb55d8b364ca31cb573e66e2caae63137a"
integrity sha512-4qjGx4ec3FuAU/7sKjsVzDXMM8ZbFW/VstxmCwaraN6hXfgruZrGY0HlEuhzor0xenhdbxMZllsPoz6FxFIxNQ==
"@primer/primitives@^5.0.0":
version "5.0.0"
resolved "https://registry.yarnpkg.com/@primer/primitives/-/primitives-5.0.0.tgz#20e5e7aae0464093f4742f5947c332cbcbc68edd"
integrity sha512-g2omeDBgfE5Q6+BQxPaflz5/shCFNjPvfpzphQMpeqIeSrV9boFyicLq7/Rd3WdsDvIMIsMCC1lWZE9JyEhR3w==

"@sinonjs/commons@^1.7.0":
version "1.8.3"
Expand Down

0 comments on commit b672367

Please sign in to comment.