Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions .changeset/pink-beds-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/stylelint-config': minor
---

Allowing to test background and border without color, failing on scale colors
54 changes: 54 additions & 0 deletions __tests__/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,60 @@ testRule({
endColumn: 44,
description: 'CSS > Errors when using a variable not in primitives',
},
{
code: '.x { background: var(--display-blue-scale-1); }',
unfixable: true,
message: messages.rejected('var(--display-blue-scale-1)', 'bg'),
line: 1,
column: 22,
endColumn: 44,
description: 'CSS > Errors when using a dispaly scale variable',
},
{
code: '.x { background-color: var(--display-blue-scale-1); }',
unfixable: true,
message: messages.rejected('var(--display-blue-scale-1)', 'bg'),
line: 1,
column: 28,
endColumn: 50,
description: 'CSS > Errors when using a dispaly scale variable',
},
{
code: '.x { background-image: linear-gradient(var(--display-blue-scale-1), var(--bgColor-default)); }',
unfixable: true,
message: messages.rejected('var(--display-blue-scale-1)', 'bg'),
line: 1,
column: 44,
endColumn: 66,
description: 'CSS > Errors when using a dispaly scale variable',
},
{
code: '.x { color: var(--display-blue-scale-1); }',
unfixable: true,
message: messages.rejected('var(--display-blue-scale-1)', 'fg'),
line: 1,
column: 17,
endColumn: 39,
description: 'CSS > Errors when using a dispaly scale variable',
},
{
code: '.x { color: var(--display-blue-scale-1); }',
unfixable: true,
message: messages.rejected('var(--display-blue-scale-1)', 'fg'),
line: 1,
column: 17,
endColumn: 39,
description: 'CSS > Errors when using a dispaly scale variable',
},
{
code: '.x { border-color: var(--display-blue-scale-9); }',
unfixable: true,
message: messages.rejected('var(--display-blue-scale-9)', 'border'),
line: 1,
column: 24,
endColumn: 46,
description: 'CSS > Errors when using a dispaly scale variable',
},
],
})

Expand Down
14 changes: 6 additions & 8 deletions plugins/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export const messages = ruleMessages(ruleName, {
let variables = primitivesVariables('colors')
const validProps = {
'^color$': ['fgColor', 'iconColor'],
'^background(-color)?$': ['bgColor'],
'^border(-top|-right|-bottom|-left|-inline|-block)*(-color)?$': ['borderColor'],
'^background(-color|-image)?$': ['bgColor'],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also test background-image

'^border(?:-top|-right|-bottom|-left|-inline|-block)?(?:-color)?$': ['borderColor'],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Enable testing for border

'^fill$': ['fgColor', 'iconColor', 'bgColor'],
'^stroke$': ['fgColor', 'iconColor', 'bgColor', 'borderColor'],
}
Expand All @@ -46,9 +46,9 @@ const validValues = [
const propType = prop => {
if (/^color/.test(prop)) {
return 'fg'
} else if (/^background(-color)?$/.test(prop)) {
} else if (/^background(-color|-image)?$/.test(prop)) {
return 'bg'
} else if (/^border(-top|-right|-bottom|-left|-inline|-block)*(-color)?$/.test(prop)) {
} else if (/^border(?:-top|-right|-bottom|-left|-inline|-block)?(?:-color)?$/.test(prop)) {
return 'border'
} else if (/^fill$/.test(prop)) {
return 'fg'
Expand Down Expand Up @@ -137,15 +137,13 @@ const ruleFunction = primary => {
) {
return
}

// Property is shortand and value doesn't include color
if (
(/^border(-top|-right|-bottom|-left|-inline|-block)*$/.test(prop) || /^background$/.test(prop)) &&
!valueNode.value.toLowerCase().includes('color')
(/^border(?:-top|-right|-bottom|-left|-inline|-block)?$/.test(prop) || /^background$/.test(prop)) &&
['color', 'scale'].every(word => !valueNode.value.toLowerCase().includes(word))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We now only return if neither color nor scale is in the token, because we want --display-blue-scale-0 to fail.

If this is risky we could also consider changing the name of the token to --display-blue-color-0 or --display-blue-colorScale-0 CC:@langermank

) {
return
}

report({
index: declarationValueIndex(declNode) + valueNode.sourceIndex,
endIndex: declarationValueIndex(declNode) + valueNode.sourceEndIndex,
Expand Down
Loading