Skip to content

Commit 99c8ac7

Browse files
committed
fix(stylelint-config): let a hover that pins the look at rest pass and drop a11y/selector-pseudo-class-focus
1 parent 4999758 commit 99c8ac7

5 files changed

Lines changed: 80 additions & 3 deletions

File tree

‎packages/stylelint-config/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
},
4242
"dependencies": {
4343
"@stylistic/stylelint-plugin": "^6.0.1",
44+
"postcss-selector-parser": "^7.2.1",
4445
"postcss-value-parser": "^4.2.0",
4546
"stylelint-declaration-strict-value": "^1.13.0",
4647
"stylelint-gamut": "^2.0.2",

‎packages/stylelint-config/src/plugin/hover-in-media.js‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import selectorParser from 'postcss-selector-parser'
12
import stylelint from 'stylelint'
23
import {
34
isInMedia,
@@ -19,6 +20,52 @@ const messages = ruleMessages(ruleName, {
1920
})
2021
const HOVER_PATTERN = /:hover(?![\w-])/i
2122
const HOVER_FEATURE_PATTERN = /\(\s*(any-)?hover\s*:\s*hover\s*\)/i
23+
const STATES_PATTERN = /^:(is|where)$/i
24+
25+
function isHover(node) {
26+
return node.type === 'pseudo' && node.value.toLowerCase() === ':hover'
27+
}
28+
29+
// `:hover` itself, or an `:is()` or `:where()` that lists it among states
30+
function isHoverState(node) {
31+
return isHover(node) || (
32+
node.type === 'pseudo'
33+
&& STATES_PATTERN.test(node.value)
34+
&& node.nodes.some(state => state.nodes.length === 1 && isHover(state.first))
35+
)
36+
}
37+
38+
function normalize(selector) {
39+
return selector.toString().trim().replace(/\s+/g, ' ')
40+
}
41+
42+
/**
43+
* Check if every hovered selector of a list stands beside itself at rest, as in
44+
* `.item[aria-current], .item[aria-current]:is(:hover, :active)`: such a hover pins the look the
45+
* element has at rest against the states' rules, so a tap leaves nothing behind.
46+
* @param {string} selectorList - A rule's selector list.
47+
* @returns {boolean} Whether its hover only pins the look at rest.
48+
*/
49+
function isPinned(selectorList) {
50+
let selectors
51+
52+
try {
53+
selectors = selectorParser().astSync(selectorList).nodes
54+
} catch {
55+
return false
56+
}
57+
58+
const atRest = new Set(selectors.map(normalize))
59+
60+
return selectors.filter(selector => HOVER_PATTERN.test(selector.toString())).every((selector) => {
61+
const rest = selector.clone()
62+
const states = rest.nodes.filter(isHoverState)
63+
64+
states.forEach(state => state.remove())
65+
66+
return states.length > 0 && atRest.has(normalize(rest))
67+
})
68+
}
2269

2370
function ruleFunction(primary, secondaryOptions) {
2471
return (root, result) => {
@@ -44,7 +91,11 @@ function ruleFunction(primary, secondaryOptions) {
4491
const customMedia = toList(secondaryOptions?.customMedia)
4592

4693
root.walkRules((rule) => {
47-
if (HOVER_PATTERN.test(rule.selector) && !isInMedia(rule, HOVER_FEATURE_PATTERN, customMedia)) {
94+
if (
95+
HOVER_PATTERN.test(rule.selector)
96+
&& !isInMedia(rule, HOVER_FEATURE_PATTERN, customMedia)
97+
&& !isPinned(rule.selector)
98+
) {
4899
report({
49100
ruleName,
50101
result,

‎packages/stylelint-config/src/plugin/plugin.spec.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,27 @@ describe('stylelint-config', () => {
5858
'trigen/hover-in-media': true
5959
}, '.button:focus-visible { color: red; }')).toEqual([])
6060
})
61+
62+
it('should allow a hover that pins the look at rest', async () => {
63+
expect(await lint({
64+
'trigen/hover-in-media': true
65+
}, [
66+
'.item[aria-current], .item[aria-current]:is(:hover, :focus-visible, :active) { color: red; }',
67+
'.thumb::before, .thumb:hover::before { color: red; }'
68+
].join('\n'))).toEqual([])
69+
})
70+
71+
it('should report a hover beside another selector at rest', async () => {
72+
expect(await lint({
73+
'trigen/hover-in-media': true
74+
}, '.item, .other:is(:hover, :focus-visible) { color: red; }')).toEqual([message])
75+
})
76+
77+
it('should report a hover the selector at rest cannot drop', async () => {
78+
expect(await lint({
79+
'trigen/hover-in-media': true
80+
}, '.item, .item:not(:hover) { color: red; }')).toEqual([message])
81+
})
6182
})
6283

6384
describe('thin-scrollbar-in-media', () => {

‎packages/stylelint-config/src/subconfigs/common.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ export default {
3838
}
3939
],
4040
// Trigen
41-
// Hover styles in a hover media query: add a project's own media through `customMedia`
41+
// Hover styles in a hover media query: add a project's own media through `customMedia`. It
42+
// takes the place of a11y's `selector-pseudo-class-focus`, which asks for `:focus` in the list
43+
// of a `:hover` that now stands in a media query of its own
4244
'trigen/hover-in-media': true,
4345
// A11y
4446
'a11y/font-size-is-readable': true,
@@ -47,7 +49,6 @@ export default {
4749
'a11y/no-obsolete-element': true,
4850
'a11y/no-outline-none': true,
4951
'a11y/no-text-align-justify': true,
50-
'a11y/selector-pseudo-class-focus': true,
5152
// High performance animation
5253
'plugin/no-low-performance-animation-properties': [
5354
true,

‎pnpm-lock.yaml‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)