From 56b6c29025089dabaa162b6e90b8d9ac499eb25b Mon Sep 17 00:00:00 2001 From: Robin Date: Sat, 23 Sep 2023 09:10:49 -0400 Subject: [PATCH] fix(accidental-hover): support shorthand media features Media Queries Level 4 (https://drafts.csswg.org/mediaqueries-4/#mq-features) provides a shorthand syntax for boolean media features such as (hover: hover) - you can omit the feature value and write it simply as (hover). --- src/rules/use-defensive-css/index.js | 5 ++++- src/rules/use-defensive-css/index.test.js | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/rules/use-defensive-css/index.js b/src/rules/use-defensive-css/index.js index 88bda0a..9f790ab 100644 --- a/src/rules/use-defensive-css/index.js +++ b/src/rules/use-defensive-css/index.js @@ -38,7 +38,10 @@ function traverseParentRules(parent) { } if (parent.parent.type === 'atrule') { - if (parent.parent.params && parent.parent.params.includes('hover: hover')) { + if ( + parent.parent.params && + /\(hover(: hover)?\)/.test(parent.parent.params) + ) { isWrappedInHoverAtRule = true; } else { traverseParentRules(parent.parent); diff --git a/src/rules/use-defensive-css/index.test.js b/src/rules/use-defensive-css/index.test.js index 340041e..f708e17 100644 --- a/src/rules/use-defensive-css/index.test.js +++ b/src/rules/use-defensive-css/index.test.js @@ -14,6 +14,10 @@ testRule({ code: `@media (hover: hover) { .btn:hover { color: black; } }`, description: 'Use media query for button hover state.', }, + { + code: `@media (hover) { .btn:hover { color: black; } }`, + description: 'Use shorthand media query for button hover state.', + }, { code: `@media (min-width: 1px) { @media (hover: hover) { .btn:hover { color: black; } } }`, description: 'Use nested media queries for button hover state.',