-
-
Notifications
You must be signed in to change notification settings - Fork 14
Add extraClassAttributes option to match additional class-like attributes in templates #413
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
Conversation
🦋 Changeset detectedLatest commit: 21ffedb The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Thanks for submitting the PR.
However, I'm still not sure what you mean by "class-like". Does that mean that in the app you're using, the hover-class
attribute is the same as the class
attribute?
Also, I'm completely unfamiliar with DCloud
, so if that knowledge is needed, could you explain it further?
I’d like to add some context on why the extraClassAttributes
option is useful:
With the current rule:
<template> <div class="button" data-class="other"></div> </template> <style scoped> /* good */ .button {} /* bad: `.other` is reported as unused */ .other {} </style>
Although .other
is actually used, eslint-plugin-vue-scoped-css only inspects the class
attribute (for class selectors), so it reports .other
as unused.
If we add a configuration like this:
import eslintPluginVueScopedCSS from 'eslint-plugin-vue-scoped-css' export default [ ...eslintPluginVueScopedCSS.configs['flat/recommended'], { rules: { 'vue-scoped-css/no-unused-selector': [ 'error', { extraClassAttributes: ['data-class'], // It can be data-class, hover-class, or something else. }, ], }, }, ]
The plugin will check both class
and data-class
, and .other
will no longer be reported as unused.
Why support attributes like hover-class
?
As mentioned earlier, hover-class
comes from MiniApp — a web-derivative application model that is very popular in China.
• In addition to standard HTML attributes (like class
), MiniApp introduces some non-standard ones (such as hover-class
).
• These attributes act as syntactic sugar for standard web features. For example:
<template> <div class="button" hover-class="button-hover"></div> </template> <style scoped> .button {} .button-hover {} </style>
is equivalent to the standard web usage with a pseudo-class:
<template> <div class="button"></div> </template> <style scoped> .button {} .button:hover {} </style>
The extraClassAttributes
option can provide better support for platforms that are built on top of standard Web (e.g. MiniApp). Standard Web applications do not need to configure this option.
Note: This PR is not related to DCloud. DCloud's uni-app framework does support writing MiniApp apps with Vue.js, but attributes like
hover-class
are part of the MiniApp platform itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the explanation and thank you for the PR! LGTM!
990e68a
into
future-architect:master
Summary
This PR adds a new configuration option to the
vue-scoped-css/no-unused-selector
rule that allows matching "class-like" attributes in addition to the standardclass
. This is useful for ecosystems derived from the Web, such as MiniApp platforms, where non-standard attributes likehover-class
are commonly used. Frameworks like DCloud's uni-app make it possible to write Vue once and target multiple platforms (MiniApp, Web, Native), hence the need for this option.Motivation
MiniApps (a Web-derived application form popular in China) diverge from the standard Web in certain template attributes. For example, they support attributes such as
hover-class
. On top of these ecosystems, frameworks like DCloud’s uni-app help developers write cross-platform apps using Vue syntax for MiniApp, standard Web, and Native. To better support such Web-derived ecosystems, this rule should be able to treat specific non-standard attributes as class-like when checking unused selectors.Changes
extraClassAttributes: string[]
class
) that should be treated as class-like and included in matching (e.g.,hover-class
,hoverClass
,placeholder-class
).[]
(no behavior change unless configured)class
is always checked; this option adds "extra" attributes to check in addition.docs/rules/no-unused-selector.md
to document the option and provide usage examples.extraClassAttributes
, covering static, dynamic, array, and object binding syntaxes.Usage
Flat config:
Legacy config:
Template example (e.g., MiniApp/uni-app):
Impact
.btn-hover
combined with:hover-class
) inno-unused-selector
.