-
-
Notifications
You must be signed in to change notification settings - Fork 82
Linter: Add html-no-underscores-in-attribute-names rule to linter
#446
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
marcoroth
merged 6 commits into
marcoroth:main
from
mickeytgl:linter-rule/html-no-underscores-in-attribute-names
Aug 30, 2025
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0bd8080
Add `html-no-underscores-in-attribute-names` rule to linter
mickeytgl 2b9f883
Leverage AttributeVisitorMixin for easier rule definition
mickeytgl e2b7ebc
Fix tests
mickeytgl feaf1d2
Leverage IdentityPrinter on `html-no-underscores-in-attribute-names` …
mickeytgl 6112b37
Update snapshots
mickeytgl 227e145
Update html-no-underscore-in-attribute-names.md
marcoroth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
javascript/packages/linter/docs/rules/html-no-underscores-in-attribute-names.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # Linter Rule: No underscores on attribute names | ||
|
|
||
| **Rule:** `html-no-underscores-in-attribute-names` | ||
|
|
||
| ## Description | ||
|
|
||
| --- | ||
|
|
||
| Warn when an HTML attribute name contains an underscore (_). According to the HTML specification, attribute names should use only lowercase letters, digits, hyphens (-), and colons (:) in specific namespaces (e.g., xlink:href in SVG). Underscores are not valid in standard HTML attribute names and may lead to unpredictable behavior or be ignored by browsers entirely. | ||
|
|
||
| ## Rationale | ||
|
|
||
| --- | ||
|
|
||
| Underscores in attribute names violate the HTML specification and are not supported in standard markup. Their use is almost always accidental (e.g., mistyping data-attr_name instead of data-attr-name) or stems from inconsistent naming conventions across backend or templating layers. | ||
|
|
||
| ## Examples | ||
|
|
||
| --- | ||
|
|
||
| ✅ Good | ||
|
|
||
| ```html | ||
| <div data-user-id="123"></div> | ||
| <img aria-label="Close"> | ||
| ``` | ||
|
|
||
| 🚫 Bad | ||
|
|
||
| ```html | ||
| <div data_user_id="123"></div> | ||
| <img aria_label="Close"> | ||
| ``` | ||
|
|
||
| ## References | ||
|
|
||
| --- | ||
|
|
||
| \- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
javascript/packages/linter/src/rules/html-no-underscores-in-attribute-names.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { ParserRule } from "../types.js" | ||
| import { | ||
| AttributeVisitorMixin, | ||
| StaticAttributeStaticValueParams, | ||
| StaticAttributeDynamicValueParams, | ||
| DynamicAttributeStaticValueParams, | ||
| DynamicAttributeDynamicValueParams | ||
| } from "./rule-utils.js" | ||
| import { getStaticContentFromNodes } from "@herb-tools/core" | ||
|
|
||
| import type { LintContext, LintOffense } from "../types.js" | ||
| import type { ParseResult, HTMLAttributeNode } from "@herb-tools/core" | ||
|
|
||
| class HTMLNoUnderscoresInAttributeNamesVisitor extends AttributeVisitorMixin { | ||
| protected checkStaticAttributeStaticValue({ attributeName, attributeNode }: StaticAttributeStaticValueParams): void { | ||
| this.check(attributeName, attributeNode) | ||
| } | ||
|
|
||
| protected checkStaticAttributeDynamicValue({ attributeName, attributeNode }: StaticAttributeDynamicValueParams): void { | ||
| this.check(attributeName, attributeNode) | ||
| } | ||
|
|
||
| protected checkDynamicAttributeStaticValue({ nameNodes, attributeNode }: DynamicAttributeStaticValueParams) { | ||
| const attributeName = getStaticContentFromNodes(nameNodes) | ||
|
|
||
| this.check(attributeName, attributeNode) | ||
| } | ||
|
|
||
| protected checkDynamicAttributeDynamicValue({ nameNodes, attributeNode }: DynamicAttributeDynamicValueParams) { | ||
| const attributeName = getStaticContentFromNodes(nameNodes) | ||
|
|
||
| this.check(attributeName, attributeNode) | ||
| } | ||
|
|
||
| private check(attributeName: string | null, attributeNode: HTMLAttributeNode): void { | ||
| if (!attributeName) return | ||
|
|
||
| if (attributeName.includes("_")) { | ||
| this.addOffense( | ||
| `Attribute \`${attributeName}\` should not contain underscores. Use hyphens (-) instead.`, | ||
| attributeNode.value!.location, | ||
| "warning" | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export class HTMLNoUnderscoresInAttributeNamesRule extends ParserRule { | ||
| name = "html-no-underscores-in-attribute-names" | ||
|
|
||
| check(result: ParseResult, context?: Partial<LintContext>): LintOffense[] { | ||
| const visitor = new HTMLNoUnderscoresInAttributeNamesVisitor(this.name, context) | ||
|
|
||
| visitor.visit(result.value) | ||
|
|
||
| return visitor.offenses | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
javascript/packages/linter/test/rules/html-no-underscores-in-attribute-names.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import dedent from "dedent" | ||
|
|
||
| import { describe, test, expect, beforeAll } from "vitest" | ||
| import { Herb } from "@herb-tools/node-wasm" | ||
| import { Linter } from "../../src/linter.js" | ||
|
|
||
| import { HTMLNoUnderscoresInAttributeNamesRule } from "../../src/rules/html-no-underscores-in-attribute-names.js" | ||
|
|
||
| describe("html-no-underscores-in-attribute-names", () => { | ||
| beforeAll(async () => { | ||
| await Herb.load() | ||
| }) | ||
|
|
||
| test("passes for valid attribute names (hyphens, letters, digits, colon)", () => { | ||
| const html = dedent` | ||
| <div data-user-id="123" aria-label="Close"></div> | ||
| <input data123-value="ok"> | ||
| ` | ||
|
|
||
| const linter = new Linter(Herb, [HTMLNoUnderscoresInAttributeNamesRule]) | ||
| const lintResult = linter.lint(html) | ||
|
|
||
| expect(lintResult.warnings).toBe(0) | ||
| expect(lintResult.offenses).toHaveLength(0) | ||
| }) | ||
|
|
||
| test("fails for attribute names with underscores", () => { | ||
| const html = dedent` | ||
| <div data_user_id="123"></div> | ||
| <img aria_label="Close"> | ||
| <custom-element custom_attr="x"></custom-element> | ||
| ` | ||
|
|
||
| const linter = new Linter(Herb, [HTMLNoUnderscoresInAttributeNamesRule]) | ||
| const lintResult = linter.lint(html) | ||
|
|
||
| expect(lintResult.warnings).toBe(3) | ||
| expect(lintResult.offenses).toHaveLength(3) | ||
|
|
||
| expect(lintResult.offenses[0].rule).toBe("html-no-underscores-in-attribute-names") | ||
| expect(lintResult.offenses[0].severity).toBe("warning") | ||
|
|
||
| expect(lintResult.offenses[0].message).toBe("Attribute `data_user_id` should not contain underscores. Use hyphens (-) instead.") | ||
| expect(lintResult.offenses[1].message).toBe("Attribute `aria_label` should not contain underscores. Use hyphens (-) instead.") | ||
| expect(lintResult.offenses[2].message).toBe("Attribute `custom_attr` should not contain underscores. Use hyphens (-) instead.") | ||
| }) | ||
|
|
||
| test("does not flag dynamic attribute names", () => { | ||
| const html = dedent` | ||
| <div data-<%= %>="value"></div> | ||
| <div <%= dynamic_name %>="value"></div> | ||
| <div data-<%= key %>-test="value"></div> | ||
| ` | ||
|
|
||
| const linter = new Linter(Herb, [HTMLNoUnderscoresInAttributeNamesRule]) | ||
| const lintResult = linter.lint(html) | ||
|
|
||
| expect(lintResult.warnings).toBe(0) | ||
| expect(lintResult.offenses).toHaveLength(0) | ||
| }) | ||
|
|
||
| test("fails for dynamic attribute names with underscores", () => { | ||
| const html = dedent` | ||
| <div data_<%= key %>="value"></div> | ||
| <div data_<%= key %>-test="value"></div> | ||
| <div data-<%= key %>_test="value"></div> | ||
| ` | ||
|
|
||
| const linter = new Linter(Herb, [HTMLNoUnderscoresInAttributeNamesRule]) | ||
| const lintResult = linter.lint(html) | ||
|
|
||
| expect(lintResult.warnings).toBe(3) | ||
| expect(lintResult.offenses).toHaveLength(3) | ||
|
|
||
| expect(lintResult.offenses[0].message).toBe("Attribute `data_` should not contain underscores. Use hyphens (-) instead.") | ||
| expect(lintResult.offenses[1].message).toBe("Attribute `data_-test` should not contain underscores. Use hyphens (-) instead.") | ||
| expect(lintResult.offenses[2].message).toBe("Attribute `data-_test` should not contain underscores. Use hyphens (-) instead.") | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.