Skip to content

Linter: Add html-no-underscores-in-attribute-names rule to linter#446

Merged
marcoroth merged 6 commits intomarcoroth:mainfrom
mickeytgl:linter-rule/html-no-underscores-in-attribute-names
Aug 30, 2025
Merged

Linter: Add html-no-underscores-in-attribute-names rule to linter#446
marcoroth merged 6 commits intomarcoroth:mainfrom
mickeytgl:linter-rule/html-no-underscores-in-attribute-names

Conversation

@mickeytgl
Copy link
Contributor

Fixes #405

Introduced a rule to flag HTML attribute names containing underscores and recommend using hyphens instead. Included corresponding tests and updated the default rules list.

Copy link
Owner

@marcoroth marcoroth left a comment

Choose a reason for hiding this comment

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

Hey @mickeytgl, thank you so much for your contribution! This looks great, thank you for giving it a shot!

I recently added a new AttributeVisitorMixin that could allow to simplify the rule but also help to more strategically visit all kind of attribute name setups, since it also handles static/dynamic attribute names.

Sadly I haven't gotten to update all existing rules to use it, but I think it would be really good for this rule to make use of it. Here's an example: html-attribute-double-quotes.

For this rule we could do something like:

import { getStaticAttributeName, getStaticContentFromNodes } from "@herb-tools/core"
import { AttributeVisitorMixin } from "./rule-utils.js"

import type { StaticAttributeStaticValueParams, StaticAttributeDynamicValueParams, DynamicAttributeStaticValueParams, DynamicAttributeDynamicValueParams } from "./rule-utils.js"

class HTMLNoUnderscoresInAttributeNamesVisitor extends AttributeVisitorMixin {
  // <div data-class="value"></div>
  protected checkStaticAttributeStaticValue({ attributeName }: StaticAttributeStaticValueParams) {
    // attributeName => "data-class"
  }

  // <div data-class="<%= class %>"></div>
  protected checkStaticAttributeDynamicValue({ attributeName }: StaticAttributeDynamicValueParams) {
    // attributeName => "data-class"
  }

  // <div data-<%= dynamic %>="class"></div>
  protected checkDynamicAttributeStaticValue({ nameNodes }: DynamicAttributeStaticValueParams) {
    const attributeName = getStaticContentFromNodes(nameNodes)
    // attributeName => "data-"
  }

  // <div data-<%= dynamic %>="<%= dynamic %>"></div>
  protected checkDynamicAttributeDynamicValue({ nameNodes }: DynamicAttributeDynamicValueParams) {
    const attributeName = getStaticContentFromNodes(nameNodes)
    // attributeName => "data-"
  }
}

That would also allow to account for cases like:

<div data_<%= dynamic %>="value"></div>
<div data_<%= dynamic %>_attribute="value"></div>
<div <%= dynamic %>_attribute="value"></div>

Thank you!

Introduced a rule to flag HTML attribute names containing underscores and recommend using hyphens instead. Included corresponding tests and updated the default rules list.
@mickeytgl mickeytgl force-pushed the linter-rule/html-no-underscores-in-attribute-names branch from 3ba0a0b to 2b9f883 Compare August 29, 2025 14:04
@mickeytgl
Copy link
Contributor Author

@marcoroth I see some failing tests on CI, but I think it's just some flakiness? 🤔 Let me know if I missed something though

@marcoroth
Copy link
Owner

marcoroth commented Aug 29, 2025

@mickeytgl I think you just need to run yarn build to build the CLI and then run yarn test -u to update the snapshots in javascript/packages/linter/

mickeytgl and others added 2 commits August 30, 2025 07:31
Updated markdown formatting for code examples and descriptions.

Signed-off-by: Marco Roth <marco.roth@intergga.ch>
Copy link
Owner

@marcoroth marcoroth 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 this contribution, @mickeytgl! 🙏🏼

@marcoroth marcoroth merged commit bf544a3 into marcoroth:main Aug 30, 2025
1 check passed
@marcoroth marcoroth added feature New feature or request linter linter-rule labels Oct 24, 2025
@marcoroth marcoroth changed the title Add html-no-underscores-in-attribute-names rule to linter Linter: Add html-no-underscores-in-attribute-names rule to linter Oct 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request linter linter-rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Linter Rule: HTML attribute names must not contain underscores

2 participants