Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
ota-meshi merged 4 commits into future-architect:master from toFrankie:master
Sep 4, 2025

Conversation

Copy link
Contributor

@toFrankie toFrankie commented Aug 28, 2025

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 standard class. This is useful for ecosystems derived from the Web, such as MiniApp platforms, where non-standard attributes like hover-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

  • Add new option: extraClassAttributes: string[]
    • Purpose: Specify attribute names (besides class) that should be treated as class-like and included in matching (e.g., hover-class, hoverClass, placeholder-class).
    • Default: [] (no behavior change unless configured)
    • Semantics: class is always checked; this option adds "extra" attributes to check in addition.
  • Documentation
    • Update docs/rules/no-unused-selector.md to document the option and provide usage examples.
  • Tests
    • Add valid and invalid cases for extraClassAttributes, covering static, dynamic, array, and object binding syntaxes.

Usage

Flat config:

import eslintPluginVueScopedCSS from 'eslint-plugin-vue-scoped-css'
export default [
 ...eslintPluginVueScopedCSS.configs['flat/recommended'],
 {
 rules: {
 'vue-scoped-css/no-unused-selector': ['error', {
 extraClassAttributes: ['hover-class'],
 }],
 },
 },
]

Legacy config:

module.exports = {
 parser: 'vue-eslint-parser',
 extends: ['plugin:vue-scoped-css/vue3-recommended'],
 rules: {
 'vue-scoped-css/no-unused-selector': ['error', {
 extraClassAttributes: ['hover-class'],
 }],
 },
}

Template example (e.g., MiniApp/uni-app):

<template>
 <view
 class="btn"
 :hover-class="disabled ? '' : 'btn-hover'"
 />
</template>
<style scoped>
.btn {}

.btn-hover {}
</style>

Impact

  • No change when the option is not configured.
  • Projects targeting MiniApp / uni-app and similar platforms can avoid false positives (e.g., .btn-hover combined with :hover-class) in no-unused-selector.

Copy link

changeset-bot bot commented Aug 28, 2025
edited
Loading

🦋 Changeset detected

Latest commit: 21ffedb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
eslint-plugin-vue-scoped-css Minor

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

Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

@ota-meshi ota-meshi left a 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!

@ota-meshi ota-meshi merged commit 990e68a into future-architect:master Sep 4, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@ota-meshi ota-meshi ota-meshi approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /