-
-
Notifications
You must be signed in to change notification settings - Fork 400
feat: new rule attr-value-no-duplication
#1650
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
coliff
merged 2 commits into
main
from
dev/coliff/feat-new-rule-attr-value-no-duplication
Jun 11, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,37 @@ | ||
import { Rule } from '../types' | ||
|
||
export default { | ||
id: 'attr-value-no-duplication', | ||
description: 'Attribute values should not contain duplicates.', | ||
init(parser, reporter) { | ||
parser.addListener('tagstart', (event) => { | ||
const attrs = event.attrs | ||
let attr | ||
const col = event.col + event.tagName.length + 1 | ||
|
||
for (let i = 0, l = attrs.length; i < l; i++) { | ||
attr = attrs[i] | ||
|
||
if (attr.value) { | ||
// Split attribute value by whitespace to get individual values | ||
const values = attr.value.trim().split(/\s+/) | ||
const duplicateMap: { [value: string]: boolean } = {} | ||
|
||
for (const value of values) { | ||
if (duplicateMap[value] === true) { | ||
reporter.error( | ||
`Duplicate value [ ${value} ] was found in attribute [ ${attr.name} ].`, | ||
event.line, | ||
col + attr.index, | ||
this, | ||
attr.raw | ||
) | ||
break // Only report the first duplicate found per attribute | ||
} | ||
duplicateMap[value] = true | ||
} | ||
} | ||
} | ||
}) | ||
}, | ||
} as Rule |
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
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
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
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,67 @@ | ||
const HTMLHint = require('../../dist/htmlhint.js').HTMLHint | ||
|
||
const ruleId = 'attr-value-no-duplication' | ||
const ruleOptions = {} | ||
|
||
ruleOptions[ruleId] = true | ||
|
||
describe(`Rules: ${ruleId}`, () => { | ||
it('Duplicate values in class attribute should result in an error', () => { | ||
const code = '<div class="d-none small d-none">Test</div>' | ||
const messages = HTMLHint.verify(code, ruleOptions) | ||
expect(messages.length).toBe(1) | ||
expect(messages[0].rule.id).toBe(ruleId) | ||
expect(messages[0].line).toBe(1) | ||
expect(messages[0].col).toBe(5) | ||
expect(messages[0].message).toBe( | ||
'Duplicate value [ d-none ] was found in attribute [ class ].' | ||
) | ||
}) | ||
|
||
it('Duplicate values in data attribute should result in an error', () => { | ||
const code = '<span data-attributes="dark light dark">Test</span>' | ||
const messages = HTMLHint.verify(code, ruleOptions) | ||
expect(messages.length).toBe(1) | ||
expect(messages[0].rule.id).toBe(ruleId) | ||
expect(messages[0].line).toBe(1) | ||
expect(messages[0].col).toBe(6) | ||
expect(messages[0].message).toBe( | ||
'Duplicate value [ dark ] was found in attribute [ data-attributes ].' | ||
) | ||
}) | ||
|
||
it('No duplicate values should not result in an error', () => { | ||
const code = '<div class="container fluid small">Test</div>' | ||
const messages = HTMLHint.verify(code, ruleOptions) | ||
expect(messages.length).toBe(0) | ||
}) | ||
|
||
it('Single value should not result in an error', () => { | ||
const code = '<div class="container">Test</div>' | ||
const messages = HTMLHint.verify(code, ruleOptions) | ||
expect(messages.length).toBe(0) | ||
}) | ||
|
||
it('Empty attribute value should not result in an error', () => { | ||
const code = '<div class="">Test</div>' | ||
const messages = HTMLHint.verify(code, ruleOptions) | ||
expect(messages.length).toBe(0) | ||
}) | ||
|
||
it('Multiple attributes with no duplicates should not result in an error', () => { | ||
const code = | ||
'<div class="btn btn-primary" id="submit-button" data-toggle="modal">Test</div>' | ||
const messages = HTMLHint.verify(code, ruleOptions) | ||
expect(messages.length).toBe(0) | ||
}) | ||
|
||
it('Multiple spaces between values should still detect duplicates', () => { | ||
const code = '<div class="btn primary btn">Test</div>' | ||
const messages = HTMLHint.verify(code, ruleOptions) | ||
expect(messages.length).toBe(1) | ||
expect(messages[0].rule.id).toBe(ruleId) | ||
expect(messages[0].message).toBe( | ||
'Duplicate value [ btn ] was found in attribute [ class ].' | ||
) | ||
}) | ||
}) |
59 changes: 59 additions & 0 deletions
59
website/src/content/docs/rules/attr-value-no-duplication.mdx
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,59 @@ | ||
--- | ||
id: attr-value-no-duplication | ||
title: attr-value-no-duplication | ||
description: Prevents duplicate values within the same attribute to ensure clean and efficient markup. | ||
draft: true | ||
sidebar: | ||
badge: New | ||
|
||
--- | ||
|
||
import { Badge } from '@astrojs/starlight/components'; | ||
|
||
Attribute values should not contain duplicates. | ||
|
||
Level: <Badge text="Error" variant="danger" /> | ||
|
||
## Config value | ||
|
||
- `true`: enable rule | ||
- `false`: disable rule | ||
|
||
### The following patterns are **not** considered rule violations | ||
|
||
```html | ||
<div class="container fluid small">Content</div> | ||
``` | ||
|
||
```html | ||
<span data-toggle="modal">Content</span> | ||
``` | ||
|
||
```html | ||
<div class="btn btn-primary btn-large">Button</div> | ||
``` | ||
|
||
### The following patterns are considered rule violations: | ||
|
||
```html | ||
<div class="d-none small d-none">Content</div> | ||
``` | ||
|
||
```html | ||
<span data-attributes="dark light dark">Content</span> | ||
``` | ||
|
||
```html | ||
<div class="btn primary btn">Button</div> | ||
``` | ||
|
||
## Why does this rule exist? | ||
|
||
Having duplicate values in attributes like `class` or custom data attributes can: | ||
|
||
- Make the markup unnecessarily verbose | ||
- Cause confusion during development | ||
- Lead to inefficient CSS specificity calculations | ||
- Indicate potential copy-paste errors or oversight | ||
|
||
This rule helps maintain clean, efficient markup by catching these duplicates early. |
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
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.