Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no-deprecated-colors require fallback for new color tokens #375

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 58 additions & 36 deletions __tests__/no-deprecated-colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,65 @@ testRule({
config: [
true,
{
deprecatedFile: path.join(__dirname, '../plugins/lib/primitives-v8.json'),
},
deprecatedFile: path.join(__dirname, '../plugins/lib/primitives-v8.json')
}
],
fix: true,
accept: [
{code: '.x { color: var(--fgColor-default); }'},
{
code: '@include focusOutline(2px, var(--focus-outlineColor));',
},
code: '@include focusOutline(2px, var(--focus-outlineColor));'
}
],
reject: [
{
code: '.x { color: var(--color-fg-default); }',
fixed: '.x { color: var(--fgColor-default); }',
message: `Variable --color-fg-default is deprecated for property color. Please use the replacement --fgColor-default. (primer/no-deprecated-colors)`,
line: 1,
column: 6,
column: 6
},
{
code: '.x { border-right: $border-width $border-style var(--color-border-muted); }',
fixed: '.x { border-right: $border-width $border-style var(--borderColor-muted); }',
message: `Variable --color-border-muted is deprecated for property border-right. Please use the replacement --borderColor-muted. (primer/no-deprecated-colors)`,
line: 1,
column: 6,
column: 6
},
{
code: '.x { border-color: var(--color-primer-border-contrast); }',
fixed: '.x { border-color: var(--borderColor-muted); }',
message: `Variable --color-primer-border-contrast is deprecated for property border-color. Please use the replacement --borderColor-muted. (primer/no-deprecated-colors)`,
line: 1,
column: 6,
column: 6
},
{
code: '.x { box-shadow: var(--color-fg-default); }',
unfixable: true,
message: `Variable --color-fg-default is deprecated for property box-shadow. Please consult the primer color docs for a replacement. https://primer.style/primitives/storybook/?path=/story/migration-tables (primer/no-deprecated-colors)`,
line: 1,
column: 6,
column: 6
},
{
code: '.x { background-color: var(--color-canvas-default-transparent); }',
fixed: '.x { background-color: var(--bgColor-transparent); }',
message: `Variable --color-canvas-default-transparent is deprecated for property background-color. Please use the replacement --bgColor-transparent. (primer/no-deprecated-colors)`,
line: 1,
column: 6,
column: 6
},
{
code: '.x { border: var(--borderWidth-thin) solid var(--color-border-default); }',
fixed: '.x { border: var(--borderWidth-thin) solid var(--borderColor-default); }',
message: `Variable --color-border-default is deprecated for property border. Please use the replacement --borderColor-default. (primer/no-deprecated-colors)`,
line: 1,
column: 6,
column: 6
},
{
code: '.x { border-color: var(--color-canvas-default-transparent); }',
fixed: '.x { border-color: var(--borderColor-transparent); }',
message: `Variable --color-canvas-default-transparent is deprecated for property border-color. Please use the replacement --borderColor-transparent. (primer/no-deprecated-colors)`,
line: 1,
column: 6,
column: 6
},
{
code: '.x { border: 1px solid var(--color-neutral-emphasis); .foo { background-color: var(--color-neutral-emphasis); } }',
Expand All @@ -76,25 +76,25 @@ testRule({
true,
{
deprecatedFile: path.join(__dirname, '../plugins/lib/primitives-v8.json'),
inlineFallback: false,
},
inlineFallback: false
}
],
warnings: [
{
message:
'Variable --color-neutral-emphasis is deprecated for property border. Please use the replacement --borderColor-neutral-emphasis. (primer/no-deprecated-colors)',
line: 1,
column: 6,
column: 6
},
{
message:
'Variable --color-neutral-emphasis is deprecated for property background-color. Please use the replacement --bgColor-neutral-emphasis. (primer/no-deprecated-colors)',
line: 1,
column: 62,
},
],
},
],
column: 62
}
]
}
]
})
// eslint-disable-next-line no-undef
testRule({
Expand All @@ -104,58 +104,58 @@ testRule({
true,
{
deprecatedFile: path.join(__dirname, '../plugins/lib/primitives-v8.json'),
inlineFallback: true,
},
inlineFallback: true
}
],
fix: true,
accept: [
{code: '.x { color: var(--fgColor-default, var(--color-fg-default)); }'},
{
code: '@include focusOutline(2px, var(--focus-outlineColor, var(--color-accent-fg)));',
},
code: '@include focusOutline(2px, var(--focus-outlineColor, var(--color-accent-fg)));'
}
],
reject: [
{
code: '.x { color: var(--color-fg-default); }',
fixed: '.x { color: var(--fgColor-default, var(--color-fg-default)); }',
message: `Variable --color-fg-default is deprecated for property color. Please use the replacement --fgColor-default. (primer/no-deprecated-colors)`,
line: 1,
column: 6,
column: 6
},
{
code: '.x { border-right: $border-width $border-style var(--color-border-muted); }',
fixed: '.x { border-right: $border-width $border-style var(--borderColor-muted, var(--color-border-muted)); }',
message: `Variable --color-border-muted is deprecated for property border-right. Please use the replacement --borderColor-muted. (primer/no-deprecated-colors)`,
line: 1,
column: 6,
column: 6
},
{
code: '.x { border-color: var(--color-primer-border-contrast); }',
fixed: '.x { border-color: var(--borderColor-muted, var(--color-primer-border-contrast)); }',
message: `Variable --color-primer-border-contrast is deprecated for property border-color. Please use the replacement --borderColor-muted. (primer/no-deprecated-colors)`,
line: 1,
column: 6,
column: 6
},
{
code: '.x { background-color: var(--color-canvas-default-transparent); }',
fixed: '.x { background-color: var(--bgColor-transparent, var(--color-canvas-default-transparent)); }',
message: `Variable --color-canvas-default-transparent is deprecated for property background-color. Please use the replacement --bgColor-transparent. (primer/no-deprecated-colors)`,
line: 1,
column: 6,
column: 6
},
{
code: '.x { border: var(--borderWidth-thin) solid var(--color-border-default); }',
fixed: '.x { border: var(--borderWidth-thin) solid var(--borderColor-default, var(--color-border-default)); }',
message: `Variable --color-border-default is deprecated for property border. Please use the replacement --borderColor-default. (primer/no-deprecated-colors)`,
line: 1,
column: 6,
column: 6
},
{
code: '.x { border-color: var(--color-canvas-default-transparent); }',
fixed: '.x { border-color: var(--borderColor-transparent, var(--color-canvas-default-transparent)); }',
message: `Variable --color-canvas-default-transparent is deprecated for property border-color. Please use the replacement --borderColor-transparent. (primer/no-deprecated-colors)`,
line: 1,
column: 6,
column: 6
},
{
code: '.x { border: 1px solid var(--color-neutral-emphasis); .foo { background-color: var(--color-neutral-emphasis); } }',
Expand All @@ -165,23 +165,45 @@ testRule({
true,
{
deprecatedFile: path.join(__dirname, '../plugins/lib/primitives-v8.json'),
inlineFallback: false,
},
inlineFallback: false
}
],
warnings: [
{
message:
'Variable --color-neutral-emphasis is deprecated for property border. Please use the replacement --borderColor-neutral-emphasis. (primer/no-deprecated-colors)',
line: 1,
column: 6,
column: 6
},
{
message:
'Variable --color-neutral-emphasis is deprecated for property background-color. Please use the replacement --bgColor-neutral-emphasis. (primer/no-deprecated-colors)',
line: 1,
column: 62,
},
],
},
column: 62
}
]
}
]
})
// eslint-disable-next-line no-undef
testRule({
Comment on lines +187 to +189
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't these be combined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to disable the fixable part but maybe it doesn't matter 😅 see what I mean, I've made a huge mess

plugins: ['./plugins/no-deprecated-colors.js'],
ruleName,
config: [
true,
{
deprecatedFile: path.join(__dirname, '../plugins/lib/primitives-v8.json'),
inlineFallback: true
}
],
fix: false,
accept: [{code: '.x { color: var(--fgColor-default, var(--color-fg-default)); }'}],
reject: [
{
code: '.x { color: var(--fgColor-default); }',
message: `Variable --fgColor-default is new color token and must have a fallback value`,
line: 1,
column: 6
}
]
})
16 changes: 13 additions & 3 deletions plugins/no-deprecated-colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const messages = stylelint.utils.ruleMessages(ruleName, {
}

return `Variable ${varName} is deprecated for property ${property}. Please use the replacement ${replacement}.`
},
}
})

// Match CSS variable references (e.g var(--color-text-primary))
Expand Down Expand Up @@ -63,6 +63,16 @@ module.exports = stylelint.createPlugin(ruleName, (enabled, options = {}, contex
}
}
}
if (inlineFallback && !replacement.includes(',')) {
// report error if inlineFallback is true and replacement does not have a fallback
stylelint.utils.report({
message: messages.rejected(variableName, replacement, node.prop),
node,
ruleName,
result
})
langermank marked this conversation as resolved.
Show resolved Hide resolved
continue
}
if (typeof replacement === 'object') {
replacement = null
}
Expand All @@ -84,15 +94,15 @@ module.exports = stylelint.createPlugin(ruleName, (enabled, options = {}, contex
message: messages.rejected(variableName, replacement, node.prop),
node,
ruleName,
result,
result
})
}
}
})
}

log(
`${Object.keys(replacedVars).length} deprecated variables replaced with ${Object.keys(newVars).length} variables.`,
`${Object.keys(replacedVars).length} deprecated variables replaced with ${Object.keys(newVars).length} variables.`
)
return lintResult
})
Expand Down